Skip to content

Commit

Permalink
chore: add typing to propagator carrier (open-telemetry#772)
Browse files Browse the repository at this point in the history
  • Loading branch information
dyladan committed Feb 18, 2021
1 parent e71ab28 commit 604bf55
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
9 changes: 5 additions & 4 deletions api/src/context/propagation/HttpTextFormat.ts
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;
}
5 changes: 3 additions & 2 deletions api/src/context/propagation/NoopHttpTextFormat.ts
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 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 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

0 comments on commit 604bf55

Please sign in to comment.