Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding otel audit mw + tests #15

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open

adding otel audit mw + tests #15

wants to merge 8 commits into from

Conversation

toma3233
Copy link
Contributor

@toma3233 toma3233 commented Oct 7, 2024

Adding otel audit middleware to the existing logging http middleware

Based off otel audit framework for go: https://github.com/microsoft/go-otel-audit

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Overview

This PR integrates OpenTelemetry audit logging into the existing HTTP logging middleware and adds corresponding tests. Key changes include:

  • Updating integration tests in logging_test.go to validate audit event behavior.
  • Introducing a new file logging_otelaudit.go that implements OTel audit event creation and delivery.
  • Modifying the logging middleware (logging.go) to accept an OTel configuration object and trigger audit events.

Reviewed Changes

File Description
http/server/logging/logging_test.go Updated tests to cover OTel audit middleware integration
http/server/logging/logging_otelaudit.go Implemented functionality for creating and sending audit events
http/server/logging/logging.go Modified middleware initialization to include OTel configuration

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (1)

http/server/logging/logging_otelaudit.go:38

  • [nitpick] Consider including the remote address (req.RemoteAddr) in the error log to provide additional context for debugging the failure.
logger.Error("failed to split host and port", "error", err)

}

func createOtelAuditEvent(logger slog.Logger, statusCode int, req *http.Request, otelConfig *OtelConfig) msgs.Msg {
host, _, err := net.SplitHostPort(req.RemoteAddr)
Copy link
Preview

Copilot AI Feb 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If net.SplitHostPort fails, the 'host' remains unset and is subsequently used by msgs.ParseAddr, which could lead to unexpected behavior. Consider handling the error with an early return or setting a safe fallback value.

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add background to the README.md file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

logger: *logger,
next: next,
now: time.Now,
logger: *logger,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's passing *logger (a dereferenced slog.Logger) instead of logger itself when initializing the loggingMiddleware struct. Since slog.Logger is a pointer type internally, this means you are copying the entire logger, which might cause unintended behavior, especially if slog.Logger holds state.

Suggested change
logger: *logger,
logger: logger,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

logger slog.Logger
next http.Handler
now func() time.Time
logger slog.Logger

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Accordingly, this canbe changed to pointer type.

Suggested change
logger slog.Logger
logger *slog.Logger

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

"github.com/microsoft/go-otel-audit/audit/msgs"
)

type OtelConfig struct {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like different naming format are being followed with struct name and filed names, let's stick to one format between PascalCase and camelCase.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason why this specific struct is in pascal case is b/c OtelConfig is not an internal only struct, it needs to be available to the caller. In this case, it's not a matter of formatting but rather a Go rule that requires us to capitalize a var name to make it available outside the package.

Comment on lines 37 to 43
if err != nil {
logger.Error("failed to split host and port", "error", err)
}
addr, err := msgs.ParseAddr(host)
if err != nil {
logger.Error("failed to parse address", "error", err)
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still want to continue execution if encoutering an error with parsing? We can consider return early or throw an error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the middleware point of view, I wanted to avoid abruptly stopping the request flow, which is why I decided to continue with execution and let the go-otel-audit package handle returning the error.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we still send the message if it fails to parse? Seems like an unnecessary operation if we can't extract out a meaningful value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed it to return early and check for error in sendOtelAuditEvent function.


func getOperationType(method string) msgs.OperationType {
switch method {
case http.MethodGet:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix indentation

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

func getOperationType(method string) msgs.OperationType {
switch method {
case http.MethodGet:
return msgs.Read

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might not need this since it's same as default

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants