Skip to content

Commit

Permalink
update TestServerWithMetricsAndTraceOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
aranjans committed Dec 17, 2024
1 parent 186ad22 commit 907a399
Showing 1 changed file with 6 additions and 247 deletions.
253 changes: 6 additions & 247 deletions stats/opentelemetry/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,25 +622,16 @@ func pollForWantMetrics(ctx context.Context, t *testing.T, reader *metric.Manual
return fmt.Errorf("error waiting for metrics %v: %v", wantMetrics, ctx.Err())
}

// TestServerWithMetricsAndTraceOptions tests emitted metrics and traces from
// OpenTelemetry instrumentation component. It then configures a system with a gRPC
// Client and gRPC server with the OpenTelemetry Dial and Server Option configured
// specifying all the metrics and traces provided by this package, and makes a Unary
// RPC and a Streaming RPC.
//
// Metrics:
// - Verifies that certain metrics are recorded for each registered metric
// observed through a Manual Metrics Reader on the provided OpenTelemetry SDK's
// Meter Provider.
//
// Traces:
// - Confirms that tracing information is correctly recorded, including span creation,
// context propagation, and any relevant attributes associated with the RPC calls.
// TestServerWithMetricsAndTraceOptions verifies the integration of metrics and traces
// emitted by the OpenTelemetry instrumentation in a gRPC environment. It tests the
// correct emission of metrics during a Unary RPC and a Streaming RPC, ensuring that
// the metrics reflect the operations performed, including the size of the compressed
// message.
func TestServerWithMetricsAndTraceOptions(t *testing.T) {
// Create default metrics options
mo, reader := defaultMetricsOptions(t, nil)
// Create default trace options
to, exporter := defaultTraceOptions(t)
to, _ := defaultTraceOptions(t)

ss := setupStubServer(t, mo, to)
defer ss.Stop()
Expand Down Expand Up @@ -681,238 +672,6 @@ func TestServerWithMetricsAndTraceOptions(t *testing.T) {
UnaryCompressedMessageSize: float64(57),
})
testutils.CompareMetrics(ctx, t, reader, gotMetrics, wantMetrics)

wantSI := []traceSpanInfo{
{
name: "grpc.testing.TestService.UnaryCall",
spanKind: trace2.SpanKindServer.String(),
attributes: []attribute.KeyValue{
{
Key: "Client",
Value: attribute.IntValue(0),
},
{
Key: "FailFast",
Value: attribute.IntValue(0),
},
{
Key: "previous-rpc-attempts",
Value: attribute.IntValue(0),
},
{
Key: "transparent-retry",
Value: attribute.IntValue(0),
},
},
events: []trace.Event{
{
Name: "Inbound compressed message",
Attributes: []attribute.KeyValue{
{
Key: "sequence-number",
Value: attribute.IntValue(1),
},
{
Key: "message-size",
Value: attribute.IntValue(10006),
},
{
Key: "message-size-compressed",
Value: attribute.IntValue(57),
},
},
},
{
Name: "Outbound compressed message",
Attributes: []attribute.KeyValue{
{
Key: "sequence-number",
Value: attribute.IntValue(1),
},
{
Key: "message-size",
Value: attribute.IntValue(10006),
},
{
Key: "message-size-compressed",
Value: attribute.IntValue(57),
},
},
},
},
},
{
name: "Attempt.grpc.testing.TestService.UnaryCall",
spanKind: trace2.SpanKindInternal.String(),
attributes: []attribute.KeyValue{
{
Key: "Client",
Value: attribute.IntValue(1),
},
{
Key: "FailFast",
Value: attribute.IntValue(1),
},
{
Key: "previous-rpc-attempts",
Value: attribute.IntValue(0),
},
{
Key: "transparent-retry",
Value: attribute.IntValue(0),
},
},
events: []trace.Event{
{
Name: "Outbound compressed message",
Attributes: []attribute.KeyValue{
{
Key: "sequence-number",
Value: attribute.IntValue(1),
},
{
Key: "message-size",
Value: attribute.IntValue(10006),
},
{
Key: "message-size-compressed",
Value: attribute.IntValue(57),
},
},
},
{
Name: "Inbound compressed message",
Attributes: []attribute.KeyValue{
{
Key: "sequence-number",
Value: attribute.IntValue(1),
},
{
Key: "message-size",
Value: attribute.IntValue(10006),
},
{
Key: "message-size-compressed",
Value: attribute.IntValue(57),
},
},
},
},
},
{
name: "grpc.testing.TestService.UnaryCall",
spanKind: trace2.SpanKindClient.String(),
attributes: []attribute.KeyValue{},
events: []trace.Event{},
},
{
name: "grpc.testing.TestService.FullDuplexCall",
spanKind: trace2.SpanKindServer.String(),
attributes: []attribute.KeyValue{
{
Key: "Client",
Value: attribute.IntValue(0),
},
{
Key: "FailFast",
Value: attribute.IntValue(0),
},
{
Key: "previous-rpc-attempts",
Value: attribute.IntValue(0),
},
{
Key: "transparent-retry",
Value: attribute.IntValue(0),
},
},
events: []trace.Event{},
},
{
name: "grpc.testing.TestService.FullDuplexCall",
spanKind: trace2.SpanKindClient.String(),
attributes: []attribute.KeyValue{},
events: []trace.Event{},
},
{
name: "Attempt.grpc.testing.TestService.FullDuplexCall",
spanKind: trace2.SpanKindInternal.String(),
attributes: []attribute.KeyValue{
{
Key: "Client",
Value: attribute.IntValue(1),
},
{
Key: "FailFast",
Value: attribute.IntValue(1),
},
{
Key: "previous-rpc-attempts",
Value: attribute.IntValue(0),
},
{
Key: "transparent-retry",
Value: attribute.IntValue(0),
},
},
events: []trace.Event{},
},
}

