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

[accountingservice] adds otel logging support #1477

Conversation

Kimbohlovette
Copy link

@Kimbohlovette Kimbohlovette commented Mar 23, 2024

The PR implements logging in the accounting service with slog in order to the slog.LogAttrs() which always receives the current context and attributes.
Use the otelslog - currently in development to collect logs and send to open telemetry.

@Kimbohlovette Kimbohlovette requested a review from a team as a code owner March 23, 2024 13:01
@Kimbohlovette
Copy link
Author

@puckpuck @jparsana please take a look at this PR

@Kimbohlovette Kimbohlovette changed the title Add otel log support to accountingservice (1/7) [accountingservice] adds otel logging support Mar 24, 2024
Copy link
Member

@julianocosta89 julianocosta89 left a comment

Choose a reason for hiding this comment

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

Hello @Kimbohlovette, thanks for taking care of this.

I've tested the PR but I can't see any logs being exported from accountingservice via OTLP.

From the OTel Go repo, I can see that this is still in development.

@pellared do we already have a sample/test for Go OTLP logs?

@Kimbohlovette
Copy link
Author

Alright @julianocosta89. I focused on reproducing the same behavior using otellogrus
Any suggestions on what I could do to get the expected results? I would add that immediately

Copy link
Member

@pellared pellared left a comment

Choose a reason for hiding this comment

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

This does NOT use any bridge using OTel Go Logs Bridge API (experimental) nor OTel Go Logs SDK (under-development).

@Kimbohlovette
Copy link
Author

@pellared is there some recommendation on what I should do to implement what we want?

Copy link
Member

@austinlparker austinlparker left a comment

Choose a reason for hiding this comment

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

This PR is using https://github.com/uptrace/opentelemetry-go-extra, not OpenTelemetry Logging. I'm not sure what the state of the upstream logging bridges are, but I don't think this actually 'adds logging support' at all, rather it's using the logrus utility from the linked repo to attach logs as span events.

@pellared
Copy link
Member

@pellared is there some recommendation on what I should do to implement what we want?

  1. Use slog for logging. Use https://pkg.go.dev/log/slog#Logger.LogAttrs for emitting log record to not lose the tracing context and to add attributes efficiently.
  2. Use OTel slog bridge: https://pkg.go.dev/go.opentelemetry.io/contrib/bridges/otelslog (it is under-development. more: Add slog log bridge opentelemetry-go-contrib#5138)
  3. Setup OTel Logs SDK: https://pkg.go.dev/go.opentelemetry.io/otel/sdk/log (it is under-development. more: https://github.com/orgs/open-telemetry/projects/43)

@Kimbohlovette
Copy link
Author

@pellared is there some recommendation on what I should do to implement what we want?

  1. Use slog for logging. Use https://pkg.go.dev/log/slog#Logger.LogAttrs for emitting log record to not lose the tracing context and to add attributes efficiently.
  2. Use OTel slog bridge: https://pkg.go.dev/go.opentelemetry.io/contrib/bridges/otelslog (it is under-development. more: Add slog log bridge opentelemetry-go-contrib#5138)
  3. Setup OTel Logs SDK: https://pkg.go.dev/go.opentelemetry.io/otel/sdk/log (it is under-development. more: https://github.com/orgs/open-telemetry/projects/43)

Okay @pellared many thanks, I will try to implement it that way. If I get any difficulties I will ping you guys immediately

@pellared
Copy link
Member

Can you please convert this PR to a draft until it is ready to review?

@Kimbohlovette
Copy link
Author

Can you please convert this PR to a draft until it is ready to review?

Okay

@Kimbohlovette Kimbohlovette marked this pull request as draft March 28, 2024 07:53
@Kimbohlovette
Copy link
Author

Kimbohlovette commented Mar 28, 2024

Hello @pellared I just replaced logrus with slog and currently using slog.LogAttrs() & slog.Log() to emit records to os.Stderr. About using the Otel Log bridge, that is , the otelslog, I can't figure out how to add it the the service since it currently does not have examples in its docs. I could you unblock me here please?

@pellared
Copy link
Member

As mentioned, it is under-development. You need to wait.

@Kimbohlovette
Copy link
Author

Kimbohlovette commented Mar 28, 2024

As mentioned, it is under-development. You need to wait.

Okay thanks

Copy link
Member

@pellared pellared left a comment

Choose a reason for hiding this comment

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

