diff --git a/core/zap/logger.go b/core/zap/logger.go index 2454ea058..e225473df 100644 --- a/core/zap/logger.go +++ b/core/zap/logger.go @@ -4,8 +4,9 @@ import ( "context" "fmt" + "go.opentelemetry.io/otel/attribute" + "flamingo.me/flamingo/v3/framework/opentelemetry" - "go.opentelemetry.io/otel/baggage" "go.opentelemetry.io/otel/metric/instrument" "go.opentelemetry.io/otel/metric/instrument/syncint64" "go.opentelemetry.io/otel/metric/unit" @@ -18,8 +19,8 @@ import ( ) var ( - logCount syncint64.Counter - keyLevel, _ = baggage.NewKeyProperty("level") + logCount syncint64.Counter + keyLevel attribute.Key = "level" ) func init() { @@ -74,11 +75,8 @@ func (l *Logger) WithContext(ctx context.Context) flamingo.Logger { } func (l *Logger) record(level string) { - areaBaggage, _ := baggage.NewMember(opentelemetry.KeyArea.Key(), l.configArea) - levelBaggage, _ := baggage.NewMember(keyLevel.Key(), level) - bagg, _ := baggage.New(areaBaggage, levelBaggage) - ctx := baggage.ContextWithBaggage(context.Background(), bagg) - logCount.Add(ctx, 1) + logCount.Add(context.Background(), 1, attribute.String(opentelemetry.KeyArea.Key(), l.configArea), + keyLevel.String(level)) } // Debug logs a message at debug level diff --git a/framework/opentelemetry/module.go b/framework/opentelemetry/module.go index 45f7ddc78..4c6e61dbf 100644 --- a/framework/opentelemetry/module.go +++ b/framework/opentelemetry/module.go @@ -98,7 +98,10 @@ func (m *Module) Configure(injector *dingo.Injector) { schemaURL, attribute.String("service.name", m.serviceName), )), - tracesdk.WithSampler(tracesdk.NeverSample()), + tracesdk.WithSampler(tracesdk.ParentBased(tracesdk.NeverSample(), + tracesdk.WithLocalParentSampled(tracesdk.AlwaysSample()), tracesdk.WithLocalParentNotSampled(tracesdk.NeverSample()), + tracesdk.WithRemoteParentSampled(tracesdk.AlwaysSample()), tracesdk.WithRemoteParentNotSampled(tracesdk.NeverSample()), + )), ) tp := tracesdk.NewTracerProvider( tracerProviderOptions..., @@ -141,7 +144,7 @@ type correlationIDInjector struct { func (rt *correlationIDInjector) RoundTrip(req *http.Request) (*http.Response, error) { span := trace.SpanFromContext(req.Context()) - if span.IsRecording() { + if span.SpanContext().IsSampled() { req.Header.Add("X-Correlation-ID", span.SpanContext().TraceID().String()) } return rt.next.RoundTrip(req) @@ -174,3 +177,25 @@ func GetMeter() metric.Meter { }) return meter } + +func (m *Module) CueConfig() string { + return ` +flamingo: opentelemetry: { + jaeger: { + enable: bool | *false + endpoint: string | *"http://localhost:14268/api/traces" + } + zipkin: { + enable: bool | *false + endpoint: string | *"http://localhost:9411/api/v2/spans" + } + serviceName: string | *"flamingo" + tracing: sampler: { + whitelist: [...string] + blacklist: [...string] + allowParentTrace: bool | *true + } + publicEndpoint: bool | *true +} +` +} diff --git a/framework/web/filter/request_metrics_filter.go b/framework/web/filter/request_metrics_filter.go index b66e196c9..8ae42c64e 100644 --- a/framework/web/filter/request_metrics_filter.go +++ b/framework/web/filter/request_metrics_filter.go @@ -5,8 +5,9 @@ import ( "net/http" "strconv" + "go.opentelemetry.io/otel/attribute" + "flamingo.me/flamingo/v3/framework/opentelemetry" - "go.opentelemetry.io/otel/baggage" "go.opentelemetry.io/otel/metric/instrument" "go.opentelemetry.io/otel/metric/instrument/syncint64" "go.opentelemetry.io/otel/metric/unit" @@ -36,7 +37,7 @@ var ( // responseCount count the number of responses served by the application responsesCount syncint64.Counter // keyHTTPStatus defines response http status code - keyHTTPStatus, _ = baggage.NewKeyProperty("status_code") + keyHTTPStatus attribute.Key = "status_code" ) func init() { @@ -82,12 +83,9 @@ func (r responseMetrics) Apply(ctx context.Context, rw http.ResponseWriter) erro err = r.result.Apply(ctx, responseWriter) } - statusBaggage, _ := baggage.NewMember(keyHTTPStatus.Key(), strconv.Itoa(responseWriter.status/100)+"xx") - bagg := baggage.FromContext(ctx) - bagg, _ = bagg.SetMember(statusBaggage) - c := baggage.ContextWithBaggage(ctx, bagg) - responseBytesCount.Add(c, responseWriter.bytes) - responsesCount.Add(c, 1) + statusAttribute := keyHTTPStatus.String(strconv.Itoa(responseWriter.status/100) + "xx") + responseBytesCount.Add(ctx, responseWriter.bytes, statusAttribute) + responsesCount.Add(ctx, 1, statusAttribute) return err }