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

SMQ-2671 - Add request ID and use it to correlate traces, logs, and events #2709

Merged
merged 8 commits into from
Feb 19, 2025

Conversation

nyagamunene
Copy link
Contributor

@nyagamunene nyagamunene commented Feb 13, 2025

What type of PR is this?

This is a feature because it adds the following functionality:

  • It adds request id in the transport layer.
  • It logs the request id using the logger.
  • It add request id in the events struct.
  • It changes the tracing id to the request id.

What does this do?

It adds request ID and uses it to correlate trace, logs and events.

Which issue(s) does this PR fix/relate to?

Have you included tests for your changes?

No.

Did you document any new/modified feature?

No.

Notes

@nyagamunene nyagamunene force-pushed the SMQ-2671 branch 2 times, most recently from d3a4444 to ecc6904 Compare February 13, 2025 21:20
@nyagamunene nyagamunene self-assigned this Feb 14, 2025
Copy link

codecov bot commented Feb 17, 2025

Codecov Report

Attention: Patch coverage is 5.68475% with 365 lines in your changes missing coverage. Please review.

Project coverage is 37.70%. Comparing base (da6e3e1) to head (81077b2).

Files with missing lines Patch % Lines
users/events/events.go 0.00% 37 Missing ⚠️
users/events/streams.go 0.00% 34 Missing ⚠️
channels/events/streams.go 0.00% 25 Missing ⚠️
domains/events/events.go 0.00% 24 Missing ⚠️
users/middleware/logging.go 0.00% 23 Missing ⚠️
users/tracing/tracing.go 0.00% 23 Missing ⚠️
groups/events/events.go 0.00% 18 Missing ⚠️
groups/events/streams.go 0.00% 16 Missing ⚠️
clients/events/streams.go 0.00% 15 Missing ⚠️
groups/middleware/logging.go 0.00% 15 Missing ⚠️
... and 14 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2709      +/-   ##
==========================================
- Coverage   42.08%   37.70%   -4.39%     
==========================================
  Files         341      209     -132     
  Lines       47488    36425   -11063     
==========================================
- Hits        19987    13734    -6253     
+ Misses      25318    21618    -3700     
+ Partials     2183     1073    -1110     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@nyagamunene nyagamunene marked this pull request as ready for review February 17, 2025 17:51
@nyagamunene nyagamunene requested a review from a team as a code owner February 17, 2025 17:51
@@ -47,3 +49,20 @@ func AuthenticateMiddleware(authn smqauthn.Authentication, domainCheck bool) fun
})
}
}

func RequestIDMiddleware() func(http.Handler) http.Handler {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should be in utils.

func RequestIDMiddleware() func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
idp := uuid.New()
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should not instantiate a new provider in the handler method, this will result in a new provider for each request.

@@ -41,6 +41,8 @@ func MakeHandler(svc certs.Service, authn smqauthn.Authentication, logger *slog.

r.Group(func(r chi.Router) {
r.Use(api.AuthenticateMiddleware(authn, true))
r.Use(api.RequestIDMiddleware())
Copy link
Collaborator

Choose a reason for hiding this comment

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

Did you consider using Chi native https://github.com/go-chi/chi/blob/master/middleware/request_id.go? Is the key format the main reason we're not using it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Did you consider using Chi native https://github.com/go-chi/chi/blob/master/middleware/request_id.go? Is the key format the main reason we're not using it?

The issue is the format which looks like this 1d825ef43136/dFDftFPuGC-000001 which will be challenge to convert to a traceID

@@ -23,9 +31,28 @@ func New(svc certs.Service, tracer trace.Tracer) certs.Service {
return &tracingMiddleware{tracer, svc}
}

func (tm *tracingMiddleware) startSpan(ctx context.Context, name string, opts ...trace.SpanStartOption) (context.Context, trace.Span) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we extract this so we don't repeat it all over?

reqID := middleware.GetReqID(ctx)
if reqID != "" {
cleanID := strings.ReplaceAll(reqID, separator, emptyString)
final := fmt.Sprintf("%032s", cleanID)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Use constant for the format, just like for separator and empty string.

Signed-off-by: nyagamunene <[email protected]>
Signed-off-by: nyagamunene <[email protected]>
Signed-off-by: nyagamunene <[email protected]>
Signed-off-by: nyagamunene <[email protected]>
Signed-off-by: nyagamunene <[email protected]>
Signed-off-by: nyagamunene <[email protected]>
Signed-off-by: nyagamunene <[email protected]>
@dborovcanin dborovcanin merged commit 72c762f into absmach:main Feb 19, 2025
5 of 7 checks passed
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.

Add request ID and use it to correlate traces, logs, and events (i.e. use the same ID for all the telemetry)
2 participants