diff --git a/experimental/packages/opentelemetry-instrumentation-grpc/src/grpc-js/index.ts b/experimental/packages/opentelemetry-instrumentation-grpc/src/grpc-js/index.ts index 2e5c833b193..6608521bd6b 100644 --- a/experimental/packages/opentelemetry-instrumentation-grpc/src/grpc-js/index.ts +++ b/experimental/packages/opentelemetry-instrumentation-grpc/src/grpc-js/index.ts @@ -212,6 +212,24 @@ export class GrpcJsInstrumentation extends InstrumentationBase { [SemanticAttributes.RPC_SERVICE]: service, }); + instrumentation._metadataCapture.server.captureRequestMetadata( + span, + call.metadata + ); + + instrumentation._wrap( + call, + 'sendMetadata', + originalSendMetadata => + (responseMetadata: grpcJs.Metadata) => { + instrumentation._metadataCapture.server.captureResponseMetadata( + span, + responseMetadata + ); + originalSendMetadata.call(call, responseMetadata); + } + ); + context.with(trace.setSpan(context.active(), span), () => { handleServerFunction.call( self, @@ -385,6 +403,16 @@ export class GrpcJsInstrumentation extends InstrumentationBase { config.metadataToSpanAttributes?.client?.responseMetadata ?? [] ), }, + server: { + captureRequestMetadata: metadataCapture( + 'request', + config.metadataToSpanAttributes?.server?.requestMetadata ?? [] + ), + captureResponseMetadata: metadataCapture( + 'response', + config.metadataToSpanAttributes?.server?.responseMetadata ?? [] + ), + }, }; } } diff --git a/experimental/packages/opentelemetry-instrumentation-grpc/src/grpc/index.ts b/experimental/packages/opentelemetry-instrumentation-grpc/src/grpc/index.ts index 4df381eb668..fbeac0395ee 100644 --- a/experimental/packages/opentelemetry-instrumentation-grpc/src/grpc/index.ts +++ b/experimental/packages/opentelemetry-instrumentation-grpc/src/grpc/index.ts @@ -223,6 +223,24 @@ export class GrpcNativeInstrumentation extends InstrumentationBase< [SemanticAttributes.RPC_SERVICE]: service, }); + instrumentation._metadataCapture.server.captureRequestMetadata( + span, + call.metadata + ); + + instrumentation._wrap( + call as any, + 'sendMetadata', + originalSendMetadata => + (responseMetadata: grpcTypes.Metadata) => { + instrumentation._metadataCapture.server.captureResponseMetadata( + span, + responseMetadata + ); + originalSendMetadata.call(call, responseMetadata); + } + ); + context.with(trace.setSpan(context.active(), span), () => { switch (type) { case 'unary': @@ -370,6 +388,16 @@ export class GrpcNativeInstrumentation extends InstrumentationBase< config.metadataToSpanAttributes?.client?.responseMetadata ?? [] ), }, + server: { + captureRequestMetadata: metadataCapture( + 'request', + config.metadataToSpanAttributes?.server?.requestMetadata ?? [] + ), + captureResponseMetadata: metadataCapture( + 'response', + config.metadataToSpanAttributes?.server?.responseMetadata ?? [] + ), + }, }; } } diff --git a/experimental/packages/opentelemetry-instrumentation-grpc/src/internal-types.ts b/experimental/packages/opentelemetry-instrumentation-grpc/src/internal-types.ts index d0052f7643e..ebc17ae1d4a 100644 --- a/experimental/packages/opentelemetry-instrumentation-grpc/src/internal-types.ts +++ b/experimental/packages/opentelemetry-instrumentation-grpc/src/internal-types.ts @@ -29,4 +29,14 @@ export type metadataCaptureType = { metadata: grpcJsTypes.Metadata | grpcTypes.Metadata ) => void; }; + server: { + captureRequestMetadata: ( + span: Span, + metadata: grpcJsTypes.Metadata | grpcTypes.Metadata + ) => void; + captureResponseMetadata: ( + span: Span, + metadata: grpcJsTypes.Metadata | grpcTypes.Metadata + ) => void; + }; }; diff --git a/experimental/packages/opentelemetry-instrumentation-grpc/src/types.ts b/experimental/packages/opentelemetry-instrumentation-grpc/src/types.ts index dd08d8224b8..430476b7a47 100644 --- a/experimental/packages/opentelemetry-instrumentation-grpc/src/types.ts +++ b/experimental/packages/opentelemetry-instrumentation-grpc/src/types.ts @@ -29,5 +29,9 @@ export interface GrpcInstrumentationConfig extends InstrumentationConfig { responseMetadata?: string[]; requestMetadata?: string[]; }; + server?: { + responseMetadata?: string[]; + requestMetadata?: string[]; + }; }; } diff --git a/experimental/packages/opentelemetry-instrumentation-grpc/test/helper.ts b/experimental/packages/opentelemetry-instrumentation-grpc/test/helper.ts index 85b6a1c64e2..b71711101a2 100644 --- a/experimental/packages/opentelemetry-instrumentation-grpc/test/helper.ts +++ b/experimental/packages/opentelemetry-instrumentation-grpc/test/helper.ts @@ -978,6 +978,10 @@ export const runTests = ( requestMetadata: ['client_metadata_key'], responseMetadata: ['server_metadata_key'], }, + server: { + requestMetadata: ['client_metadata_key'], + responseMetadata: ['server_metadata_key'], + }, }, }); @@ -1006,6 +1010,11 @@ export const runTests = ( 'rpc.response.metadata.server_metadata_key': 'server_metadata_value', }, + serverAttributes: { + 'rpc.request.metadata.client_metadata_key': 'client_metadata_value', + 'rpc.response.metadata.server_metadata_key': + 'server_metadata_value', + }, }; runTestWithAttributeValidation(