// Verify traces
spans := exporter.GetSpans()
if got, want := len(spans), 6; got != want {
t.Fatalf("got %d spans, want %d", got, want)
}

// Add assertions for specific span attributes and events as needed.
// For example, to check if the server span has the correct status:
serverSpan := spans[0]
if got, want := serverSpan.Status.Code, otelcodes.Ok; got != want {
t.Errorf("got status code %v, want %v", got, want)
}

for index, span := range spans {
// Check that the attempt span has the correct status
if got, want := spans[index].Status.Code, otelcodes.Ok; got != want {
t.Errorf("Got status code %v, want %v", got, want)
}
// name
if got, want := span.Name, wantSI[index].name; got != want {
t.Errorf("Span name is %q, want %q", got, want)
}
// spanKind
if got, want := span.SpanKind.String(), wantSI[index].spanKind; got != want {
t.Errorf("Got span kind %q, want %q", got, want)
}
// attributes
if got, want := len(span.Attributes), len(wantSI[index].attributes); got != want {
t.Errorf("Got attributes list of size %q, want %q", got, want)
}
for idx, att := range span.Attributes {
if got, want := att.Key, wantSI[index].attributes[idx].Key; got != want {
t.Errorf("Got attribute key for span name %v as %v, want %v", span.Name, got, want)
}
}
// events
if got, want := len(span.Events), len(wantSI[index].events); got != want {
t.Errorf("Event length is %q, want %q", got, want)
}
for eventIdx, event := range span.Events {
if got, want := event.Name, wantSI[index].events[eventIdx].Name; got != want {
t.Errorf("Got event name for span name %q as %q, want %q", span.Name, got, want)
}
for idx, att := range event.Attributes {
if got, want := att.Key, wantSI[index].events[eventIdx].Attributes[idx].Key; got != want {
t.Errorf("Got attribute key for span name %q with event name %v, as %v, want %v", span.Name, event.Name, got, want)
}
if got, want := att.Value, wantSI[index].events[eventIdx].Attributes[idx].Value; got != want {
t.Errorf("Got attribute value for span name %v with event name %v, as %v, want %v", span.Name, event.Name, got, want)
}
}
}
}

}

// TestSpan verifies that the gRPC Trace Binary propagator correctly
Expand Down

0 comments on commit 907a399

Please sign in to comment.