forked from grpc/grpc-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
stats/opentelemetry: Introduce Tracing API (grpc#7852)
- Loading branch information
Showing
8 changed files
with
1,334 additions
and
59 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,36 @@ | ||
/* | ||
* Copyright 2024 gRPC authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
// Package opentelemetry is EXPERIMENTAL and will be moved to stats/opentelemetry | ||
// package in a later release. | ||
package opentelemetry | ||
|
||
import ( | ||
"go.opentelemetry.io/otel/propagation" | ||
"go.opentelemetry.io/otel/trace" | ||
) | ||
|
||
// TraceOptions contains the tracing settings for OpenTelemetry instrumentation. | ||
type TraceOptions struct { | ||
// TracerProvider is the OpenTelemetry tracer which is required to | ||
// record traces/trace spans for instrumentation. If unset, tracing | ||
// will not be recorded. | ||
TracerProvider trace.TracerProvider | ||
|
||
// TextMapPropagator propagates span context through text map carrier. | ||
// If unset, tracing will not be recorded. | ||
TextMapPropagator propagation.TextMapPropagator | ||
} |
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,54 @@ | ||
/* | ||
* Copyright 2024 gRPC authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package opentelemetry | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
|
||
"go.opentelemetry.io/otel" | ||
"go.opentelemetry.io/otel/trace" | ||
otelinternaltracing "google.golang.org/grpc/stats/opentelemetry/internal/tracing" | ||
) | ||
|
||
// traceTagRPC populates provided context with a new span using the | ||
// TextMapPropagator supplied in trace options and internal itracing.carrier. | ||
// It creates a new outgoing carrier which serializes information about this | ||
// span into gRPC Metadata, if TextMapPropagator is provided in the trace | ||
// options. if TextMapPropagator is not provided, it returns the context as is. | ||
func (h *clientStatsHandler) traceTagRPC(ctx context.Context, ai *attemptInfo) (context.Context, *attemptInfo) { | ||
mn := "Attempt." + strings.Replace(ai.method, "/", ".", -1) | ||
tracer := otel.Tracer("grpc-open-telemetry") | ||
ctx, span := tracer.Start(ctx, mn) | ||
carrier := otelinternaltracing.NewOutgoingCarrier(ctx) | ||
otel.GetTextMapPropagator().Inject(ctx, carrier) | ||
ai.traceSpan = span | ||
return carrier.Context(), ai | ||
} | ||
|
||
// createCallTraceSpan creates a call span to put in the provided context using | ||
// provided TraceProvider. If TraceProvider is nil, it returns context as is. | ||
func (h *clientStatsHandler) createCallTraceSpan(ctx context.Context, method string) (context.Context, trace.Span) { | ||
if h.options.TraceOptions.TracerProvider == nil { | ||
logger.Error("TraceProvider is not provided in trace options") | ||
return ctx, nil | ||
} | ||
mn := strings.Replace(removeLeadingSlash(method), "/", ".", -1) | ||
tracer := otel.Tracer("grpc-open-telemetry") | ||
ctx, span := tracer.Start(ctx, mn, trace.WithSpanKind(trace.SpanKindClient)) | ||
return ctx, span | ||
} |
Oops, something went wrong.