-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): add open telemetry support for Tracetest agent (#3662)
* wip * wip * chore: add support for otel collector on tracetest * adding support for OTel tracer on workers * Add fallback to ignore tracer if no collector endpoint is declared * adding PR suggestions
- Loading branch information
1 parent
46e50da
commit 471fc43
Showing
14 changed files
with
313 additions
and
84 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
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
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,88 @@ | ||
package telemetry | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"go.opentelemetry.io/otel" | ||
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc" | ||
"go.opentelemetry.io/otel/propagation" | ||
"go.opentelemetry.io/otel/sdk/resource" | ||
sdkTrace "go.opentelemetry.io/otel/sdk/trace" | ||
semconv "go.opentelemetry.io/otel/semconv/v1.4.0" | ||
"go.opentelemetry.io/otel/trace" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/credentials/insecure" | ||
) | ||
|
||
const spanExporterTimeout = 1 * time.Minute | ||
|
||
func GetTracer(ctx context.Context, otelExporterEndpoint, serviceName string) (trace.Tracer, error) { | ||
if otelExporterEndpoint == "" { | ||
// fallback, return noop | ||
return trace.NewNoopTracerProvider().Tracer("noop"), nil | ||
} | ||
|
||
realServiceName := fmt.Sprintf("tracetestAgent_%s", serviceName) | ||
|
||
spanExporter, err := newSpanExporter(ctx, otelExporterEndpoint) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to setup span exporter: %w", err) | ||
} | ||
|
||
traceProvider, err := newTraceProvider(ctx, spanExporter, realServiceName) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to setup trace provider: %w", err) | ||
} | ||
|
||
return traceProvider.Tracer(realServiceName), nil | ||
} | ||
|
||
func newSpanExporter(ctx context.Context, otelExporterEndpoint string) (sdkTrace.SpanExporter, error) { | ||
ctx, cancel := context.WithTimeout(ctx, spanExporterTimeout) | ||
defer cancel() | ||
|
||
conn, err := grpc.DialContext(ctx, otelExporterEndpoint, grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithBlock()) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create gRPC connection to collector: %w", err) | ||
} | ||
|
||
traceExporter, err := otlptracegrpc.New(ctx, otlptracegrpc.WithGRPCConn(conn)) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create trace exporter: %w", err) | ||
} | ||
|
||
return traceExporter, nil | ||
} | ||
|
||
func newTraceProvider(ctx context.Context, spanExporter sdkTrace.SpanExporter, serviceName string) (*sdkTrace.TracerProvider, error) { | ||
defaultResource := resource.Default() | ||
|
||
mergedResource, err := resource.Merge( | ||
defaultResource, | ||
resource.NewWithAttributes( | ||
defaultResource.SchemaURL(), | ||
semconv.ServiceNameKey.String(serviceName), | ||
), | ||
) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create otel resource: %w", err) | ||
} | ||
|
||
tp := sdkTrace.NewTracerProvider( | ||
sdkTrace.WithBatcher(spanExporter), | ||
sdkTrace.WithResource(mergedResource), | ||
) | ||
|
||
otel.SetTracerProvider(tp) | ||
|
||
otel.SetTextMapPropagator( | ||
propagation.NewCompositeTextMapPropagator( | ||
propagation.TraceContext{}, | ||
propagation.Baggage{}, | ||
), | ||
) | ||
|
||
return tp, 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
Oops, something went wrong.