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

Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added otellogrus to the accounting service
Kimbohlovette committed Mar 23, 2024

Verified

This commit was signed with the committer’s verified signature.
Kimbohlovette KIMBOH LOVETTE
commit 230b09aaf75038df80df9150d7c507988a1214f6
4 changes: 3 additions & 1 deletion src/accountingservice/go.mod
Original file line number Diff line number Diff line change
@@ -36,12 +36,14 @@ require (
github.com/klauspost/compress v1.17.5 // indirect
github.com/pierrec/lz4/v4 v4.1.21 // indirect
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/uptrace/opentelemetry-go-extra/otellogrus v0.2.4 // indirect
github.com/uptrace/opentelemetry-go-extra/otelutil v0.2.4 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.24.0 // indirect
go.opentelemetry.io/otel/metric v1.24.0 // indirect
go.opentelemetry.io/proto/otlp v1.1.0 // indirect
golang.org/x/crypto v0.19.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240125205218-1f4bbc51befe // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240125205218-1f4bbc51befe // indirect
6 changes: 6 additions & 0 deletions src/accountingservice/go.sum
Original file line number Diff line number Diff line change
@@ -70,6 +70,10 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/uptrace/opentelemetry-go-extra/otellogrus v0.2.4 h1:3Ok5iuhXYDM2cfs9/a5GPsWma58uo8FJhjWndQtmt/A=
github.com/uptrace/opentelemetry-go-extra/otellogrus v0.2.4/go.mod h1:EiUD7/t6Vre9nayvP5IVdOqbLMabf/c4k8H2j1yHhdg=
github.com/uptrace/opentelemetry-go-extra/otelutil v0.2.4 h1:A6+6ZGgLRoUTD+Jkw/Ph0g8HKiHUsiGlbngcSqBaHsw=
github.com/uptrace/opentelemetry-go-extra/otelutil v0.2.4/go.mod h1:gNYQe4RRVyszriFOhuMpwpAu4kdoFlZgcsw6dcIDFWg=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.opentelemetry.io/otel v1.24.0 h1:0LAOdjNmQeSTzGBzduGe/rU4tZhMwL5rWgtp9Ku5Jfo=
go.opentelemetry.io/otel v1.24.0/go.mod h1:W7b9Ozg4nkF5tWI5zsXkaKKDjdVjpD4oAt9Qi/MArHo=
@@ -114,6 +118,8 @@ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
13 changes: 12 additions & 1 deletion src/accountingservice/kafka/consumer.go
Original file line number Diff line number Diff line change
@@ -4,10 +4,12 @@ package kafka

import (
"context"

pb "github.com/open-telemetry/opentelemetry-demo/src/accountingservice/genproto/oteldemo"

"github.com/IBM/sarama"
"github.com/sirupsen/logrus"
"github.com/uptrace/opentelemetry-go-extra/otellogrus"
"google.golang.org/protobuf/proto"
)

@@ -17,6 +19,15 @@ var (
GroupID = "accountingservice"
)

func initLogger() {
logrus.AddHook(otellogrus.NewHook(otellogrus.WithLevels(
logrus.PanicLevel,
logrus.FatalLevel,
logrus.ErrorLevel,
logrus.WarnLevel,
)))
}

func StartConsumerGroup(ctx context.Context, brokers []string, log *logrus.Logger) (sarama.ConsumerGroup, error) {
saramaConfig := sarama.NewConfig()
saramaConfig.Version = ProtocolVersion
@@ -63,7 +74,7 @@ func (g *groupHandler) ConsumeClaim(session sarama.ConsumerGroupSession, claim s
return err
}

g.log.WithFields(logrus.Fields{
g.log.WithContext(context.Background()).WithFields(logrus.Fields{
"orderId": orderResult.OrderId,
"messageTimestamp": message.Timestamp,
"messageTopic": message.Topic,
36 changes: 17 additions & 19 deletions src/accountingservice/main.go
Original file line number Diff line number Diff line change
@@ -14,10 +14,10 @@ import (
"strings"
"sync"
"syscall"
"time"

"github.com/IBM/sarama"
"github.com/sirupsen/logrus"
"github.com/uptrace/opentelemetry-go-extra/otellogrus"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/propagation"
@@ -33,15 +33,12 @@ 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.AddHook(otellogrus.NewHook(otellogrus.WithLevels(
logrus.PanicLevel,
logrus.FatalLevel,
logrus.ErrorLevel,
logrus.WarnLevel,
)))
log.Out = os.Stdout
}

@@ -79,39 +76,40 @@ func initTracerProvider() (*sdktrace.TracerProvider, error) {
}

func main() {
ctx := context.Background()
tp, err := initTracerProvider()
if err != nil {
log.Fatal(err)
log.WithContext(ctx).WithContext(ctx)
}
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 {
log.WithContext(ctx).WithError(err)
}
log.Println("Shutdown trace provider")
log.WithContext(ctx).WithField("Message", "Shotdown trace provider")
}()

var brokers string
mustMapEnv(&brokers, "KAFKA_SERVICE_ADDR")

brokerList := strings.Split(brokers, ",")
log.Printf("Kafka brokers: %s", strings.Join(brokerList, ", "))
log.WithField("Kafka brokers: %s", 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)
log.WithContext(ctx).WithError(err)
}
defer func() {
if err := consumerGroup.Close(); err != nil {
log.Printf("Error closing consumer group: %v", err)
log.WithContext(ctx).WithField("Error closing consumer group: %v", err)
}
log.Println("Closed consumer group")
log.WithContext(ctx).WithField("Message", "Closed consumer group")
}()

<-ctx.Done()

log.Println("Accounting service exited")
log.WithContext(ctx).WithField("Message", "Accounting service exited")
}

func mustMapEnv(target *string, envKey string) {