-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Changes from 5 commits
230b09a
ec2ddb4
f50deea
6a5545a
7c016d3
d138dc8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -9,15 +9,14 @@ package main | |||||
import ( | ||||||
"context" | ||||||
"fmt" | ||||||
"log/slog" | ||||||
"os" | ||||||
"os/signal" | ||||||
"strings" | ||||||
"sync" | ||||||
"syscall" | ||||||
"time" | ||||||
|
||||||
"github.com/IBM/sarama" | ||||||
"github.com/sirupsen/logrus" | ||||||
"go.opentelemetry.io/otel" | ||||||
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc" | ||||||
"go.opentelemetry.io/otel/propagation" | ||||||
|
@@ -27,24 +26,14 @@ import ( | |||||
"github.com/open-telemetry/opentelemetry-demo/src/accountingservice/kafka" | ||||||
) | ||||||
|
||||||
var log *logrus.Logger | ||||||
var resource *sdkresource.Resource | ||||||
var initResourcesOnce sync.Once | ||||||
|
||||||
func init() { | ||||||
log = logrus.New() | ||||||
log.Level = logrus.DebugLevel | ||||||
log.Formatter = &logrus.JSONFormatter{ | ||||||
FieldMap: logrus.FieldMap{ | ||||||
logrus.FieldKeyTime: "timestamp", | ||||||
logrus.FieldKeyLevel: "severity", | ||||||
logrus.FieldKeyMsg: "message", | ||||||
}, | ||||||
TimestampFormat: time.RFC3339Nano, | ||||||
} | ||||||
log.Out = os.Stdout | ||||||
func initLogger() *slog.Logger { | ||||||
logger := slog.New(slog.NewJSONHandler(os.Stderr, nil)).With("service", "accounting") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the place where you would use to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Noted |
||||||
slog.SetDefault(logger) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay I will do the changes as requested |
||||||
return logger | ||||||
} | ||||||
|
||||||
func initResource() *sdkresource.Resource { | ||||||
initResourcesOnce.Do(func() { | ||||||
extraResources, _ := sdkresource.New( | ||||||
|
@@ -79,39 +68,41 @@ func initTracerProvider() (*sdktrace.TracerProvider, error) { | |||||
} | ||||||
|
||||||
func main() { | ||||||
logger := initLogger() | ||||||
ctx := context.Background() | ||||||
tp, err := initTracerProvider() | ||||||
if err != nil { | ||||||
log.Fatal(err) | ||||||
logger.LogAttrs(ctx, slog.LevelError, "failed to initialize trace provider", slog.String("error", err.Error())) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
defer func() { | ||||||
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())) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
log.Println("Shutdown trace provider") | ||||||
logger.LogAttrs(ctx, slog.LevelInfo, "", slog.String("message", "Shotdown trace provider")) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
}() | ||||||
|
||||||
var brokers string | ||||||
mustMapEnv(&brokers, "KAFKA_SERVICE_ADDR") | ||||||
|
||||||
brokerList := strings.Split(brokers, ",") | ||||||
log.Printf("Kafka brokers: %s", strings.Join(brokerList, ", ")) | ||||||
logger.LogAttrs(ctx, slog.LevelInfo, "Kafka brokers", slog.String("Kafka brokers", strings.Join(brokerList, ","))) | ||||||
|
||||||
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM, syscall.SIGKILL) | ||||||
defer cancel() | ||||||
var consumerGroup sarama.ConsumerGroup | ||||||
if consumerGroup, err = kafka.StartConsumerGroup(ctx, brokerList, log); err != nil { | ||||||
log.Fatal(err) | ||||||
if consumerGroup, err = kafka.StartConsumerGroup(ctx, brokerList, logger); err != nil { | ||||||
logger.LogAttrs(ctx, slog.LevelError, "Failed to start consumer group", slog.String("error", err.Error())) | ||||||
} | ||||||
defer func() { | ||||||
if err := consumerGroup.Close(); err != nil { | ||||||
log.Printf("Error closing consumer group: %v", err) | ||||||
logger.LogAttrs(ctx, slog.LevelError, "Error closing consumer group", slog.String("error", err.Error())) | ||||||
} | ||||||
log.Println("Closed consumer group") | ||||||
logger.Log(ctx, slog.LevelInfo, "Closed consumer group") | ||||||
}() | ||||||
|
||||||
<-ctx.Done() | ||||||
|
||||||
log.Println("Accounting service exited") | ||||||
logger.Log(ctx, slog.LevelInfo, "message", "Accounting service exited") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
|
||||||
func mustMapEnv(target *string, envKey string) { | ||||||
|
There was a problem hiding this comment.
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 asNewOTelInterceptor
does not pass the context created inoi.tracer.Start
. Therefore, the logs would not contain theTraceID
andSpanID
fields.There was a problem hiding this comment.
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()
?There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay