go.temporal.io/server/common/telemetry/grpc.go
197 LOC · 98 covered · 99 uncovered · 24 ranges · 77 concepts · 11 introducers · 17 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
package telemetry
import (
"context"
"time"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"
otelnoop "go.opentelemetry.io/otel/trace/noop"
"go.temporal.io/server/common/log"
"go.temporal.io/server/common/log/tag"
"go.temporal.io/server/common/rpc/interceptor/logtags"
"go.temporal.io/server/common/tasktoken"
"google.golang.org/grpc/stats"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)
type methodNameKey struct{}
type (
// ServerStatsHandler gives a named type to the stats.Handler implementation provided by otelgrpc.
ServerStatsHandler stats.Handler
// ClientStatsHandler gives a named type to the grpc.UnaryClientInterceptor implementation provided by otelgrpc.
ClientStatsHandler stats.Handler
customServerStatsHandler struct {
isDebug bool
wrapped stats.Handler
tags *logtags.WorkflowTags
}
)
// NewServerStatsHandler creates a new gRPC stats handler that tracks each request with an encapsulating span
// using the provided TracerProvider and TextMapPropagator.
//
// NOTE: If the TracerProvider is `noop.TracerProvider`, it returns `nil`.
func NewServerStatsHandler(
tp trace.TracerProvider,
tmp propagation.TextMapPropagator,
logger log.Logger,
if !isEnabled(tp) {
}
otelgrpc.NewServerHandler(
otelgrpc.WithPropagators(tmp),
otelgrpc.WithTracerProvider(tp),
),
logger)
}
// NewClientStatsHandler creates a new gRPC stats handler that tracks each request with an encapsulating span
// using the provided TracerProvider and TextMapPropagator.
//
// NOTE: If the TracerProvider is `noop.TracerProvider`, it returns `nil`.
func NewClientStatsHandler(
tp trace.TracerProvider,
tmp propagation.TextMapPropagator,
if !isEnabled(tp) {
}
otelgrpc.WithPropagators(tmp),
otelgrpc.WithTracerProvider(tp),
)
}
func newCustomServerStatsHandler(
handler stats.Handler,
logger log.Logger,
return &customServerStatsHandler{
wrapped: handler,
isDebug: DebugMode(),
tags: logtags.NewWorkflowTags(tasktoken.NewSerializer(), logger),
}
}
func (c *customServerStatsHandler) TagRPC(ctx context.Context, info *stats.RPCTagInfo) context.Context {
grpc.go ×9
return c.wrapped.TagRPC(
context.WithValue(ctx, methodNameKey{}, info.FullMethodName),
info)
}
func (c *customServerStatsHandler) HandleRPC(ctx context.Context, stat stats.RPCStats) {
grpc.go ×9
// handling `End` before wrapped stats.Handler since it closes the span
switch s := stat.(type) {
case *stats.End:
// annotate with gRPC error payload
if c.isDebug {
//revive:disable-next-line:unchecked-type-assertion
statusErr, ok := status.FromError(s.Error)
if ok && statusErr != nil {
span.SetAttributes(attribute.Key("rpc.response.error").String(string(payload)))
}
}
}
switch s := stat.(type) {
if c.isDebug {
span := trace.SpanFromContext(ctx)
for key, values := range s.Header {
span.SetAttributes(attribute.StringSlice("rpc.request.headers."+key, values))
}
if deadline, ok := ctx.Deadline(); ok {
span.SetAttributes(attribute.String("rpc.request.deadline", deadline.Format(time.RFC3339Nano)))
span.SetAttributes(attribute.String("rpc.request.timeout", time.Until(deadline).String()))
}
}
span := trace.SpanFromContext(ctx)
c.annotateTags(ctx, span, s.Payload)
// annotate with gRPC request payload
if c.isDebug {
reqMsg := s.Payload.(proto.Message)
payload, _ := protojson.Marshal(reqMsg)
msgType := string(proto.MessageName(reqMsg).Name())
span.SetAttributes(attribute.Key("rpc.request.payload").String(string(payload)))
span.SetAttributes(attribute.Key("rpc.request.type").String(msgType))
}
if c.isDebug {
span := trace.SpanFromContext(ctx)
for key, values := range s.Header {
span.SetAttributes(attribute.StringSlice("rpc.response.headers."+key, values))
}
}
span := trace.SpanFromContext(ctx)
c.annotateTags(ctx, span, s.Payload)
// annotate with gRPC response payload
if c.isDebug {
respMsg := s.Payload.(proto.Message)
payload, _ := protojson.Marshal(respMsg)
msgType := string(proto.MessageName(respMsg).Name())
span.SetAttributes(attribute.Key("rpc.response.payload").String(string(payload)))
span.SetAttributes(attribute.Key("rpc.response.type").String(msgType))
}
}
}
func (c *customServerStatsHandler) annotateTags(
ctx context.Context,
span trace.Span,
payload any,
methodName, ok := ctx.Value(methodNameKey{}).(string)
if !ok {
methodName = "unknown"
}
// annotate span with workflow tags (same ones the Temporal SDKs use)
var k string
switch logTag.Key() {
case tag.WorkflowIDKey:
k = WorkflowIDKey
case tag.WorkflowRunIDKey:
k = RunIDKey
default:
continue
}
}
}
func (c *customServerStatsHandler) TagConn(ctx context.Context, info *stats.ConnTagInfo) context.Context {
data_store_factory.go ×29
return c.wrapped.TagConn(ctx, info)
}
func (c *customServerStatsHandler) HandleConn(ctx context.Context, stat stats.ConnStats) {
data_store_factory.go ×29
c.wrapped.HandleConn(ctx, stat)
}
_, isNoop := tp.(otelnoop.TracerProvider)
return !isNoop
}