Skip to content
This repository was archived by the owner on Nov 10, 2022. It is now read-only.

Commit a7f1619

Browse files
committed
chore: move span method for context in trace API #40
1 parent 9fba460 commit a7f1619

File tree

7 files changed

+144
-113
lines changed

7 files changed

+144
-113
lines changed

src/api/trace.ts

+9
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { ProxyTracerProvider } from '../trace/ProxyTracerProvider';
1818
import { Tracer } from '../trace/tracer';
1919
import { TracerProvider } from '../trace/tracer_provider';
2020
import { isSpanContextValid } from '../trace/spancontext-utils';
21+
import { getSpan, getSpanContext, setSpan, setSpanContext } from '../trace/context-utils'
2122
import {
2223
getGlobal,
2324
registerGlobal,
@@ -77,4 +78,12 @@ export class TraceAPI {
7778
}
7879

7980
public isSpanContextValid = isSpanContextValid;
81+
82+
public getSpan = getSpan;
83+
84+
public getSpanContext = getSpanContext;
85+
86+
public setSpan = setSpan;
87+
88+
public setSpanContext = setSpanContext;
8089
}

src/baggage/index.ts

+24
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,18 @@ import { Baggage } from './Baggage';
1818
import { BaggageEntry, BaggageEntryMetadata } from './Entry';
1919
import { BaggageImpl } from './internal/baggage';
2020
import { baggageEntryMetadataSymbol } from './internal/symbol';
21+
import { Context } from '../context/types';
22+
import { createContextKey } from '../context/context'
23+
2124

2225
export * from './Baggage';
2326
export * from './Entry';
2427

28+
/**
29+
* Baggage key
30+
*/
31+
const BAGGAGE_KEY = createContextKey('OpenTelemetry Baggage Key');
32+
2533
/**
2634
* Create a new Baggage with optional entries
2735
*
@@ -33,6 +41,22 @@ export function createBaggage(
3341
return new BaggageImpl(new Map(Object.entries(entries)));
3442
}
3543

44+
/**
45+
* @param {Context} Context that manage all context values
46+
* @returns {Baggage} Extracted baggage from the context
47+
*/
48+
export function getBaggage(context: Context): Baggage | undefined {
49+
return (context.getValue(BAGGAGE_KEY) as Baggage) || undefined;
50+
}
51+
52+
/**
53+
* @param {Context} Context that manage all context values
54+
* @param {Baggage} baggage that will be set in the actual context
55+
*/
56+
export function setBaggage(context: Context, baggage: Baggage): Context {
57+
return context.setValue(BAGGAGE_KEY, baggage);
58+
}
59+
3660
/**
3761
* Create a serializable BaggageEntryMetadata object from a string.
3862
*

src/context/context.ts

-107
Original file line numberDiff line numberDiff line change
@@ -15,114 +15,7 @@
1515
*/
1616

1717
import { Context } from './types';
18-
import { Baggage, Span, SpanContext } from '../';
19-
import { NoopSpan } from '../trace/NoopSpan';
2018

21-
/**
22-
* span key
23-
*/
24-
const SPAN_KEY = createContextKey('OpenTelemetry Context Key SPAN');
25-
26-
/**
27-
* Shared key for indicating if instrumentation should be suppressed beyond
28-
* this current scope.
29-
*/
30-
const SUPPRESS_INSTRUMENTATION_KEY = createContextKey(
31-
'OpenTelemetry Context Key SUPPRESS_INSTRUMENTATION'
32-
);
33-
34-
/**
35-
* Baggage key
36-
*/
37-
const BAGGAGE_KEY = createContextKey('OpenTelemetry Baggage Key');
38-
39-
/**
40-
* Return the span if one exists
41-
*
42-
* @param context context to get span from
43-
*/
44-
export function getSpan(context: Context): Span | undefined {
45-
return (context.getValue(SPAN_KEY) as Span) || undefined;
46-
}
47-
48-
/**
49-
* Set the span on a context
50-
*
51-
* @param context context to use as parent
52-
* @param span span to set active
53-
*/
54-
export function setSpan(context: Context, span: Span): Context {
55-
return context.setValue(SPAN_KEY, span);
56-
}
57-
58-
/**
59-
* Wrap span context in a NoopSpan and set as span in a new
60-
* context
61-
*
62-
* @param context context to set active span on
63-
* @param spanContext span context to be wrapped
64-
*/
65-
export function setSpanContext(
66-
context: Context,
67-
spanContext: SpanContext
68-
): Context {
69-
return setSpan(context, new NoopSpan(spanContext));
70-
}
71-
72-
/**
73-
* Get the span context of the span if it exists.
74-
*
75-
* @param context context to get values from
76-
*/
77-
export function getSpanContext(context: Context): SpanContext | undefined {
78-
return getSpan(context)?.context();
79-
}
80-
81-
/**
82-
* Sets value on context to indicate that instrumentation should
83-
* be suppressed beyond this current scope.
84-
*
85-
* @param context context to set the suppress instrumentation value on.
86-
*/
87-
export function suppressInstrumentation(context: Context): Context {
88-
return context.setValue(SUPPRESS_INSTRUMENTATION_KEY, true);
89-
}
90-
91-
/**
92-
* Sets value on context to indicate that instrumentation should
93-
* no-longer be suppressed beyond this current scope.
94-
*
95-
* @param context context to set the suppress instrumentation value on.
96-
*/
97-
export function unsuppressInstrumentation(context: Context): Context {
98-
return context.setValue(SUPPRESS_INSTRUMENTATION_KEY, false);
99-
}
100-
101-
/**
102-
* Return current suppress instrumentation value for the given context,
103-
* if it exists.
104-
*
105-
* @param context context check for the suppress instrumentation value.
106-
*/
107-
export function isInstrumentationSuppressed(context: Context): boolean {
108-
return Boolean(context.getValue(SUPPRESS_INSTRUMENTATION_KEY));
109-
}
110-
111-
/**
112-
* @param {Context} Context that manage all context values
113-
* @returns {Baggage} Extracted baggage from the context
114-
*/
115-
export function getBaggage(context: Context): Baggage | undefined {
116-
return (context.getValue(BAGGAGE_KEY) as Baggage) || undefined;
117-
}
118-
119-
/**
120-
* @param {Context} Context that manage all context values
121-
* @param {Baggage} baggage that will be set in the actual context
122-
*/
123-
export function setBaggage(context: Context, baggage: Baggage): Context {
124-
return context.setValue(BAGGAGE_KEY, baggage);
125-
}
12619

12720
/** Get a key to uniquely identify a context value */
12821
export function createContextKey(description: string) {

src/trace/NoopTracer.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { getSpanContext } from '../context/context';
17+
import { getSpanContext } from '../trace/context-utils';
1818
import { Context } from '../context/types';
1919
import { NoopSpan } from './NoopSpan';
2020
import { Span } from './span';

src/trace/context-utils.ts

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { createContextKey } from '../context/context'
18+
import { Context } from '../context/types'
19+
import { Span } from './span'
20+
import { SpanContext } from './span_context'
21+
import { NoopSpan } from './NoopSpan'
22+
23+
/**
24+
* span key
25+
*/
26+
const SPAN_KEY = createContextKey('OpenTelemetry Context Key SPAN');
27+
28+
/**
29+
* Shared key for indicating if instrumentation should be suppressed beyond
30+
* this current scope.
31+
*/
32+
const SUPPRESS_INSTRUMENTATION_KEY = createContextKey(
33+
'OpenTelemetry Context Key SUPPRESS_INSTRUMENTATION'
34+
);
35+
36+
/**
37+
* Return the span if one exists
38+
*
39+
* @param context context to get span from
40+
*/
41+
export function getSpan(context: Context): Span | undefined {
42+
return (context.getValue(SPAN_KEY) as Span) || undefined;
43+
}
44+
45+
/**
46+
* Set the span on a context
47+
*
48+
* @param context context to use as parent
49+
* @param span span to set active
50+
*/
51+
export function setSpan(context: Context, span: Span): Context {
52+
return context.setValue(SPAN_KEY, span);
53+
}
54+
55+
/**
56+
* Wrap span context in a NoopSpan and set as span in a new
57+
* context
58+
*
59+
* @param context context to set active span on
60+
* @param spanContext span context to be wrapped
61+
*/
62+
export function setSpanContext(
63+
context: Context,
64+
spanContext: SpanContext
65+
): Context {
66+
return setSpan(context, new NoopSpan(spanContext));
67+
}
68+
69+
/**
70+
* Get the span context of the span if it exists.
71+
*
72+
* @param context context to get values from
73+
*/
74+
export function getSpanContext(context: Context): SpanContext | undefined {
75+
return getSpan(context)?.context();
76+
}
77+
78+
/**
79+
* Sets value on context to indicate that instrumentation should
80+
* be suppressed beyond this current scope.
81+
*
82+
* @param context context to set the suppress instrumentation value on.
83+
*/
84+
export function suppressInstrumentation(context: Context): Context {
85+
return context.setValue(SUPPRESS_INSTRUMENTATION_KEY, true);
86+
}
87+
88+
/**
89+
* Sets value on context to indicate that instrumentation should
90+
* no-longer be suppressed beyond this current scope.
91+
*
92+
* @param context context to set the suppress instrumentation value on.
93+
*/
94+
export function unsuppressInstrumentation(context: Context): Context {
95+
return context.setValue(SUPPRESS_INSTRUMENTATION_KEY, false);
96+
}
97+
98+
/**
99+
* Return current suppress instrumentation value for the given context,
100+
* if it exists.
101+
*
102+
* @param context context check for the suppress instrumentation value.
103+
*/
104+
export function isInstrumentationSuppressed(context: Context): boolean {
105+
return Boolean(context.getValue(SUPPRESS_INSTRUMENTATION_KEY));
106+
}

test/noop-implementations/noop-tracer.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
SpanKind,
2222
TraceFlags,
2323
context,
24-
setSpanContext,
24+
trace,
2525
} from '../../src';
2626
import { NoopSpan } from '../../src/trace/NoopSpan';
2727

@@ -50,7 +50,7 @@ describe('NoopTracer', () => {
5050
const span = tracer.startSpan(
5151
'test-1',
5252
{},
53-
setSpanContext(context.active(), parent)
53+
trace.setSpanContext(context.active(), parent)
5454
);
5555
assert(span.context().traceId === parent.traceId);
5656
assert(span.context().spanId === parent.spanId);

test/context/context.test.ts renamed to test/trace/context-utils.test.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@
1616

1717
import * as assert from 'assert';
1818
import {
19-
createContextKey,
2019
isInstrumentationSuppressed,
21-
ROOT_CONTEXT,
2220
suppressInstrumentation,
2321
unsuppressInstrumentation,
24-
} from '../../src/context/context';
22+
} from '../../src/trace/context-utils';
23+
import { createContextKey, ROOT_CONTEXT} from '../../src/context/context'
2524

2625
const SUPPRESS_INSTRUMENTATION_KEY = createContextKey(
2726
'OpenTelemetry Context Key SUPPRESS_INSTRUMENTATION'

0 commit comments

Comments
 (0)