| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package otelgrpc_test |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"errors" |
| 9 |
"io" |
| 10 |
"net" |
| 11 |
"strconv" |
| 12 |
"sync" |
| 13 |
"testing" |
| 14 |
"time" |
| 15 |
|
| 16 |
"github.com/stretchr/testify/assert" |
| 17 |
"github.com/stretchr/testify/require" |
| 18 |
"go.opentelemetry.io/otel/attribute" |
| 19 |
"go.opentelemetry.io/otel/sdk/instrumentation" |
| 20 |
"go.opentelemetry.io/otel/sdk/metric" |
| 21 |
"go.opentelemetry.io/otel/sdk/metric/metricdata" |
| 22 |
"go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest" |
| 23 |
"go.opentelemetry.io/otel/sdk/trace" |
| 24 |
"go.opentelemetry.io/otel/sdk/trace/tracetest" |
| 25 |
semconv "go.opentelemetry.io/otel/semconv/v1.40.0" |
| 26 |
"go.opentelemetry.io/otel/semconv/v1.40.0/rpcconv" |
| 27 |
oteltrace "go.opentelemetry.io/otel/trace" |
| 28 |
"google.golang.org/grpc" |
| 29 |
"google.golang.org/grpc/codes" |
| 30 |
testpb "google.golang.org/grpc/interop/grpc_testing" |
| 31 |
"google.golang.org/grpc/stats" |
| 32 |
"google.golang.org/grpc/status" |
| 33 |
|
| 34 |
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc" |
| 35 |
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc/filters" |
| 36 |
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc/internal/test" |
| 37 |
) |
| 38 |
|
| 39 |
var ( |
| 40 |
testSpanAttr = attribute.String("test_span", "OK") |
| 41 |
testMetricAttr = attribute.String("test_metric", "OK") |
| 42 |
) |
| 43 |
|
| 44 |
func TestStatsHandler(t *testing.T) { |
| 45 |
tests := []struct { |
| 46 |
name string |
| 47 |
filterSvcName string |
| 48 |
expectRecorded bool |
| 49 |
}{ |
| 50 |
{ |
| 51 |
name: "Recorded", |
| 52 |
filterSvcName: "grpc.testing.TestService", |
| 53 |
expectRecorded: true, |
| 54 |
}, |
| 55 |
{ |
| 56 |
name: "Dropped", |
| 57 |
filterSvcName: "grpc.testing.OtherService", |
| 58 |
expectRecorded: false, |
| 59 |
}, |
| 60 |
} |
| 61 |
|
| 62 |
for _, tt := range tests { |
| 63 |
t.Run(tt.name, func(t *testing.T) { |
| 64 |
t.Setenv("OTEL_METRICS_EXEMPLAR_FILTER", "always_off") |
| 65 |
clientSR := tracetest.NewSpanRecorder() |
| 66 |
clientTP := trace.NewTracerProvider(trace.WithSpanProcessor(clientSR)) |
| 67 |
clientMetricReader := metric.NewManualReader() |
| 68 |
clientMP := metric.NewMeterProvider(metric.WithReader(clientMetricReader)) |
| 69 |
|
| 70 |
serverSR := tracetest.NewSpanRecorder() |
| 71 |
serverTP := trace.NewTracerProvider(trace.WithSpanProcessor(serverSR)) |
| 72 |
serverMetricReader := metric.NewManualReader() |
| 73 |
serverMP := metric.NewMeterProvider(metric.WithReader(serverMetricReader)) |
| 74 |
|
| 75 |
listener, err := (&net.ListenConfig{}).Listen(t.Context(), "tcp", "127.0.0.1:0") |
| 76 |
require.NoError(t, err, "failed to open port") |
| 77 |
client := newGrpcTest(t, listener, |
| 78 |
[]grpc.DialOption{ |
| 79 |
grpc.WithStatsHandler(otelgrpc.NewClientHandler( |
| 80 |
otelgrpc.WithTracerProvider(clientTP), |
| 81 |
otelgrpc.WithMeterProvider(clientMP), |
| 82 |
otelgrpc.WithMessageEvents(otelgrpc.ReceivedEvents, otelgrpc.SentEvents), |
| 83 |
otelgrpc.WithFilter(filters.ServiceName(tt.filterSvcName)), |
| 84 |
otelgrpc.WithSpanAttributes(testSpanAttr), |
| 85 |
otelgrpc.WithMetricAttributes(testMetricAttr)), |
| 86 |
), |
| 87 |
}, |
| 88 |
[]grpc.ServerOption{ |
| 89 |
grpc.StatsHandler(otelgrpc.NewServerHandler( |
| 90 |
otelgrpc.WithTracerProvider(serverTP), |
| 91 |
otelgrpc.WithMeterProvider(serverMP), |
| 92 |
otelgrpc.WithMessageEvents(otelgrpc.ReceivedEvents, otelgrpc.SentEvents), |
| 93 |
otelgrpc.WithFilter(filters.ServiceName(tt.filterSvcName)), |
| 94 |
otelgrpc.WithSpanAttributes(testSpanAttr), |
| 95 |
otelgrpc.WithMetricAttributes(testMetricAttr)), |
| 96 |
), |
| 97 |
}, |
| 98 |
) |
| 99 |
ctx, cancel := context.WithCancel(t.Context()) |
| 100 |
defer cancel() |
| 101 |
doCalls(ctx, client) |
| 102 |
|
| 103 |
if tt.expectRecorded { |
| 104 |
t.Run("ClientSpans", func(t *testing.T) { |
| 105 |
checkClientSpans(t, clientSR.Ended(), listener.Addr().String()) |
| 106 |
}) |
| 107 |
|
| 108 |
t.Run("ClientMetrics", func(t *testing.T) { |
| 109 |
checkClientMetrics(t, clientMetricReader, listener.Addr().String(), "") |
| 110 |
}) |
| 111 |
|
| 112 |
t.Run("ServerSpans", func(t *testing.T) { |
| 113 |
checkServerSpans(t, serverSR, listener.Addr().String()) |
| 114 |
}) |
| 115 |
|
| 116 |
t.Run("ServerMetrics", func(t *testing.T) { |
| 117 |
checkServerMetrics(t, serverMetricReader, "") |
| 118 |
}) |
| 119 |
} else { |
| 120 |
t.Run("ClientSpans", func(t *testing.T) { |
| 121 |
require.Empty(t, clientSR.Ended()) |
| 122 |
}) |
| 123 |
|
| 124 |
t.Run("ClientMetrics", func(t *testing.T) { |
| 125 |
rm := metricdata.ResourceMetrics{} |
| 126 |
err := clientMetricReader.Collect(t.Context(), &rm) |
| 127 |
assert.NoError(t, err) |
| 128 |
require.Empty(t, rm.ScopeMetrics) |
| 129 |
}) |
| 130 |
|
| 131 |
t.Run("ServerSpans", func(t *testing.T) { |
| 132 |
require.Empty(t, serverSR.Ended()) |
| 133 |
}) |
| 134 |
|
| 135 |
t.Run("ServerMetrics", func(t *testing.T) { |
| 136 |
rm := metricdata.ResourceMetrics{} |
| 137 |
err := serverMetricReader.Collect(t.Context(), &rm) |
| 138 |
assert.NoError(t, err) |
| 139 |
require.Empty(t, rm.ScopeMetrics) |
| 140 |
}) |
| 141 |
} |
| 142 |
}) |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
func checkClientSpans(t *testing.T, spans []trace.ReadOnlySpan, addr string) { |
| 147 |
require.Len(t, spans, 5) |
| 148 |
|
| 149 |
host, p, err := net.SplitHostPort(addr) |
| 150 |
require.NoError(t, err) |
| 151 |
port, err := strconv.Atoi(p) |
| 152 |
require.NoError(t, err) |
| 153 |
|
| 154 |
emptySpan := spans[0] |
| 155 |
assert.False(t, emptySpan.EndTime().IsZero()) |
| 156 |
assert.Equal(t, "grpc.testing.TestService/EmptyCall", emptySpan.Name()) |
| 157 |
assert.Empty(t, emptySpan.Events()) |
| 158 |
assert.ElementsMatch(t, []attribute.KeyValue{ |
| 159 |
semconv.RPCMethodKey.String("grpc.testing.TestService/EmptyCall"), |
| 160 |
semconv.RPCSystemNameGRPC, |
| 161 |
semconv.RPCResponseStatusCode(codes.OK.String()), |
| 162 |
semconv.ServerAddress(host), |
| 163 |
semconv.ServerPort(port), |
| 164 |
testSpanAttr, |
| 165 |
}, emptySpan.Attributes()) |
| 166 |
|
| 167 |
largeSpan := spans[1] |
| 168 |
assert.False(t, largeSpan.EndTime().IsZero()) |
| 169 |
assert.Equal(t, "grpc.testing.TestService/UnaryCall", largeSpan.Name()) |
| 170 |
assert.Empty(t, largeSpan.Events()) |
| 171 |
assert.ElementsMatch(t, []attribute.KeyValue{ |
| 172 |
semconv.RPCMethodKey.String("grpc.testing.TestService/UnaryCall"), |
| 173 |
semconv.RPCSystemNameGRPC, |
| 174 |
semconv.RPCResponseStatusCode(codes.OK.String()), |
| 175 |
semconv.ServerAddress(host), |
| 176 |
semconv.ServerPort(port), |
| 177 |
testSpanAttr, |
| 178 |
}, largeSpan.Attributes()) |
| 179 |
|
| 180 |
streamInput := spans[2] |
| 181 |
assert.False(t, streamInput.EndTime().IsZero()) |
| 182 |
assert.Equal(t, "grpc.testing.TestService/StreamingInputCall", streamInput.Name()) |
| 183 |
assert.Empty(t, streamInput.Events()) |
| 184 |
assert.ElementsMatch(t, []attribute.KeyValue{ |
| 185 |
semconv.RPCMethodKey.String("grpc.testing.TestService/StreamingInputCall"), |
| 186 |
semconv.RPCSystemNameGRPC, |
| 187 |
semconv.RPCResponseStatusCode(codes.OK.String()), |
| 188 |
semconv.ServerAddress(host), |
| 189 |
semconv.ServerPort(port), |
| 190 |
testSpanAttr, |
| 191 |
}, streamInput.Attributes()) |
| 192 |
|
| 193 |
streamOutput := spans[3] |
| 194 |
assert.False(t, streamOutput.EndTime().IsZero()) |
| 195 |
assert.Equal(t, "grpc.testing.TestService/StreamingOutputCall", streamOutput.Name()) |
| 196 |
assert.Empty(t, streamOutput.Events()) |
| 197 |
assert.ElementsMatch(t, []attribute.KeyValue{ |
| 198 |
semconv.RPCMethodKey.String("grpc.testing.TestService/StreamingOutputCall"), |
| 199 |
semconv.RPCSystemNameGRPC, |
| 200 |
semconv.RPCResponseStatusCode(codes.OK.String()), |
| 201 |
semconv.ServerAddress(host), |
| 202 |
semconv.ServerPort(port), |
| 203 |
testSpanAttr, |
| 204 |
}, streamOutput.Attributes()) |
| 205 |
|
| 206 |
pingPong := spans[4] |
| 207 |
assert.False(t, pingPong.EndTime().IsZero()) |
| 208 |
assert.Equal(t, "grpc.testing.TestService/FullDuplexCall", pingPong.Name()) |
| 209 |
assert.Empty(t, pingPong.Events()) |
| 210 |
assert.ElementsMatch(t, []attribute.KeyValue{ |
| 211 |
semconv.RPCMethodKey.String("grpc.testing.TestService/FullDuplexCall"), |
| 212 |
semconv.RPCSystemNameGRPC, |
| 213 |
semconv.RPCResponseStatusCode(codes.OK.String()), |
| 214 |
semconv.ServerAddress(host), |
| 215 |
semconv.ServerPort(port), |
| 216 |
testSpanAttr, |
| 217 |
}, pingPong.Attributes()) |
| 218 |
} |
| 219 |
|
| 220 |
func checkServerSpans(t *testing.T, sr *tracetest.SpanRecorder, addr string) { |
| 221 |
host, p, err := net.SplitHostPort(addr) |
| 222 |
require.NoError(t, err) |
| 223 |
port, err := strconv.Atoi(p) |
| 224 |
require.NoError(t, err) |
| 225 |
|
| 226 |
var spans []trace.ReadOnlySpan |
| 227 |
require.Eventually(t, func() bool { |
| 228 |
spans = sr.Ended() |
| 229 |
return len(spans) == 5 |
| 230 |
}, 1*time.Second, 10*time.Millisecond) |
| 231 |
|
| 232 |
spansByName := make(map[string]trace.ReadOnlySpan, len(spans)) |
| 233 |
for _, s := range spans { |
| 234 |
spansByName[s.Name()] = s |
| 235 |
} |
| 236 |
|
| 237 |
for _, tc := range []struct { |
| 238 |
name string |
| 239 |
}{ |
| 240 |
{"grpc.testing.TestService/EmptyCall"}, |
| 241 |
{"grpc.testing.TestService/UnaryCall"}, |
| 242 |
{"grpc.testing.TestService/StreamingInputCall"}, |
| 243 |
{"grpc.testing.TestService/StreamingOutputCall"}, |
| 244 |
{"grpc.testing.TestService/FullDuplexCall"}, |
| 245 |
} { |
| 246 |
s, ok := spansByName[tc.name] |
| 247 |
if !assert.True(t, ok, "missing span %s", tc.name) { |
| 248 |
continue |
| 249 |
} |
| 250 |
assert.False(t, s.EndTime().IsZero()) |
| 251 |
assert.Equal(t, tc.name, s.Name()) |
| 252 |
assert.Empty(t, s.Events()) |
| 253 |
assert.ElementsMatch(t, []attribute.KeyValue{ |
| 254 |
semconv.RPCMethodKey.String(tc.name), |
| 255 |
semconv.RPCSystemNameGRPC, |
| 256 |
semconv.RPCResponseStatusCode(codes.OK.String()), |
| 257 |
semconv.ServerAddress(host), |
| 258 |
semconv.ServerPort(port), |
| 259 |
testSpanAttr, |
| 260 |
}, s.Attributes()) |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
func checkClientMetrics(t *testing.T, reader metric.Reader, addr, stabilityOptIn string) { |
| 265 |
rm := metricdata.ResourceMetrics{} |
| 266 |
err := reader.Collect(t.Context(), &rm) |
| 267 |
assert.NoError(t, err) |
| 268 |
require.Len(t, rm.ScopeMetrics, 1) |
| 269 |
|
| 270 |
host, p, err := net.SplitHostPort(addr) |
| 271 |
require.NoError(t, err) |
| 272 |
port, err := strconv.Atoi(p) |
| 273 |
require.NoError(t, err) |
| 274 |
|
| 275 |
var expectedMetrics []metricdata.Metrics |
| 276 |
|
| 277 |
switch stabilityOptIn { |
| 278 |
case "rpc/old": |
| 279 |
expectedMetrics = append(expectedMetrics, metricdata.Metrics{ |
| 280 |
Name: "rpc.client.duration", |
| 281 |
Description: "Measures the duration of outbound RPC.", |
| 282 |
Unit: "ms", |
| 283 |
Data: metricdata.Histogram[float64]{ |
| 284 |
Temporality: metricdata.CumulativeTemporality, |
| 285 |
DataPoints: []metricdata.HistogramDataPoint[float64]{ |
| 286 |
{Attributes: attribute.NewSet(attribute.String("rpc.system", "grpc"), semconv.ServerAddress(host), semconv.ServerPort(port), attribute.String("rpc.service", "grpc.testing.TestService"), attribute.String("rpc.method", "EmptyCall"), semconv.RPCResponseStatusCode(codes.OK.String()), testMetricAttr)}, |
| 287 |
{Attributes: attribute.NewSet(attribute.String("rpc.system", "grpc"), semconv.ServerAddress(host), semconv.ServerPort(port), attribute.String("rpc.service", "grpc.testing.TestService"), attribute.String("rpc.method", "UnaryCall"), semconv.RPCResponseStatusCode(codes.OK.String()), testMetricAttr)}, |
| 288 |
{Attributes: attribute.NewSet(attribute.String("rpc.system", "grpc"), semconv.ServerAddress(host), semconv.ServerPort(port), attribute.String("rpc.service", "grpc.testing.TestService"), attribute.String("rpc.method", "StreamingInputCall"), semconv.RPCResponseStatusCode(codes.OK.String()), testMetricAttr)}, |
| 289 |
{Attributes: attribute.NewSet(attribute.String("rpc.system", "grpc"), semconv.ServerAddress(host), semconv.ServerPort(port), attribute.String("rpc.service", "grpc.testing.TestService"), attribute.String("rpc.method", "StreamingOutputCall"), semconv.RPCResponseStatusCode(codes.OK.String()), testMetricAttr)}, |
| 290 |
{Attributes: attribute.NewSet(attribute.String("rpc.system", "grpc"), semconv.ServerAddress(host), semconv.ServerPort(port), attribute.String("rpc.service", "grpc.testing.TestService"), attribute.String("rpc.method", "FullDuplexCall"), semconv.RPCResponseStatusCode(codes.OK.String()), testMetricAttr)}, |
| 291 |
}, |
| 292 |
}, |
| 293 |
}) |
| 294 |
case "": |
| 295 |
expectedMetrics = append(expectedMetrics, metricdata.Metrics{ |
| 296 |
Name: rpcconv.ClientCallDuration{}.Name(), |
| 297 |
Description: rpcconv.ClientCallDuration{}.Description(), |
| 298 |
Unit: rpcconv.ClientCallDuration{}.Unit(), |
| 299 |
Data: metricdata.Histogram[float64]{ |
| 300 |
Temporality: metricdata.CumulativeTemporality, |
| 301 |
DataPoints: []metricdata.HistogramDataPoint[float64]{ |
| 302 |
{Attributes: attribute.NewSet(semconv.RPCResponseStatusCode(codes.OK.String()), semconv.RPCMethod("grpc.testing.TestService/EmptyCall"), semconv.RPCSystemNameGRPC, semconv.ServerAddress(host), semconv.ServerPort(port), testMetricAttr)}, |
| 303 |
{Attributes: attribute.NewSet(semconv.RPCResponseStatusCode(codes.OK.String()), semconv.RPCMethod("grpc.testing.TestService/UnaryCall"), semconv.RPCSystemNameGRPC, semconv.ServerAddress(host), semconv.ServerPort(port), testMetricAttr)}, |
| 304 |
{Attributes: attribute.NewSet(semconv.RPCResponseStatusCode(codes.OK.String()), semconv.RPCMethod("grpc.testing.TestService/StreamingInputCall"), semconv.RPCSystemNameGRPC, semconv.ServerAddress(host), semconv.ServerPort(port), testMetricAttr)}, |
| 305 |
{Attributes: attribute.NewSet(semconv.RPCResponseStatusCode(codes.OK.String()), semconv.RPCMethod("grpc.testing.TestService/StreamingOutputCall"), semconv.RPCSystemNameGRPC, semconv.ServerAddress(host), semconv.ServerPort(port), testMetricAttr)}, |
| 306 |
{Attributes: attribute.NewSet(semconv.RPCResponseStatusCode(codes.OK.String()), semconv.RPCMethod("grpc.testing.TestService/FullDuplexCall"), semconv.RPCSystemNameGRPC, semconv.ServerAddress(host), semconv.ServerPort(port), testMetricAttr)}, |
| 307 |
}, |
| 308 |
}, |
| 309 |
}) |
| 310 |
case "rpc/dup": |
| 311 |
combinedAttr := func(method string) attribute.Set { |
| 312 |
return attribute.NewSet( |
| 313 |
attribute.String("rpc.system", "grpc"), |
| 314 |
attribute.String("rpc.service", "grpc.testing.TestService"), |
| 315 |
semconv.RPCMethod("grpc.testing.TestService/"+method), |
| 316 |
semconv.RPCResponseStatusCode(codes.OK.String()), |
| 317 |
semconv.RPCSystemNameGRPC, |
| 318 |
semconv.ServerAddress(host), |
| 319 |
semconv.ServerPort(port), |
| 320 |
testMetricAttr, |
| 321 |
) |
| 322 |
} |
| 323 |
expectedMetrics = append(expectedMetrics, metricdata.Metrics{ |
| 324 |
Name: "rpc.client.duration", |
| 325 |
Description: "Measures the duration of outbound RPC.", |
| 326 |
Unit: "ms", |
| 327 |
Data: metricdata.Histogram[float64]{ |
| 328 |
Temporality: metricdata.CumulativeTemporality, |
| 329 |
DataPoints: []metricdata.HistogramDataPoint[float64]{ |
| 330 |
{Attributes: combinedAttr("EmptyCall")}, |
| 331 |
{Attributes: combinedAttr("UnaryCall")}, |
| 332 |
{Attributes: combinedAttr("StreamingInputCall")}, |
| 333 |
{Attributes: combinedAttr("StreamingOutputCall")}, |
| 334 |
{Attributes: combinedAttr("FullDuplexCall")}, |
| 335 |
}, |
| 336 |
}, |
| 337 |
}, metricdata.Metrics{ |
| 338 |
Name: rpcconv.ClientCallDuration{}.Name(), |
| 339 |
Description: rpcconv.ClientCallDuration{}.Description(), |
| 340 |
Unit: rpcconv.ClientCallDuration{}.Unit(), |
| 341 |
Data: metricdata.Histogram[float64]{ |
| 342 |
Temporality: metricdata.CumulativeTemporality, |
| 343 |
DataPoints: []metricdata.HistogramDataPoint[float64]{ |
| 344 |
{Attributes: combinedAttr("EmptyCall")}, |
| 345 |
{Attributes: combinedAttr("UnaryCall")}, |
| 346 |
{Attributes: combinedAttr("StreamingInputCall")}, |
| 347 |
{Attributes: combinedAttr("StreamingOutputCall")}, |
| 348 |
{Attributes: combinedAttr("FullDuplexCall")}, |
| 349 |
}, |
| 350 |
}, |
| 351 |
}) |
| 352 |
} |
| 353 |
|
| 354 |
expectedScopeMetric := metricdata.ScopeMetrics{ |
| 355 |
Scope: instrumentation.Scope{ |
| 356 |
Name: "go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc", |
| 357 |
Version: otelgrpc.Version, |
| 358 |
SchemaURL: semconv.SchemaURL, |
| 359 |
}, |
| 360 |
Metrics: expectedMetrics, |
| 361 |
} |
| 362 |
|
| 363 |
metricdatatest.AssertEqual(t, expectedScopeMetric, rm.ScopeMetrics[0], metricdatatest.IgnoreTimestamp(), metricdatatest.IgnoreValue(), metricdatatest.IgnoreExemplars()) |
| 364 |
} |
| 365 |
|
| 366 |
func checkServerMetrics(t *testing.T, reader metric.Reader, stabilityOptIn string) { |
| 367 |
var rm metricdata.ResourceMetrics |
| 368 |
require.Eventually(t, func() bool { |
| 369 |
rm = metricdata.ResourceMetrics{} |
| 370 |
if err := reader.Collect(t.Context(), &rm); err != nil { |
| 371 |
return false |
| 372 |
} |
| 373 |
if len(rm.ScopeMetrics) == 0 || len(rm.ScopeMetrics[0].Metrics) == 0 { |
| 374 |
return false |
| 375 |
} |
| 376 |
isOld := stabilityOptIn == "rpc/old" || stabilityOptIn == "rpc/dup" |
| 377 |
isNew := stabilityOptIn == "" || stabilityOptIn == "rpc/dup" |
| 378 |
|
| 379 |
var expectedCount int |
| 380 |
if isOld { |
| 381 |
expectedCount++ |
| 382 |
} |
| 383 |
if isNew { |
| 384 |
expectedCount++ |
| 385 |
} |
| 386 |
return len(rm.ScopeMetrics[0].Metrics) == expectedCount |
| 387 |
}, 1*time.Second, 10*time.Millisecond) |
| 388 |
|
| 389 |
require.Len(t, rm.ScopeMetrics, 1) |
| 390 |
|
| 391 |
var expectedMetrics []metricdata.Metrics |
| 392 |
|
| 393 |
switch stabilityOptIn { |
| 394 |
case "rpc/old": |
| 395 |
expectedMetrics = append(expectedMetrics, metricdata.Metrics{ |
| 396 |
Name: "rpc.server.duration", |
| 397 |
Description: "Measures the duration of inbound RPC.", |
| 398 |
Unit: "ms", |
| 399 |
Data: metricdata.Histogram[float64]{ |
| 400 |
Temporality: metricdata.CumulativeTemporality, |
| 401 |
DataPoints: []metricdata.HistogramDataPoint[float64]{ |
| 402 |
{Attributes: attribute.NewSet(attribute.String("rpc.system", "grpc"), attribute.String("rpc.service", "grpc.testing.TestService"), attribute.String("rpc.method", "EmptyCall"), semconv.RPCResponseStatusCode(codes.OK.String()), testMetricAttr)}, |
| 403 |
{Attributes: attribute.NewSet(attribute.String("rpc.system", "grpc"), attribute.String("rpc.service", "grpc.testing.TestService"), attribute.String("rpc.method", "UnaryCall"), semconv.RPCResponseStatusCode(codes.OK.String()), testMetricAttr)}, |
| 404 |
{Attributes: attribute.NewSet(attribute.String("rpc.system", "grpc"), attribute.String("rpc.service", "grpc.testing.TestService"), attribute.String("rpc.method", "StreamingInputCall"), semconv.RPCResponseStatusCode(codes.OK.String()), testMetricAttr)}, |
| 405 |
{Attributes: attribute.NewSet(attribute.String("rpc.system", "grpc"), attribute.String("rpc.service", "grpc.testing.TestService"), attribute.String("rpc.method", "StreamingOutputCall"), semconv.RPCResponseStatusCode(codes.OK.String()), testMetricAttr)}, |
| 406 |
{Attributes: attribute.NewSet(attribute.String("rpc.system", "grpc"), attribute.String("rpc.service", "grpc.testing.TestService"), attribute.String("rpc.method", "FullDuplexCall"), semconv.RPCResponseStatusCode(codes.OK.String()), testMetricAttr)}, |
| 407 |
}, |
| 408 |
}, |
| 409 |
}) |
| 410 |
case "": |
| 411 |
expectedMetrics = append(expectedMetrics, metricdata.Metrics{ |
| 412 |
Name: rpcconv.ServerCallDuration{}.Name(), |
| 413 |
Description: rpcconv.ServerCallDuration{}.Description(), |
| 414 |
Unit: rpcconv.ServerCallDuration{}.Unit(), |
| 415 |
Data: metricdata.Histogram[float64]{ |
| 416 |
Temporality: metricdata.CumulativeTemporality, |
| 417 |
DataPoints: []metricdata.HistogramDataPoint[float64]{ |
| 418 |
{Attributes: attribute.NewSet(semconv.RPCResponseStatusCode(codes.OK.String()), semconv.RPCMethod("grpc.testing.TestService/EmptyCall"), semconv.RPCSystemNameGRPC, testMetricAttr)}, |
| 419 |
{Attributes: attribute.NewSet(semconv.RPCResponseStatusCode(codes.OK.String()), semconv.RPCMethod("grpc.testing.TestService/UnaryCall"), semconv.RPCSystemNameGRPC, testMetricAttr)}, |
| 420 |
{Attributes: attribute.NewSet(semconv.RPCResponseStatusCode(codes.OK.String()), semconv.RPCMethod("grpc.testing.TestService/StreamingInputCall"), semconv.RPCSystemNameGRPC, testMetricAttr)}, |
| 421 |
{Attributes: attribute.NewSet(semconv.RPCResponseStatusCode(codes.OK.String()), semconv.RPCMethod("grpc.testing.TestService/StreamingOutputCall"), semconv.RPCSystemNameGRPC, testMetricAttr)}, |
| 422 |
{Attributes: attribute.NewSet(semconv.RPCResponseStatusCode(codes.OK.String()), semconv.RPCMethod("grpc.testing.TestService/FullDuplexCall"), semconv.RPCSystemNameGRPC, testMetricAttr)}, |
| 423 |
}, |
| 424 |
}, |
| 425 |
}) |
| 426 |
case "rpc/dup": |
| 427 |
combinedAttr := func(method string) attribute.Set { |
| 428 |
return attribute.NewSet( |
| 429 |
attribute.String("rpc.system", "grpc"), |
| 430 |
attribute.String("rpc.service", "grpc.testing.TestService"), |
| 431 |
semconv.RPCMethod("grpc.testing.TestService/"+method), |
| 432 |
semconv.RPCResponseStatusCode(codes.OK.String()), |
| 433 |
semconv.RPCSystemNameGRPC, |
| 434 |
testMetricAttr, |
| 435 |
) |
| 436 |
} |
| 437 |
expectedMetrics = append(expectedMetrics, metricdata.Metrics{ |
| 438 |
Name: "rpc.server.duration", |
| 439 |
Description: "Measures the duration of inbound RPC.", |
| 440 |
Unit: "ms", |
| 441 |
Data: metricdata.Histogram[float64]{ |
| 442 |
Temporality: metricdata.CumulativeTemporality, |
| 443 |
DataPoints: []metricdata.HistogramDataPoint[float64]{ |
| 444 |
{Attributes: combinedAttr("EmptyCall")}, |
| 445 |
{Attributes: combinedAttr("UnaryCall")}, |
| 446 |
{Attributes: combinedAttr("StreamingInputCall")}, |
| 447 |
{Attributes: combinedAttr("StreamingOutputCall")}, |
| 448 |
{Attributes: combinedAttr("FullDuplexCall")}, |
| 449 |
}, |
| 450 |
}, |
| 451 |
}, metricdata.Metrics{ |
| 452 |
Name: rpcconv.ServerCallDuration{}.Name(), |
| 453 |
Description: rpcconv.ServerCallDuration{}.Description(), |
| 454 |
Unit: rpcconv.ServerCallDuration{}.Unit(), |
| 455 |
Data: metricdata.Histogram[float64]{ |
| 456 |
Temporality: metricdata.CumulativeTemporality, |
| 457 |
DataPoints: []metricdata.HistogramDataPoint[float64]{ |
| 458 |
{Attributes: combinedAttr("EmptyCall")}, |
| 459 |
{Attributes: combinedAttr("UnaryCall")}, |
| 460 |
{Attributes: combinedAttr("StreamingInputCall")}, |
| 461 |
{Attributes: combinedAttr("StreamingOutputCall")}, |
| 462 |
{Attributes: combinedAttr("FullDuplexCall")}, |
| 463 |
}, |
| 464 |
}, |
| 465 |
}) |
| 466 |
} |
| 467 |
|
| 468 |
expectedScopeMetric := metricdata.ScopeMetrics{ |
| 469 |
Scope: instrumentation.Scope{ |
| 470 |
Name: "go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc", |
| 471 |
Version: otelgrpc.Version, |
| 472 |
SchemaURL: semconv.SchemaURL, |
| 473 |
}, |
| 474 |
Metrics: expectedMetrics, |
| 475 |
} |
| 476 |
|
| 477 |
metricdatatest.AssertEqual(t, expectedScopeMetric, rm.ScopeMetrics[0], metricdatatest.IgnoreTimestamp(), metricdatatest.IgnoreValue(), metricdatatest.IgnoreExemplars()) |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
func TestStatsHandlerConcurrentSafeContextCancellation(t *testing.T) { |
| 483 |
listener, err := (&net.ListenConfig{}).Listen(t.Context(), "tcp", "127.0.0.1:0") |
| 484 |
require.NoError(t, err, "failed to open port") |
| 485 |
client := newGrpcTest(t, listener, |
| 486 |
[]grpc.DialOption{ |
| 487 |
grpc.WithStatsHandler(otelgrpc.NewClientHandler()), |
| 488 |
}, |
| 489 |
[]grpc.ServerOption{ |
| 490 |
grpc.StatsHandler(otelgrpc.NewServerHandler()), |
| 491 |
}, |
| 492 |
) |
| 493 |
|
| 494 |
const n = 10 |
| 495 |
for range n { |
| 496 |
ctx, cancel := context.WithCancel(t.Context()) |
| 497 |
stream, err := client.FullDuplexCall(ctx) |
| 498 |
require.NoError(t, err) |
| 499 |
|
| 500 |
const messageCount = 10 |
| 501 |
var wg sync.WaitGroup |
| 502 |
|
| 503 |
wg.Go(func() { |
| 504 |
for range messageCount { |
| 505 |
const reqSize = 1 |
| 506 |
pl := test.ClientNewPayload(testpb.PayloadType_COMPRESSABLE, reqSize) |
| 507 |
respParam := []*testpb.ResponseParameters{ |
| 508 |
{ |
| 509 |
Size: reqSize, |
| 510 |
}, |
| 511 |
} |
| 512 |
req := &testpb.StreamingOutputCallRequest{ |
| 513 |
ResponseType: testpb.PayloadType_COMPRESSABLE, |
| 514 |
ResponseParameters: respParam, |
| 515 |
Payload: pl, |
| 516 |
} |
| 517 |
err := stream.Send(req) |
| 518 |
if errors.Is(err, io.EOF) || status.Code(err) == codes.Canceled { |
| 519 |
assert.ErrorIs(t, ctx.Err(), context.Canceled) |
| 520 |
} else { |
| 521 |
assert.NoError(t, err) |
| 522 |
} |
| 523 |
} |
| 524 |
assert.NoError(t, stream.CloseSend()) |
| 525 |
}) |
| 526 |
|
| 527 |
wg.Go(func() { |
| 528 |
for i := range messageCount { |
| 529 |
_, err := stream.Recv() |
| 530 |
if i > messageCount/2 { |
| 531 |
cancel() |
| 532 |
} |
| 533 |
|
| 534 |
if status.Code(err) == codes.Canceled { |
| 535 |
return |
| 536 |
} |
| 537 |
assert.NoError(t, err) |
| 538 |
} |
| 539 |
}) |
| 540 |
|
| 541 |
wg.Wait() |
| 542 |
} |
| 543 |
} |
| 544 |
|
| 545 |
func TestServerHandlerTagRPC(t *testing.T) { |
| 546 |
tests := []struct { |
| 547 |
name string |
| 548 |
server stats.Handler |
| 549 |
ctx context.Context |
| 550 |
info *stats.RPCTagInfo |
| 551 |
exp bool |
| 552 |
}{ |
| 553 |
{ |
| 554 |
name: "start a span without filters", |
| 555 |
server: otelgrpc.NewServerHandler(otelgrpc.WithTracerProvider(trace.NewTracerProvider())), |
| 556 |
ctx: t.Context(), |
| 557 |
info: &stats.RPCTagInfo{ |
| 558 |
FullMethodName: "/grpc.health.v1.Health/Check", |
| 559 |
}, |
| 560 |
exp: true, |
| 561 |
}, |
| 562 |
{ |
| 563 |
name: "don't start a span with filter and match", |
| 564 |
server: otelgrpc.NewServerHandler(otelgrpc.WithTracerProvider(trace.NewTracerProvider()), otelgrpc.WithFilter(func(ri *stats.RPCTagInfo) bool { |
| 565 |
return ri.FullMethodName != "/grpc.health.v1.Health/Check" |
| 566 |
})), |
| 567 |
ctx: t.Context(), |
| 568 |
info: &stats.RPCTagInfo{ |
| 569 |
FullMethodName: "/grpc.health.v1.Health/Check", |
| 570 |
}, |
| 571 |
exp: false, |
| 572 |
}, |
| 573 |
{ |
| 574 |
name: "start a span with filter and no match", |
| 575 |
server: otelgrpc.NewServerHandler(otelgrpc.WithTracerProvider(trace.NewTracerProvider()), otelgrpc.WithFilter(func(ri *stats.RPCTagInfo) bool { |
| 576 |
return ri.FullMethodName != "/grpc.health.v1.Health/Check" |
| 577 |
})), |
| 578 |
ctx: t.Context(), |
| 579 |
info: &stats.RPCTagInfo{ |
| 580 |
FullMethodName: "/app.v1.Service/Get", |
| 581 |
}, |
| 582 |
exp: true, |
| 583 |
}, |
| 584 |
} |
| 585 |
|
| 586 |
for _, tt := range tests { |
| 587 |
t.Run(t.Name(), func(t *testing.T) { |
| 588 |
ctx := tt.server.TagRPC(tt.ctx, tt.info) |
| 589 |
|
| 590 |
got := oteltrace.SpanFromContext(ctx).IsRecording() |
| 591 |
|
| 592 |
if tt.exp != got { |
| 593 |
t.Errorf("expected %t, got %t", tt.exp, got) |
| 594 |
} |
| 595 |
}) |
| 596 |
} |
| 597 |
} |
| 598 |
|
| 599 |
func TestClientHandlerTagRPC(t *testing.T) { |
| 600 |
tests := []struct { |
| 601 |
name string |
| 602 |
client stats.Handler |
| 603 |
ctx context.Context |
| 604 |
info *stats.RPCTagInfo |
| 605 |
exp bool |
| 606 |
}{ |
| 607 |
{ |
| 608 |
name: "start a span without filters", |
| 609 |
client: otelgrpc.NewClientHandler(otelgrpc.WithTracerProvider(trace.NewTracerProvider())), |
| 610 |
ctx: t.Context(), |
| 611 |
info: &stats.RPCTagInfo{ |
| 612 |
FullMethodName: "/grpc.health.v1.Health/Check", |
| 613 |
}, |
| 614 |
exp: true, |
| 615 |
}, |
| 616 |
{ |
| 617 |
name: "don't start a span with filter and match", |
| 618 |
client: otelgrpc.NewClientHandler(otelgrpc.WithTracerProvider(trace.NewTracerProvider()), otelgrpc.WithFilter(func(ri *stats.RPCTagInfo) bool { |
| 619 |
return ri.FullMethodName != "/grpc.health.v1.Health/Check" |
| 620 |
})), |
| 621 |
ctx: t.Context(), |
| 622 |
info: &stats.RPCTagInfo{ |
| 623 |
FullMethodName: "/grpc.health.v1.Health/Check", |
| 624 |
}, |
| 625 |
exp: false, |
| 626 |
}, |
| 627 |
{ |
| 628 |
name: "start a span with filter and no match", |
| 629 |
client: otelgrpc.NewClientHandler(otelgrpc.WithTracerProvider(trace.NewTracerProvider()), otelgrpc.WithFilter(func(ri *stats.RPCTagInfo) bool { |
| 630 |
return ri.FullMethodName != "/grpc.health.v1.Health/Check" |
| 631 |
})), |
| 632 |
ctx: t.Context(), |
| 633 |
info: &stats.RPCTagInfo{ |
| 634 |
FullMethodName: "/app.v1.Service/Get", |
| 635 |
}, |
| 636 |
exp: true, |
| 637 |
}, |
| 638 |
} |
| 639 |
|
| 640 |
for _, tt := range tests { |
| 641 |
t.Run(t.Name(), func(t *testing.T) { |
| 642 |
ctx := tt.client.TagRPC(tt.ctx, tt.info) |
| 643 |
|
| 644 |
got := oteltrace.SpanFromContext(ctx).IsRecording() |
| 645 |
|
| 646 |
if tt.exp != got { |
| 647 |
t.Errorf("expected %t, got %t", tt.exp, got) |
| 648 |
} |
| 649 |
}) |
| 650 |
} |
| 651 |
} |
| 652 |
|
| 653 |
func TestSpansSemconvOptIn(t *testing.T) { |
| 654 |
tests := []struct { |
| 655 |
name string |
| 656 |
stabilityOptIn string |
| 657 |
wantAttrs []attribute.KeyValue |
| 658 |
notWantAttrs []string |
| 659 |
}{ |
| 660 |
{ |
| 661 |
name: "default", |
| 662 |
stabilityOptIn: "", |
| 663 |
wantAttrs: []attribute.KeyValue{ |
| 664 |
attribute.String("rpc.system.name", "grpc"), |
| 665 |
attribute.String("rpc.method", "pkg.Service/Method"), |
| 666 |
}, |
| 667 |
notWantAttrs: []string{"rpc.service"}, |
| 668 |
}, |
| 669 |
|
| 670 |
{ |
| 671 |
name: "rpc_old", |
| 672 |
stabilityOptIn: "rpc/old", |
| 673 |
wantAttrs: []attribute.KeyValue{ |
| 674 |
attribute.String("rpc.system", "grpc"), |
| 675 |
attribute.String("rpc.service", "pkg.Service"), |
| 676 |
attribute.String("rpc.method", "Method"), |
| 677 |
}, |
| 678 |
notWantAttrs: []string{"rpc.system.name"}, |
| 679 |
}, |
| 680 |
{ |
| 681 |
name: "rpc_dup", |
| 682 |
stabilityOptIn: "rpc/dup", |
| 683 |
wantAttrs: []attribute.KeyValue{ |
| 684 |
attribute.String("rpc.system", "grpc"), |
| 685 |
attribute.String("rpc.service", "pkg.Service"), |
| 686 |
attribute.String("rpc.method", "pkg.Service/Method"), |
| 687 |
}, |
| 688 |
}, |
| 689 |
} |
| 690 |
|
| 691 |
for _, tt := range tests { |
| 692 |
t.Run(tt.name, func(t *testing.T) { |
| 693 |
if tt.stabilityOptIn != "" { |
| 694 |
t.Setenv("OTEL_SEMCONV_STABILITY_OPT_IN", tt.stabilityOptIn) |
| 695 |
} |
| 696 |
|
| 697 |
sr := tracetest.NewSpanRecorder() |
| 698 |
tp := trace.NewTracerProvider(trace.WithSpanProcessor(sr)) |
| 699 |
|
| 700 |
h := otelgrpc.NewServerHandler(otelgrpc.WithTracerProvider(tp)) |
| 701 |
|
| 702 |
ctx := t.Context() |
| 703 |
info := &stats.RPCTagInfo{ |
| 704 |
FullMethodName: "/pkg.Service/Method", |
| 705 |
} |
| 706 |
|
| 707 |
ctx = h.TagRPC(ctx, info) |
| 708 |
|
| 709 |
span := oteltrace.SpanFromContext(ctx) |
| 710 |
require.True(t, span.IsRecording()) |
| 711 |
|
| 712 |
span.End() |
| 713 |
|
| 714 |
spans := sr.Ended() |
| 715 |
require.Len(t, spans, 1) |
| 716 |
|
| 717 |
attrs := spans[0].Attributes() |
| 718 |
for _, want := range tt.wantAttrs { |
| 719 |
assert.Contains(t, attrs, want) |
| 720 |
} |
| 721 |
|
| 722 |
for _, key := range tt.notWantAttrs { |
| 723 |
found := false |
| 724 |
for _, a := range attrs { |
| 725 |
if string(a.Key) == key { |
| 726 |
found = true |
| 727 |
break |
| 728 |
} |
| 729 |
} |
| 730 |
assert.False(t, found, "should not contain attribute %s", key) |
| 731 |
} |
| 732 |
}) |
| 733 |
} |
| 734 |
} |
| 735 |
|
| 736 |
func TestMetricsSemconvOptIn(t *testing.T) { |
| 737 |
tests := []struct { |
| 738 |
name string |
| 739 |
stabilityOptIn string |
| 740 |
}{ |
| 741 |
{ |
| 742 |
name: "default", |
| 743 |
stabilityOptIn: "", |
| 744 |
}, |
| 745 |
{ |
| 746 |
name: "rpc_old", |
| 747 |
stabilityOptIn: "rpc/old", |
| 748 |
}, |
| 749 |
{ |
| 750 |
name: "rpc_dup", |
| 751 |
stabilityOptIn: "rpc/dup", |
| 752 |
}, |
| 753 |
} |
| 754 |
|
| 755 |
for _, tt := range tests { |
| 756 |
t.Run(tt.name, func(t *testing.T) { |
| 757 |
if tt.stabilityOptIn != "" { |
| 758 |
t.Setenv("OTEL_SEMCONV_STABILITY_OPT_IN", tt.stabilityOptIn) |
| 759 |
} else { |
| 760 |
t.Setenv("OTEL_SEMCONV_STABILITY_OPT_IN", "") |
| 761 |
} |
| 762 |
|
| 763 |
clientMetricReader := metric.NewManualReader() |
| 764 |
clientMP := metric.NewMeterProvider(metric.WithReader(clientMetricReader)) |
| 765 |
|
| 766 |
serverMetricReader := metric.NewManualReader() |
| 767 |
serverMP := metric.NewMeterProvider(metric.WithReader(serverMetricReader)) |
| 768 |
|
| 769 |
listener, err := (&net.ListenConfig{}).Listen(t.Context(), "tcp", "127.0.0.1:0") |
| 770 |
require.NoError(t, err, "failed to open port") |
| 771 |
|
| 772 |
client := newGrpcTest(t, listener, |
| 773 |
[]grpc.DialOption{ |
| 774 |
grpc.WithStatsHandler(otelgrpc.NewClientHandler( |
| 775 |
otelgrpc.WithMeterProvider(clientMP), |
| 776 |
otelgrpc.WithMetricAttributes(testMetricAttr)), |
| 777 |
), |
| 778 |
}, |
| 779 |
[]grpc.ServerOption{ |
| 780 |
grpc.StatsHandler(otelgrpc.NewServerHandler( |
| 781 |
otelgrpc.WithTracerProvider(trace.NewTracerProvider()), |
| 782 |
otelgrpc.WithMeterProvider(serverMP), |
| 783 |
otelgrpc.WithMetricAttributes(testMetricAttr)), |
| 784 |
), |
| 785 |
}, |
| 786 |
) |
| 787 |
ctx, cancel := context.WithCancel(t.Context()) |
| 788 |
defer cancel() |
| 789 |
doCalls(ctx, client) |
| 790 |
|
| 791 |
t.Run("ClientMetrics", func(t *testing.T) { |
| 792 |
checkClientMetrics(t, clientMetricReader, listener.Addr().String(), tt.stabilityOptIn) |
| 793 |
}) |
| 794 |
|
| 795 |
t.Run("ServerMetrics", func(t *testing.T) { |
| 796 |
checkServerMetrics(t, serverMetricReader, tt.stabilityOptIn) |
| 797 |
}) |
| 798 |
}) |
| 799 |
} |
| 800 |
} |