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

chore: add typing to propagator carrier #772

Merged
merged 1 commit into from
Feb 7, 2020
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

import { SpanContext } from '../../trace/span_context';
import { Carrier } from './carrier';

/**
* Injects and extracts a value as text into carriers that travel in-band
Expand All @@ -35,18 +36,18 @@ export interface HttpTextFormat {
*
* @param spanContext the SpanContext to transmit over the wire.
* @param format the format of the carrier.
* @param carrier the carrier of propagation fields, such as an http request.
* @param carrier the carrier of propagation fields, such as http request headers.
*/
inject(spanContext: SpanContext, format: string, carrier: unknown): void;
inject(spanContext: SpanContext, format: string, carrier: Carrier): void;

/**
* Returns a {@link SpanContext} instance extracted from `carrier` in the
* given format from upstream.
*
* @param format the format of the carrier.
* @param carrier the carrier of propagation fields, such as an http request.
* @param carrier the carrier of propagation fields, such as http request headers.
* @returns SpanContext The extracted SpanContext, or null if no such
* SpanContext could be found in carrier.
*/
extract(format: string, carrier: unknown): SpanContext | null;
extract(format: string, carrier: Carrier): SpanContext | null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@
*/

import { SpanContext } from '../../trace/span_context';
import { Carrier } from './carrier';
import { HttpTextFormat } from './HttpTextFormat';