I made a quick review for you.

}
log.Out = os.Stdout
func initLogger() *slog.Logger {
logger := slog.New(slog.NewJSONHandler(os.Stderr, nil)).With("service", "accounting")
Copy link
Member

Choose a reason for hiding this comment

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

This is the place where you would use to otelslog bridge instead of slog.NewJSONHandle.

Copy link
Author

Choose a reason for hiding this comment

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

Noted

tp, err := initTracerProvider()
if err != nil {
log.Fatal(err)
logger.LogAttrs(ctx, slog.LevelError, "failed to initialize trace provider", slog.String("error", err.Error()))
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
logger.LogAttrs(ctx, slog.LevelError, "failed to initialize trace provider", slog.String("error", err.Error()))
logger.LogAttrs(ctx, slog.LevelError, "Failed to initialize trace provider", slog.String("error", err.Error()))

if err := tp.Shutdown(context.Background()); err != nil {
log.Printf("Error shutting down tracer provider: %v", err)
if err := tp.Shutdown(ctx); err != nil {
logger.LogAttrs(ctx, slog.LevelError, "failed to shotdown properly", slog.String("error", err.Error()))
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
logger.LogAttrs(ctx, slog.LevelError, "failed to shotdown properly", slog.String("error", err.Error()))
logger.LogAttrs(ctx, slog.LevelError, "Failed to shutdown properly", slog.String("error", err.Error()))

}()

<-ctx.Done()

log.Println("Accounting service exited")
logger.Log(ctx, slog.LevelInfo, "message", "Accounting service exited")
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
logger.Log(ctx, slog.LevelInfo, "message", "Accounting service exited")
logger.LogAttrs(ctx, slog.LevelInfo, "Accounting service exited")

}
log.Println("Shutdown trace provider")
logger.LogAttrs(ctx, slog.LevelInfo, "", slog.String("message", "Shotdown trace provider"))
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
logger.LogAttrs(ctx, slog.LevelInfo, "", slog.String("message", "Shotdown trace provider"))
logger.LogAttrs(ctx, slog.LevelInfo, "Shutdown trace provider")

log.Out = os.Stdout
func initLogger() *slog.Logger {
logger := slog.New(slog.NewJSONHandler(os.Stderr, nil)).With("service", "accounting")
slog.SetDefault(logger)
Copy link
Member

Choose a reason for hiding this comment

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

Why are you setting the default logger. You never use it afterwards.

Consider removing this line or not returning the logger in this function and using slog.LogAttrs instead of logger.LogAttrs.

Copy link
Author

Choose a reason for hiding this comment

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

Okay I will do the changes as requested
Thank you for the reviews 😊

@Kimbohlovette
Copy link
Author

I made a quick review for you.

Okay 😊

@Kimbohlovette
Copy link
Author

Kimbohlovette commented Mar 28, 2024

I have mentained the initialization logger := slog.New(.. at the top of main() because it is passed to the kafka.StartConsumerGroup() method

"messageTimestamp": message.Timestamp,
"messageTopic": message.Topic,
}).Info("Message claimed")
g.log.LogAttrs(session.Context(), slog.LevelInfo, "Message claimed", slog.String("orderId", orderResult.OrderId), slog.String("messageTimestamp", message.Timestamp.String()), slog.String("messageTopic", message.Topic))
Copy link
Member

@pellared pellared Apr 2, 2024

Choose a reason for hiding this comment

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

Notice that session.Context() would NOT contain the trace context information as NewOTelInterceptor does not pass the context created in oi.tracer.Start. Therefore, the logs would not contain the TraceID and SpanID fields.

Copy link
Author

Choose a reason for hiding this comment

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

Okay, so should I just use a new context like context.TODO()?

Copy link
Member

@pellared pellared Apr 2, 2024

Choose a reason for hiding this comment

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

You should try to refine the sarama instrumentation so that you can propagate trace context to the logs.

Copy link
Author

Choose a reason for hiding this comment

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

Okay

@julianocosta89
Copy link
Member

I spoke with @Kimbohlovette today and mentioned that this is not supported yet as OTel logs in Go are still in development.
He will reach out to the OTel Go team to check if he can help out on bringing this to Stable.

Closing this PR for now.

@pellared
Copy link
Member

pellared commented Apr 4, 2024

@julianocosta89, @Kimbohlovette can still find out how to address #1477 (comment). Even though that OTel Go Logs are in development it still something that can be worked on. I cannot find anything more useful to help in OTel Go Logs. We will probably have a usable experimental release of OTel Go Logs this or next month.

It is already possible to use Logs Bridge API and slog Bridge which is everything needed for addressing #1477 (comment).

@julianocosta89
Copy link
Member

@pellared if we can make it work, than that would be great!
Unfortunately I'm a bit out of time to guide @Kimbohlovette on this. Would you know someone, or would you be able to help him out?

Once we get one working, he can apply the same solution for the other 2 Go services.

@pellared
Copy link
Member

pellared commented Apr 4, 2024

Would you know someone, or would you be able to help him out?

I already do my best to do so.

@julianocosta89
Copy link
Member

I already do my best to do so.

Appreciate it!

@Kimbohlovette would you be able to address #1477 (comment)?
I believe that if you are able to work out with it we will be able to have this one merged.

@Kimbohlovette
Copy link
Author

@julianocosta89 yes I will continue to work with @pellared and definitely get it done

@julianocosta89
Copy link
Member

Feel free to re-open the issue whenever you have it in a working state

@Kimbohlovette
Copy link
Author

Alright @julianocosta89

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.

5 participants