Skip to content

Commit

Permalink
plugin(grpc): use HttpTextFormat instead of Binary propagator (open-t…
Browse files Browse the repository at this point in the history
…elemetry#762)

* feat: update HttpTraceContext for gRPC metadata

* keep textformat interface intact

* update the usage based on new implementation

Co-authored-by: Daniel Dyla <[email protected]>
  • Loading branch information
mayurkale22 and dyladan authored Feb 22, 2020
1 parent 12570c9 commit 03bf191
Showing 1 changed file with 19 additions and 13 deletions.
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)) {
metadata.set(k, v as string);
}
}

private _patchServer() {
Expand Down

0 comments on commit 03bf191

Please sign in to comment.