/**
* No-op implementations of {@link HttpTextFormat}.
*/
export class NoopHttpTextFormat implements HttpTextFormat {
// By default does nothing
inject(spanContext: SpanContext, format: string, carrier: unknown): void {}
inject(spanContext: SpanContext, format: string, carrier: Carrier): void {}
// By default does nothing
extract(format: string, carrier: unknown): SpanContext | null {
extract(format: string, carrier: Carrier): SpanContext | null {
return null;
}
}
Expand Down
19 changes: 19 additions & 0 deletions packages/opentelemetry-api/src/context/propagation/carrier.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*!
* Copyright 2020, OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export type Carrier = {
[key: string]: unknown;
};
1 change: 1 addition & 0 deletions packages/opentelemetry-api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
export * from './common/Logger';
export * from './common/Time';
export * from './context/propagation/BinaryFormat';
export * from './context/propagation/carrier';
export * from './context/propagation/HttpTextFormat';
export * from './distributed_context/DistributedContext';
export * from './distributed_context/EntryValue';
Expand Down
18 changes: 8 additions & 10 deletions packages/opentelemetry-core/src/context/propagation/B3Format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@
* limitations under the License.
*/

import { SpanContext, HttpTextFormat, TraceFlags } from '@opentelemetry/api';
import {
Carrier,
HttpTextFormat,
SpanContext,
TraceFlags,
} from '@opentelemetry/api';

export const X_B3_TRACE_ID = 'x-b3-traceid';
export const X_B3_SPAN_ID = 'x-b3-spanid';
Expand All @@ -36,11 +41,7 @@ function isValidSpanId(spanId: string): boolean {
* Based on: https://github.com/openzipkin/b3-propagation
*/
export class B3Format implements HttpTextFormat {
inject(
spanContext: SpanContext,
format: string,
carrier: { [key: string]: unknown }
): void {
inject(spanContext: SpanContext, format: string, carrier: Carrier): void {
if (
isValidTraceId(spanContext.traceId) &&
isValidSpanId(spanContext.spanId)
Expand All @@ -56,10 +57,7 @@ export class B3Format implements HttpTextFormat {
}
}

extract(
format: string,
carrier: { [key: string]: unknown }
): SpanContext | null {
extract(format: string, carrier: Carrier): SpanContext | null {
const traceIdHeader = carrier[X_B3_TRACE_ID];
const spanIdHeader = carrier[X_B3_SPAN_ID];
const sampledHeader = carrier[X_B3_SAMPLED];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@
* limitations under the License.
*/

import { HttpTextFormat, SpanContext, TraceFlags } from '@opentelemetry/api';
import {
Carrier,
HttpTextFormat,
SpanContext,
TraceFlags,
} from '@opentelemetry/api';
import { TraceState } from '../../trace/TraceState';

export const TRACE_PARENT_HEADER = 'traceparent';
Expand Down Expand Up @@ -56,11 +61,7 @@ export function parseTraceParent(traceParent: string): SpanContext | null {
* https://www.w3.org/TR/trace-context/
*/
export class HttpTraceContext implements HttpTextFormat {
inject(
spanContext: SpanContext,
format: string,
carrier: { [key: string]: unknown }
) {
inject(spanContext: SpanContext, format: string, carrier: Carrier) {
const traceParent = `${VERSION}-${spanContext.traceId}-${
spanContext.spanId
}-0${Number(spanContext.traceFlags || TraceFlags.UNSAMPLED).toString(16)}`;
Expand All @@ -71,10 +72,7 @@ export class HttpTraceContext implements HttpTextFormat {
}
}

extract(
format: string,
carrier: { [key: string]: unknown }
): SpanContext | null {
extract(format: string, carrier: Carrier): SpanContext | null {
const traceParentHeader = carrier[TRACE_PARENT_HEADER];
if (!traceParentHeader) return null;
const traceParent = Array.isArray(traceParentHeader)
Expand Down
2 changes: 1 addition & 1 deletion packages/opentelemetry-plugin-http/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ export class HttpPlugin extends BasePlugin<Http> {
const span = plugin._startHttpSpan(operationName, spanOptions);
plugin._tracer
.getHttpTextFormat()
.inject(span.context(), Format.HTTP, options.headers);
.inject(span.context(), Format.HTTP, options.headers!);
Copy link
Member

Choose a reason for hiding this comment

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

ahh, earlier this worked due to unknown type.


const request: ClientRequest = plugin._safeExecute(
span,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@
* limitations under the License.
*/

import { SpanContext, HttpTextFormat, TraceFlags } from '@opentelemetry/api';
import {
SpanContext,
HttpTextFormat,
TraceFlags,
Carrier,
} from '@opentelemetry/api';

export const UBER_TRACE_ID_HEADER = 'uber-trace-id';

Expand Down Expand Up @@ -46,13 +51,9 @@ export class JaegerHttpTraceFormat implements HttpTextFormat {
/**
* @param {SpanContext} spanContext - context from which we take information to inject in carrier.
* @param {string} format - unused.
* @param { [key: string]: unknown } carrier - a carrier to which span information will be injected.
* @param {Carrier} carrier - a carrier to which span information will be injected.
**/
inject(
spanContext: SpanContext,
format: string,
carrier: { [key: string]: unknown }
) {
inject(spanContext: SpanContext, format: string, carrier: Carrier) {
const traceFlags = `0${(
spanContext.traceFlags || TraceFlags.UNSAMPLED
).toString(16)}`;
Expand All @@ -64,13 +65,10 @@ export class JaegerHttpTraceFormat implements HttpTextFormat {

/**
* @param {string} format - unused.
* @param { [key: string]: unknown } carrier - a carrier from which span context will be constructed.
* @param {Carrier} carrier - a carrier from which span context will be constructed.
* @return {SpanContext} - returns a span context extracted from carrier.
**/
extract(
format: string,
carrier: { [key: string]: unknown }
): SpanContext | null {
extract(format: string, carrier: Carrier): SpanContext | null {
const uberTraceIdHeader = carrier[this._jaegerTraceHeader];
if (!uberTraceIdHeader) return null;
const uberTraceId = Array.isArray(uberTraceIdHeader)
Expand Down
7 changes: 5 additions & 2 deletions packages/opentelemetry-shim-opentracing/src/shim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export class TracerShim extends opentracing.Tracer {
_inject(
spanContext: opentracing.SpanContext,
format: string,
carrier: unknown
carrier: types.Carrier
): void {
const opentelemSpanContext: types.SpanContext = (spanContext as SpanContextShim).getSpanContext();
switch (format) {
Expand All @@ -141,7 +141,10 @@ export class TracerShim extends opentracing.Tracer {
}
}

_extract(format: string, carrier: unknown): opentracing.SpanContext | null {
_extract(
format: string,
carrier: types.Carrier
): opentracing.SpanContext | null {
switch (format) {
// tslint:disable-next-line:no-switch-case-fall-through
case opentracing.FORMAT_HTTP_HEADERS:
Expand Down