| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package otelgrpc_test |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
|
| 9 |
"go.opentelemetry.io/otel/attribute" |
| 10 |
"go.opentelemetry.io/otel/baggage" |
| 11 |
"go.opentelemetry.io/otel/propagation" |
| 12 |
"google.golang.org/grpc" |
| 13 |
"google.golang.org/grpc/metadata" |
| 14 |
|
| 15 |
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc" |
| 16 |
) |
| 17 |
|
| 18 |
func ExampleNewClientHandler() { |
| 19 |
_, _ = grpc.NewClient("localhost", grpc.WithStatsHandler(otelgrpc.NewClientHandler())) |
| 20 |
} |
| 21 |
|
| 22 |
func ExampleNewServerHandler() { |
| 23 |
_ = grpc.NewServer(grpc.StatsHandler(otelgrpc.NewServerHandler())) |
| 24 |
} |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
func ExampleNewClientHandler_withMetricAttributesFn_interceptor() { |
| 29 |
|
| 30 |
type retryAttemptKey struct{} |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
_, _ = grpc.NewClient("localhost", grpc.WithStatsHandler(otelgrpc.NewClientHandler( |
| 43 |
otelgrpc.WithMetricAttributesFn(func(ctx context.Context) []attribute.KeyValue { |
| 44 |
if attempt, ok := ctx.Value(retryAttemptKey{}).(int); ok { |
| 45 |
return []attribute.KeyValue{ |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
attribute.Int("retry.attempt", attempt), |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
return nil |
| 54 |
}), |
| 55 |
))) |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
func ExampleNewClientHandler_withMetricAttributesFn_baggage() { |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
_, _ = grpc.NewClient("localhost", grpc.WithStatsHandler(otelgrpc.NewClientHandler( |
| 71 |
otelgrpc.WithMetricAttributesFn(func(ctx context.Context) []attribute.KeyValue { |
| 72 |
bag := baggage.FromContext(ctx) |
| 73 |
if trafficType := bag.Member("traffic.type"); trafficType.Value() != "" { |
| 74 |
return []attribute.KeyValue{ |
| 75 |
attribute.String("traffic.type", trafficType.Value()), |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
return nil |
| 80 |
}), |
| 81 |
))) |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
func ExampleNewServerHandler_withMetricAttributesFn_metadata() { |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
_ = grpc.NewServer(grpc.StatsHandler(otelgrpc.NewServerHandler( |
| 92 |
otelgrpc.WithMetricAttributesFn(func(ctx context.Context) []attribute.KeyValue { |
| 93 |
md, ok := metadata.FromIncomingContext(ctx) |
| 94 |
if ok { |
| 95 |
if origins := md.Get("origin"); len(origins) > 0 && origins[0] != "" { |
| 96 |
return []attribute.KeyValue{attribute.String("origin", origins[0])} |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
return []attribute.KeyValue{attribute.String("origin", "unknown")} |
| 102 |
}), |
| 103 |
))) |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
func ExampleNewServerHandler_withMetricAttributesFn_baggage() { |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
_ = grpc.NewServer(grpc.StatsHandler(otelgrpc.NewServerHandler( |
| 129 |
|
| 130 |
otelgrpc.WithPropagators(propagation.NewCompositeTextMapPropagator( |
| 131 |
propagation.TraceContext{}, |
| 132 |
propagation.Baggage{}, |
| 133 |
)), |
| 134 |
otelgrpc.WithMetricAttributesFn(func(ctx context.Context) []attribute.KeyValue { |
| 135 |
bag := baggage.FromContext(ctx) |
| 136 |
if tier := bag.Member("tenant.tier"); tier.Value() != "" { |
| 137 |
return []attribute.KeyValue{ |
| 138 |
attribute.String("tenant.tier", tier.Value()), |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
return nil |
| 143 |
}), |
| 144 |
))) |
| 145 |
} |