Skip to content

Commit 0a746dc

Browse files
etilitedmathieu
andauthored
chore: remove unnecessary heap-allocation in otelgrpc (open-telemetry#8074)
Co-authored-by: Damien Mathieu <[email protected]>
1 parent b5b8766 commit 0a746dc

File tree

4 files changed

+76
-7
lines changed

4 files changed

+76
-7
lines changed

instrumentation/google.golang.org/grpc/otelgrpc/metadata_supplier.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,21 @@ type metadataSupplier struct {
1515
}
1616

1717
// assert that metadataSupplier implements the TextMapCarrier interface.
18-
var _ propagation.TextMapCarrier = &metadataSupplier{}
18+
var _ propagation.TextMapCarrier = metadataSupplier{}
1919

20-
func (s *metadataSupplier) Get(key string) string {
20+
func (s metadataSupplier) Get(key string) string {
2121
values := s.metadata.Get(key)
2222
if len(values) == 0 {
2323
return ""
2424
}
2525
return values[0]
2626
}
2727

28-
func (s *metadataSupplier) Set(key, value string) {
28+
func (s metadataSupplier) Set(key, value string) {
2929
s.metadata.Set(key, value)
3030
}
3131

32-
func (s *metadataSupplier) Keys() []string {
32+
func (s metadataSupplier) Keys() []string {
3333
out := make([]string, 0, len(s.metadata))
3434
for key := range s.metadata {
3535
out = append(out, key)
@@ -42,7 +42,7 @@ func inject(ctx context.Context, propagators propagation.TextMapPropagator) cont
4242
if !ok {
4343
md = metadata.MD{}
4444
}
45-
propagators.Inject(ctx, &metadataSupplier{
45+
propagators.Inject(ctx, metadataSupplier{
4646
metadata: md,
4747
})
4848
return metadata.NewOutgoingContext(ctx, md)
@@ -54,7 +54,7 @@ func extract(ctx context.Context, propagators propagation.TextMapPropagator) con
5454
md = metadata.MD{}
5555
}
5656

57-
return propagators.Extract(ctx, &metadataSupplier{
57+
return propagators.Extract(ctx, metadataSupplier{
5858
metadata: md,
5959
})
6060
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package otelgrpc
5+
6+
import (
7+
"testing"
8+
9+
"go.opentelemetry.io/otel"
10+
)
11+
12+
func BenchmarkMetadataSupplier(b *testing.B) {
13+
ctx := b.Context()
14+
propagator := otel.GetTextMapPropagator()
15+
16+
b.Run("extract", func(b *testing.B) {
17+
b.ReportAllocs()
18+
for b.Loop() {
19+
_ = extract(ctx, propagator)
20+
}
21+
})
22+
23+
b.Run("inject", func(b *testing.B) {
24+
b.ReportAllocs()
25+
for b.Loop() {
26+
_ = inject(ctx, propagator)
27+
}
28+
})
29+
}

instrumentation/google.golang.org/grpc/otelgrpc/metadata_supplier_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func TestMetadataSupplier(t *testing.T) {
1414
md := metadata.New(map[string]string{
1515
"k1": "v1",
1616
})
17-
ms := &metadataSupplier{md}
17+
ms := metadataSupplier{md}
1818

1919
v1 := ms.Get("k1")
2020
assert.Equal(t, "v1", v1)
@@ -25,4 +25,8 @@ func TestMetadataSupplier(t *testing.T) {
2525
v2 := ms.Get("k2")
2626
assert.Equal(t, "v1", v1)
2727
assert.Equal(t, "v2", v2)
28+
29+
wantKeys := []string{"k1", "k2"}
30+
keys := ms.Keys()
31+
assert.ElementsMatch(t, wantKeys, keys)
2832
}

instrumentation/google.golang.org/grpc/otelgrpc/stats_handler_bench_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,39 @@ func BenchmarkServerHandler_HandleRPC_NoOp_End(b *testing.B) {
141141
func BenchmarkServerHandler_HandleRPC_NoOp_Nil(b *testing.B) {
142142
benchmarkServerHandlerHandleRPCNoOp(b, nil)
143143
}
144+
145+
func BenchmarkServerHandler_TagRPCNoOp(b *testing.B) {
146+
handler := NewServerHandler(
147+
WithTracerProvider(tracenoop.NewTracerProvider()),
148+
WithMeterProvider(metricnoop.NewMeterProvider()),
149+
WithMessageEvents(ReceivedEvents, SentEvents),
150+
)
151+
ctx := b.Context()
152+
153+
tagInfo := &stats.RPCTagInfo{
154+
FullMethodName: "/package.service/method",
155+
}
156+
157+
b.ReportAllocs()
158+
for b.Loop() {
159+
_ = handler.TagRPC(ctx, tagInfo)
160+
}
161+
}
162+
163+
func BenchmarkClientHandler_TagRPCNoOp(b *testing.B) {
164+
handler := NewClientHandler(
165+
WithTracerProvider(tracenoop.NewTracerProvider()),
166+
WithMeterProvider(metricnoop.NewMeterProvider()),
167+
WithMessageEvents(ReceivedEvents, SentEvents),
168+
)
169+
ctx := b.Context()
170+
171+
tagInfo := &stats.RPCTagInfo{
172+
FullMethodName: "/package.service/method",
173+
}
174+
175+
b.ReportAllocs()
176+
for b.Loop() {
177+
_ = handler.TagRPC(ctx, tagInfo)
178+
}
179+
}

0 commit comments

Comments
 (0)