Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plugin(grpc): use HttpTextFormat instead of Binary propagator #762

Merged
merged 6 commits into from
Feb 22, 2020
Merged
Changes from 3 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
35 changes: 22 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,28 @@ 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.ROOT_CONTEXT, spanContext),
mayurkale22 marked this conversation as resolved.
Show resolved Hide resolved
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