Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions packages/opentelemetry-plugin-grpc/src/grpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,19 @@
* limitations under the License.
*/

import { BasePlugin } from '@opentelemetry/core';
import {
BasePlugin,
getExtractedSpanContext,
setExtractedSpanContext,
} from '@opentelemetry/core';
import {
CanonicalCode,
Span,
SpanContext,
SpanKind,
SpanOptions,
Status,
Context,
} from '@opentelemetry/api';
import * as events from 'events';
import * as grpcTypes from 'grpc';
Expand Down Expand Up @@ -122,24 +127,25 @@ export class GrpcPlugin extends BasePlugin<grpc> {
}
}

private _getSpanContext(metadata: grpcTypes.Metadata): SpanContext | null {
const metadataValue = metadata.getMap()[GRPC_TRACE_KEY] as Buffer;
// Entry doesn't exist
if (!metadataValue) {
return null;
}
return this._tracer.getBinaryFormat().fromBytes(metadataValue);
private _getSpanContext(
metadata: grpcTypes.Metadata
): SpanContext | undefined {
return getExtractedSpanContext(
this._tracer.getHttpTextFormat().extract(Context.TODO, metadata.getMap())
);
}

private _setSpanContext(
metadata: grpcTypes.Metadata,
spanContext: SpanContext
): void {
const serializedSpanContext = this._tracer
.getBinaryFormat()
.toBytes(spanContext);
const buffer = Buffer.from(serializedSpanContext);
metadata.set(GRPC_TRACE_KEY, buffer);
const carrier = {};
this._tracer
.getHttpTextFormat()
.inject(setExtractedSpanContext(Context.TODO, spanContext), carrier);
for (const [k, v] of Object.entries(carrier)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to approve this for now, but we should be using the new setter interface for propagators to handle this. https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/api-propagators.md#setter-argument

metadata.set(k, v as string);
}
}

private _patchServer() {
Expand Down