Skip to content

Commit

Permalink
Remove expensive calculation of gRPC message size
Browse files Browse the repository at this point in the history
Signed-off-by: Anthony J Mirabella <a9@aneurysm9.com>
  • Loading branch information
Aneurysm9 committed Jan 18, 2023
1 parent 4b49f06 commit c9c9290
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 95 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- `otelmux`: Add new `WithSpanNameFormatter` option to `go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux` to allow customizing span names. (#3041)
- Improve documentation for `samplers/jaegerremote` by providing examples of sampling endpoints. (#3147)

### Changed

- `otelgrpc`: Remove expensive calculation of uncompressed message size attribute. (#TBD)

## [1.12.0/0.37.0/0.6.0]

### Added
Expand Down
20 changes: 6 additions & 14 deletions instrumentation/google.golang.org/grpc/otelgrpc/interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,16 @@ import (
type messageType attribute.KeyValue

// Event adds an event of the messageType to the span associated with the
// passed context with id and size (if message is a proto message).
func (m messageType) Event(ctx context.Context, id int, message interface{}) {
// passed context with a message id.
func (m messageType) Event(ctx context.Context, id int, _ interface{}) {
span := trace.SpanFromContext(ctx)
if !span.IsRecording() {
return
}
if p, ok := message.(proto.Message); ok {
span.AddEvent("message", trace.WithAttributes(
attribute.KeyValue(m),
RPCMessageIDKey.Int(id),
RPCMessageUncompressedSizeKey.Int(proto.Size(p)),
))
} else {
span.AddEvent("message", trace.WithAttributes(
attribute.KeyValue(m),
RPCMessageIDKey.Int(id),
))
}
span.AddEvent("message", trace.WithAttributes(
attribute.KeyValue(m),
RPCMessageIDKey.Int(id),
))
}

var (
Expand Down
43 changes: 0 additions & 43 deletions instrumentation/google.golang.org/grpc/otelgrpc/test/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,13 @@ func checkUnaryClientSpans(t *testing.T, spans []trace.ReadOnlySpan) {
Attributes: []attribute.KeyValue{
otelgrpc.RPCMessageIDKey.Int(1),
otelgrpc.RPCMessageTypeKey.String("SENT"),
otelgrpc.RPCMessageUncompressedSizeKey.Int(0),
},
},
{
Name: "message",
Attributes: []attribute.KeyValue{
otelgrpc.RPCMessageIDKey.Int(1),
otelgrpc.RPCMessageTypeKey.String("RECEIVED"),
otelgrpc.RPCMessageUncompressedSizeKey.Int(0),
},
},
}, emptySpan.Events())
Expand All @@ -162,7 +160,6 @@ func checkUnaryClientSpans(t *testing.T, spans []trace.ReadOnlySpan) {
otelgrpc.RPCMessageIDKey.Int(1),
otelgrpc.RPCMessageTypeKey.String("SENT"),
// largeReqSize from "google.golang.org/grpc/interop" + 12 (overhead).
otelgrpc.RPCMessageUncompressedSizeKey.Int(271840),
},
},
{
Expand All @@ -171,7 +168,6 @@ func checkUnaryClientSpans(t *testing.T, spans []trace.ReadOnlySpan) {
otelgrpc.RPCMessageIDKey.Int(1),
otelgrpc.RPCMessageTypeKey.String("RECEIVED"),
// largeRespSize from "google.golang.org/grpc/interop" + 8 (overhead).
otelgrpc.RPCMessageUncompressedSizeKey.Int(314167),
},
},
}, largeSpan.Events())
Expand All @@ -196,31 +192,27 @@ func checkStreamClientSpans(t *testing.T, spans []trace.ReadOnlySpan) {
Attributes: []attribute.KeyValue{
otelgrpc.RPCMessageIDKey.Int(1),
otelgrpc.RPCMessageTypeKey.String("SENT"),
otelgrpc.RPCMessageUncompressedSizeKey.Int(27190),
},
},
{
Name: "message",
Attributes: []attribute.KeyValue{
otelgrpc.RPCMessageIDKey.Int(2),
otelgrpc.RPCMessageTypeKey.String("SENT"),
otelgrpc.RPCMessageUncompressedSizeKey.Int(12),
},
},
{
Name: "message",
Attributes: []attribute.KeyValue{
otelgrpc.RPCMessageIDKey.Int(3),
otelgrpc.RPCMessageTypeKey.String("SENT"),
otelgrpc.RPCMessageUncompressedSizeKey.Int(1834),
},
},
{
Name: "message",
Attributes: []attribute.KeyValue{
otelgrpc.RPCMessageIDKey.Int(4),
otelgrpc.RPCMessageTypeKey.String("SENT"),
otelgrpc.RPCMessageUncompressedSizeKey.Int(45912),
},
},
// client does not record an event for the server response.
Expand All @@ -242,39 +234,34 @@ func checkStreamClientSpans(t *testing.T, spans []trace.ReadOnlySpan) {
Attributes: []attribute.KeyValue{
otelgrpc.RPCMessageIDKey.Int(1),
otelgrpc.RPCMessageTypeKey.String("SENT"),
otelgrpc.RPCMessageUncompressedSizeKey.Int(21),
},
},
{
Name: "message",
Attributes: []attribute.KeyValue{
otelgrpc.RPCMessageIDKey.Int(1),
otelgrpc.RPCMessageTypeKey.String("RECEIVED"),
otelgrpc.RPCMessageUncompressedSizeKey.Int(31423),
},
},
{
Name: "message",
Attributes: []attribute.KeyValue{
otelgrpc.RPCMessageIDKey.Int(2),
otelgrpc.RPCMessageTypeKey.String("RECEIVED"),
otelgrpc.RPCMessageUncompressedSizeKey.Int(13),
},
},
{
Name: "message",
Attributes: []attribute.KeyValue{
otelgrpc.RPCMessageIDKey.Int(3),
otelgrpc.RPCMessageTypeKey.String("RECEIVED"),
otelgrpc.RPCMessageUncompressedSizeKey.Int(2659),
},
},
{
Name: "message",
Attributes: []attribute.KeyValue{
otelgrpc.RPCMessageIDKey.Int(4),
otelgrpc.RPCMessageTypeKey.String("RECEIVED"),
otelgrpc.RPCMessageUncompressedSizeKey.Int(58987),
},
},
}, streamOutput.Events())
Expand All @@ -294,63 +281,55 @@ func checkStreamClientSpans(t *testing.T, spans []trace.ReadOnlySpan) {
Attributes: []attribute.KeyValue{
otelgrpc.RPCMessageIDKey.Int(1),
otelgrpc.RPCMessageTypeKey.String("SENT"),
otelgrpc.RPCMessageUncompressedSizeKey.Int(27196),
},
},
{
Name: "message",
Attributes: []attribute.KeyValue{
otelgrpc.RPCMessageIDKey.Int(1),
otelgrpc.RPCMessageTypeKey.String("RECEIVED"),
otelgrpc.RPCMessageUncompressedSizeKey.Int(31423),
},
},
{
Name: "message",
Attributes: []attribute.KeyValue{
otelgrpc.RPCMessageIDKey.Int(2),
otelgrpc.RPCMessageTypeKey.String("SENT"),
otelgrpc.RPCMessageUncompressedSizeKey.Int(16),
},
},
{
Name: "message",
Attributes: []attribute.KeyValue{
otelgrpc.RPCMessageIDKey.Int(2),
otelgrpc.RPCMessageTypeKey.String("RECEIVED"),
otelgrpc.RPCMessageUncompressedSizeKey.Int(13),
},
},
{
Name: "message",
Attributes: []attribute.KeyValue{
otelgrpc.RPCMessageIDKey.Int(3),
otelgrpc.RPCMessageTypeKey.String("SENT"),
otelgrpc.RPCMessageUncompressedSizeKey.Int(1839),
},
},
{
Name: "message",
Attributes: []attribute.KeyValue{
otelgrpc.RPCMessageIDKey.Int(3),
otelgrpc.RPCMessageTypeKey.String("RECEIVED"),
otelgrpc.RPCMessageUncompressedSizeKey.Int(2659),
},
},
{
Name: "message",
Attributes: []attribute.KeyValue{
otelgrpc.RPCMessageIDKey.Int(4),
otelgrpc.RPCMessageTypeKey.String("SENT"),
otelgrpc.RPCMessageUncompressedSizeKey.Int(45918),
},
},
{
Name: "message",
Attributes: []attribute.KeyValue{
otelgrpc.RPCMessageIDKey.Int(4),
otelgrpc.RPCMessageTypeKey.String("RECEIVED"),
otelgrpc.RPCMessageUncompressedSizeKey.Int(58987),
},
},
}, pingPong.Events())
Expand All @@ -375,39 +354,34 @@ func checkStreamServerSpans(t *testing.T, spans []trace.ReadOnlySpan) {
Attributes: []attribute.KeyValue{
otelgrpc.RPCMessageIDKey.Int(1),
otelgrpc.RPCMessageTypeKey.String("RECEIVED"),
otelgrpc.RPCMessageUncompressedSizeKey.Int(27190),
},
},
{
Name: "message",
Attributes: []attribute.KeyValue{
otelgrpc.RPCMessageIDKey.Int(2),
otelgrpc.RPCMessageTypeKey.String("RECEIVED"),
otelgrpc.RPCMessageUncompressedSizeKey.Int(12),
},
},
{
Name: "message",
Attributes: []attribute.KeyValue{
otelgrpc.RPCMessageIDKey.Int(3),
otelgrpc.RPCMessageTypeKey.String("RECEIVED"),
otelgrpc.RPCMessageUncompressedSizeKey.Int(1834),
},
},
{
Name: "message",
Attributes: []attribute.KeyValue{
otelgrpc.RPCMessageIDKey.Int(4),
otelgrpc.RPCMessageTypeKey.String("RECEIVED"),
otelgrpc.RPCMessageUncompressedSizeKey.Int(45912),
},
},
{
Name: "message",
Attributes: []attribute.KeyValue{
otelgrpc.RPCMessageIDKey.Int(1),
otelgrpc.RPCMessageTypeKey.String("SENT"),
otelgrpc.RPCMessageUncompressedSizeKey.Int(4),
},
},
}, streamInput.Events())
Expand All @@ -428,39 +402,34 @@ func checkStreamServerSpans(t *testing.T, spans []trace.ReadOnlySpan) {
Attributes: []attribute.KeyValue{
otelgrpc.RPCMessageIDKey.Int(1),
otelgrpc.RPCMessageTypeKey.String("RECEIVED"),
otelgrpc.RPCMessageUncompressedSizeKey.Int(21),
},
},
{
Name: "message",
Attributes: []attribute.KeyValue{
otelgrpc.RPCMessageIDKey.Int(1),
otelgrpc.RPCMessageTypeKey.String("SENT"),
otelgrpc.RPCMessageUncompressedSizeKey.Int(31423),
},
},
{
Name: "message",
Attributes: []attribute.KeyValue{
otelgrpc.RPCMessageIDKey.Int(2),
otelgrpc.RPCMessageTypeKey.String("SENT"),
otelgrpc.RPCMessageUncompressedSizeKey.Int(13),
},
},
{
Name: "message",
Attributes: []attribute.KeyValue{
otelgrpc.RPCMessageIDKey.Int(3),
otelgrpc.RPCMessageTypeKey.String("SENT"),
otelgrpc.RPCMessageUncompressedSizeKey.Int(2659),
},
},
{
Name: "message",
Attributes: []attribute.KeyValue{
otelgrpc.RPCMessageIDKey.Int(4),
otelgrpc.RPCMessageTypeKey.String("SENT"),
otelgrpc.RPCMessageUncompressedSizeKey.Int(58987),
},
},
}, streamOutput.Events())
Expand All @@ -480,63 +449,55 @@ func checkStreamServerSpans(t *testing.T, spans []trace.ReadOnlySpan) {
Attributes: []attribute.KeyValue{
otelgrpc.RPCMessageIDKey.Int(1),
otelgrpc.RPCMessageTypeKey.String("RECEIVED"),
otelgrpc.RPCMessageUncompressedSizeKey.Int(27196),
},
},
{
Name: "message",
Attributes: []attribute.KeyValue{
otelgrpc.RPCMessageIDKey.Int(1),
otelgrpc.RPCMessageTypeKey.String("SENT"),
otelgrpc.RPCMessageUncompressedSizeKey.Int(31423),
},
},
{
Name: "message",
Attributes: []attribute.KeyValue{
otelgrpc.RPCMessageIDKey.Int(2),
otelgrpc.RPCMessageTypeKey.String("RECEIVED"),
otelgrpc.RPCMessageUncompressedSizeKey.Int(16),
},
},
{
Name: "message",
Attributes: []attribute.KeyValue{
otelgrpc.RPCMessageIDKey.Int(2),
otelgrpc.RPCMessageTypeKey.String("SENT"),
otelgrpc.RPCMessageUncompressedSizeKey.Int(13),
},
},
{
Name: "message",
Attributes: []attribute.KeyValue{
otelgrpc.RPCMessageIDKey.Int(3),
otelgrpc.RPCMessageTypeKey.String("RECEIVED"),
otelgrpc.RPCMessageUncompressedSizeKey.Int(1839),
},
},
{
Name: "message",
Attributes: []attribute.KeyValue{
otelgrpc.RPCMessageIDKey.Int(3),
otelgrpc.RPCMessageTypeKey.String("SENT"),
otelgrpc.RPCMessageUncompressedSizeKey.Int(2659),
},
},
{
Name: "message",
Attributes: []attribute.KeyValue{
otelgrpc.RPCMessageIDKey.Int(4),
otelgrpc.RPCMessageTypeKey.String("RECEIVED"),
otelgrpc.RPCMessageUncompressedSizeKey.Int(45918),
},
},
{
Name: "message",
Attributes: []attribute.KeyValue{
otelgrpc.RPCMessageIDKey.Int(4),
otelgrpc.RPCMessageTypeKey.String("SENT"),
otelgrpc.RPCMessageUncompressedSizeKey.Int(58987),
},
},
}, pingPong.Events())
Expand All @@ -560,15 +521,13 @@ func checkUnaryServerSpans(t *testing.T, spans []trace.ReadOnlySpan) {
Attributes: []attribute.KeyValue{
otelgrpc.RPCMessageIDKey.Int(1),
otelgrpc.RPCMessageTypeKey.String("RECEIVED"),
otelgrpc.RPCMessageUncompressedSizeKey.Int(0),
},
},
{
Name: "message",
Attributes: []attribute.KeyValue{
otelgrpc.RPCMessageIDKey.Int(1),
otelgrpc.RPCMessageTypeKey.String("SENT"),
otelgrpc.RPCMessageUncompressedSizeKey.Int(0),
},
},
}, emptySpan.Events())
Expand All @@ -589,7 +548,6 @@ func checkUnaryServerSpans(t *testing.T, spans []trace.ReadOnlySpan) {
otelgrpc.RPCMessageIDKey.Int(1),
otelgrpc.RPCMessageTypeKey.String("RECEIVED"),
// largeReqSize from "google.golang.org/grpc/interop" + 12 (overhead).
otelgrpc.RPCMessageUncompressedSizeKey.Int(271840),
},
},
{
Expand All @@ -598,7 +556,6 @@ func checkUnaryServerSpans(t *testing.T, spans []trace.ReadOnlySpan) {
otelgrpc.RPCMessageIDKey.Int(1),
otelgrpc.RPCMessageTypeKey.String("SENT"),
// largeRespSize from "google.golang.org/grpc/interop" + 8 (overhead).
otelgrpc.RPCMessageUncompressedSizeKey.Int(314167),
},
},
}, largeSpan.Events())
Expand Down

0 comments on commit c9c9290

Please sign in to comment.