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

Commit d2dad62

Browse files
committed
chore: move span method for context in trace API #40
1 parent e938882 commit d2dad62

File tree

7 files changed

+148
-114
lines changed

7 files changed

+148
-114
lines changed

src/api/trace.ts

+14
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ import {
2626
} from '../trace/spancontext-utils';
2727
import { Tracer } from '../trace/tracer';
2828
import { TracerProvider } from '../trace/tracer_provider';
29+
import {
30+
getSpan,
31+
getSpanContext,
32+
setSpan,
33+
setSpanContext,
34+
} from '../trace/context-utils';
2935

3036
const API_NAME = 'trace';
3137

@@ -82,4 +88,12 @@ export class TraceAPI {
8288
public wrapSpanContext = wrapSpanContext;
8389

8490
public isSpanContextValid = isSpanContextValid;
91+
92+
public getSpan = getSpan;
93+
94+
public getSpanContext = getSpanContext;
95+
96+
public setSpan = setSpan;
97+
98+
public setSpanContext = setSpanContext;
8599
}

src/baggage/index.ts

+23
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,17 @@ 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';
2123

2224
export * from './Baggage';
2325
export * from './Entry';
2426

27+
/**
28+
* Baggage key
29+
*/
30+
const BAGGAGE_KEY = createContextKey('OpenTelemetry Baggage Key');
31+
2532
/**
2633
* Create a new Baggage with optional entries
2734
*
@@ -33,6 +40,22 @@ export function createBaggage(
3340
return new BaggageImpl(new Map(Object.entries(entries)));
3441
}
3542

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

src/context/context.ts

-108
Original file line numberDiff line numberDiff line change
@@ -15,114 +15,6 @@
1515
*/
1616

1717
import { Context } from './types';
18-
import { Baggage, Span, SpanContext } from '../';
19-
import { NonRecordingSpan } from '../trace/NonRecordingSpan';
20-
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 NonRecordingSpan 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 NonRecordingSpan(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)?.spanContext();
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-
}
12618

12719
/** Get a key to uniquely identify a context value */
12820
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 { NonRecordingSpan } from './NonRecordingSpan';
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 { NonRecordingSpan } from './NonRecordingSpan';
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 NonRecordingSpan(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)?.spanContext();
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 { NonRecordingSpan } from '../../src/trace/NonRecordingSpan';
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.spanContext().traceId === parent.traceId);
5656
assert(span.spanContext().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)