Skip to content

Commit 23553fe

Browse files
committed
Add client-side tests for Labeler and MetricAttributesFn
Signed-off-by: Michal Jurecko <michal.jurecko@gmail.com>
1 parent cc4b1d9 commit 23553fe

1 file changed

Lines changed: 72 additions & 0 deletions

File tree

interceptor_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2857,3 +2857,75 @@ func TestLabelerAndMetricAttributesFnCombined(t *testing.T) {
28572857
"span should not contain MetricAttributesFn attributes")
28582858
}
28592859
}
2860+
2861+
func TestLabelerUnaryClient(t *testing.T) {
2862+
t.Parallel()
2863+
metricReader, meterProvider := setupMetrics()
2864+
spanRecorder := tracetest.NewSpanRecorder()
2865+
traceProvider := trace.NewTracerProvider(trace.WithSpanProcessor(spanRecorder))
2866+
customAttrs := []attribute.KeyValue{
2867+
attribute.String("custom.label", "client-value"),
2868+
}
2869+
interceptor, err := NewInterceptor(
2870+
WithMeterProvider(meterProvider),
2871+
WithTracerProvider(traceProvider),
2872+
)
2873+
require.NoError(t, err)
2874+
client, _, _ := startServer(t,
2875+
nil,
2876+
[]connect.ClientOption{
2877+
connect.WithInterceptors(interceptor, labelerInterceptor{attrs: customAttrs}),
2878+
},
2879+
okayPingServer(),
2880+
)
2881+
_, err = client.Ping(context.Background(), requestOfSize(1, 12))
2882+
require.NoError(t, err)
2883+
assertMetrics(t, metricReader, expectedMetrics{
2884+
ClientDuration: true,
2885+
RequiredAttrs: map[string]attribute.Value{
2886+
"custom.label": attribute.StringValue("client-value"),
2887+
},
2888+
})
2889+
require.Len(t, spanRecorder.Ended(), 1)
2890+
for _, attr := range spanRecorder.Ended()[0].Attributes() {
2891+
assert.NotEqual(t, attribute.Key("custom.label"), attr.Key,
2892+
"span should not contain labeler attributes")
2893+
}
2894+
}
2895+
2896+
func TestMetricAttributesFnUnaryClient(t *testing.T) {
2897+
t.Parallel()
2898+
metricReader, meterProvider := setupMetrics()
2899+
spanRecorder := tracetest.NewSpanRecorder()
2900+
traceProvider := trace.NewTracerProvider(trace.WithSpanProcessor(spanRecorder))
2901+
interceptor, err := NewInterceptor(
2902+
WithMeterProvider(meterProvider),
2903+
WithTracerProvider(traceProvider),
2904+
WithMetricAttributesFn(func(_ context.Context, spec connect.Spec) []attribute.KeyValue {
2905+
return []attribute.KeyValue{
2906+
attribute.String("fn.procedure", spec.Procedure),
2907+
}
2908+
}),
2909+
)
2910+
require.NoError(t, err)
2911+
client, _, _ := startServer(t,
2912+
nil,
2913+
[]connect.ClientOption{
2914+
connect.WithInterceptors(interceptor),
2915+
},
2916+
okayPingServer(),
2917+
)
2918+
_, err = client.Ping(context.Background(), requestOfSize(1, 12))
2919+
require.NoError(t, err)
2920+
assertMetrics(t, metricReader, expectedMetrics{
2921+
ClientDuration: true,
2922+
RequiredAttrs: map[string]attribute.Value{
2923+
"fn.procedure": attribute.StringValue(pingv1connect.PingServicePingProcedure),
2924+
},
2925+
})
2926+
require.Len(t, spanRecorder.Ended(), 1)
2927+
for _, attr := range spanRecorder.Ended()[0].Attributes() {
2928+
assert.NotEqual(t, attribute.Key("fn.procedure"), attr.Key,
2929+
"span should not contain MetricAttributesFn attributes")
2930+
}
2931+
}

0 commit comments

Comments
 (0)