-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial support for opentelemetry tracing in maestro server
Signed-off-by: Vu Dinh <vudinh@outlook.com>
- Loading branch information
1 parent
373563d
commit 5ae6b39
Showing
7 changed files
with
192 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package common | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
"go.opentelemetry.io/contrib/exporters/autoexport" | ||
"go.opentelemetry.io/otel" | ||
"go.opentelemetry.io/otel/propagation" | ||
"go.opentelemetry.io/otel/sdk/resource" | ||
tracesdk "go.opentelemetry.io/otel/sdk/trace" | ||
semconv "go.opentelemetry.io/otel/semconv/v1.25.0" | ||
|
||
errors "github.com/zgalor/weberr" | ||
|
||
"github.com/openshift-online/maestro/pkg/constants" | ||
"github.com/openshift-online/maestro/pkg/logger" | ||
) | ||
|
||
// Without a specific configuration, a noop tracer is used by default. | ||
// At least two environment variables must be configured to enable trace export: | ||
// - name: OTEL_EXPORTER_OTLP_ENDPOINT | ||
// value: http(s)://<service>.<namespace>:4318 | ||
// - name: OTEL_TRACES_EXPORTER | ||
// value: otlp | ||
func InstallOpenTelemetryTracer(ctx context.Context, log logger.OCMLogger) (func(context.Context) error, error) { | ||
log.Info("initializing OpenTelemetry tracer") | ||
|
||
exp, err := autoexport.NewSpanExporter(ctx, autoexport.WithFallbackSpanExporter(newNoopFactory)) | ||
if err != nil { | ||
return nil, errors.Errorf("failed to create OTEL exporter: %s", err) | ||
} | ||
|
||
resources, err := resource.New(context.Background(), | ||
resource.WithAttributes( | ||
semconv.ServiceNameKey.String(constants.DefaultSourceID), | ||
), | ||
resource.WithHost(), | ||
) | ||
if err != nil { | ||
return nil, errors.Errorf("failed to initialize trace resources: %s", err) | ||
} | ||
|
||
tp := tracesdk.NewTracerProvider( | ||
tracesdk.WithBatcher(exp), | ||
tracesdk.WithResource(resources), | ||
) | ||
otel.SetTracerProvider(tp) | ||
|
||
shutdown := func(ctx context.Context) error { | ||
ctx, cancel := context.WithTimeout(ctx, 5*time.Second) | ||
defer cancel() | ||
return tp.Shutdown(ctx) | ||
} | ||
|
||
propagator := propagation.NewCompositeTextMapPropagator(propagation.Baggage{}, propagation.TraceContext{}) | ||
otel.SetTextMapPropagator(propagator) | ||
|
||
otel.SetErrorHandler(otelErrorHandlerFunc(func(err error) { | ||
log.Error(fmt.Sprintf("OpenTelemetry.ErrorHandler: %v", err)) | ||
})) | ||
|
||
return shutdown, nil | ||
} | ||
|
||
// TracingEnabled returns true if the environment variable OTEL_TRACES_EXPORTER | ||
// to configure the OpenTelemetry Exporter is defined. | ||
func TracingEnabled() bool { | ||
_, ok := os.LookupEnv("OTEL_TRACES_EXPORTER") | ||
return ok | ||
} | ||
|
||
type otelErrorHandlerFunc func(error) | ||
|
||
// Handle implements otel.ErrorHandler | ||
func (f otelErrorHandlerFunc) Handle(err error) { | ||
f(err) | ||
} | ||
|
||
func newNoopFactory(_ context.Context) (tracesdk.SpanExporter, error) { | ||
return &noopSpanExporter{}, nil | ||
} | ||
|
||
var _ tracesdk.SpanExporter = noopSpanExporter{} | ||
|
||
// noopSpanExporter is an implementation of trace.SpanExporter that performs no operations. | ||
type noopSpanExporter struct{} | ||
|
||
// ExportSpans is part of trace.SpanExporter interface. | ||
func (e noopSpanExporter) ExportSpans(ctx context.Context, spans []tracesdk.ReadOnlySpan) error { | ||
return nil | ||
} | ||
|
||
// Shutdown is part of trace.SpanExporter interface. | ||
func (e noopSpanExporter) Shutdown(ctx context.Context) error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package server | ||
|
||
import ( | ||
"net/http" | ||
|
||
"go.opentelemetry.io/otel/attribute" | ||
"go.opentelemetry.io/otel/baggage" | ||
"go.opentelemetry.io/otel/trace" | ||
|
||
"github.com/openshift-online/maestro/pkg/constants" | ||
"github.com/openshift-online/maestro/pkg/logger" | ||
) | ||
|
||
// traceAttributeMiddleware is currently only relevant for the correlation of | ||
// requests by the ARO-HCP resource provider frontend. | ||
// | ||
// The middleware extracts correlation data transferred in the baggage and sets | ||
// it as an attribute in the currently active span. | ||
// This middleware has no effect if tracing is deactivated or if there is no | ||
// data in the transferred baggage. | ||
func traceAttributeMiddleware(h http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
ctx := logger.WithOpID(r.Context()) | ||
b := baggage.FromContext(ctx) | ||
attrs := []attribute.KeyValue{} | ||
bvalues := []string{constants.ClusterServiceClusterID, constants.AROCorrelationID, constants.AROClientRequestID, constants.ARORequestID, string(logger.OpIDKey)} | ||
for _, k := range bvalues { | ||
if v := b.Member(k).Value(); v != "" { | ||
attrs = append(attrs, attribute.String(k, b.Member(k).Value())) | ||
} | ||
} | ||
|
||
if len(attrs) > 0 { | ||
trace.SpanFromContext(ctx).SetAttributes(attrs...) | ||
} | ||
h.ServeHTTP(w, r) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters