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

Add propagation API #797

Merged
merged 5 commits into from
Feb 26, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
71 changes: 71 additions & 0 deletions packages/opentelemetry-api/src/api/propagation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*!
* 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.
*/

import { Context } from '@opentelemetry/scope-base';
import { Carrier } from '../context/propagation/carrier';
import { HttpTextFormat } from '../context/propagation/HttpTextFormat';
import { NOOP_HTTP_TEXT_FORMAT } from '../context/propagation/NoopHttpTextFormat';
import { ContextAPI } from './context';

const contextApi = ContextAPI.getInstance();

/**
* Singleton object which represents the entry point to the OpenTelemetry Propagation API
*/
export class PropagationAPI {
dyladan marked this conversation as resolved.
Show resolved Hide resolved
private static _instance?: PropagationAPI;
private _propagator: HttpTextFormat = NOOP_HTTP_TEXT_FORMAT;

/** Empty private constructor prevents end users from constructing a new instance of the API */
private constructor() {}

/** Get the singleton instance of the Propagator API */
public static getInstance(): PropagationAPI {
if (!this._instance) {
this._instance = new PropagationAPI();
}

return this._instance;
}

/**
* Set the current propagator. Returns the initialized propagator
*/
public initGlobalPropagator(propagator: HttpTextFormat): HttpTextFormat {
Copy link
Member

Choose a reason for hiding this comment

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

nit: consistency with using public keyword

Copy link
Member Author

Choose a reason for hiding this comment

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

to be clear, we want to always use or never use?

this._propagator = propagator;
return propagator;
}

/**
* Inject context into a carrier to be propagated inter-process
*
* @param carrier carrier to inject context into
* @param context Context carrying tracing data to inject. Defaults to the currently active context.
*/
inject(carrier: Carrier, context = contextApi.active()): void {
return this._propagator.inject(context, carrier);
}

/**
* Extract context from a carrier
*
* @param carrier Carrier to extract context from
* @param context Context which the newly created context will inherit from. Defaults to the currently active context.
*/
extract(carrier: Carrier, context = contextApi.active()): Context {
return this._propagator.extract(context, carrier);
}
}
5 changes: 5 additions & 0 deletions packages/opentelemetry-api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,13 @@ import { MetricsAPI } from './api/metrics';
/** Entrypoint for metrics API */
export const metrics = MetricsAPI.getInstance();

import { PropagationAPI } from './api/propagation';
/** Entrypoint for propagation API */
export const propagation = PropagationAPI.getInstance();

export default {
trace,
metrics,
context,
propagation,
};