Skip to content

Commit

Permalink
feat: implement W3C Correlation Context propagator
Browse files Browse the repository at this point in the history
Signed-off-by: Ruben Vargas <ruben.vp8510@gmail.com>
  • Loading branch information
rubenvp8510 authored and Ruben committed Mar 3, 2020
1 parent 5a5b6b8 commit e82d1c9
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*!
* 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 { DistributedContext } from '@opentelemetry/api';
import { Context } from '@opentelemetry/scope-base';

const DISTRIBUTE_CONTEXTS = Context.createKey(
'OpenTelemetry Distributed Contexts Key'
);

export function getDistributedContext(
context: Context
): DistributedContext | undefined {
return (
(context.getValue(DISTRIBUTE_CONTEXTS) as DistributedContext) || undefined
);
}

export function setDistributedContext(
context: Context,
distributedContext: DistributedContext
): Context {
return context.setValue(DISTRIBUTE_CONTEXTS, distributedContext);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*!
* 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 {
Carrier,
Context,
HttpTextFormat,
DistributedContext,
} from '@opentelemetry/api';

import {
getDistributedContext,
setDistributedContext,
} from '../distribute-context';

export const CORRELATION_CONTEXT_HEADER = 'Correlation-Context';

/**
* Propagates {@link DistributedContext} through Context format propagation.
*
* Based on the Correlation Context specification:
* https://w3c.github.io/correlation-context/
*/
export class HttpTraceContext implements HttpTextFormat {
inject(context: Context, carrier: Carrier) {
const distContext = getDistributedContext(context);
if (distContext) {
const headerValue = Object.keys(distContext)
.map(
(key: string) =>
`${encodeURIComponent(key)}=${encodeURIComponent(
distContext[key].value
)}`
)
.join(',');
if (headerValue.length > 0) {
carrier[CORRELATION_CONTEXT_HEADER] = headerValue;
}
}
}

extract(context: Context, carrier: Carrier): Context {
const headerValue: string = carrier[CORRELATION_CONTEXT_HEADER] as string;
const distributedContext: DistributedContext = {};
if (headerValue.length > 0) {
headerValue.split(',').forEach(entry => {
const valueProps = entry.split(';');
if (valueProps.length > 0) {
const keyPair = valueProps[0].split('=');
const key = decodeURIComponent(keyPair[0].trim());
let value = decodeURIComponent(keyPair[1].trim());
if (valueProps.length > 1) {
value = value + ';' + valueProps.join(';');
}
distributedContext[key] = { value };
}
});
}
return setDistributedContext(context, distributedContext);
}
}

0 comments on commit e82d1c9

Please sign in to comment.