Skip to content

Commit 37009ad

Browse files
authored
chore: remove usages of Span constructor (#2458)
1 parent 138c6c5 commit 37009ad

File tree

5 files changed

+44
-52
lines changed

5 files changed

+44
-52
lines changed

packages/baggage-span-processor/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@
1414
* limitations under the License.
1515
*/
1616

17-
export * from './baggage-span-processor';
18-
export * from './types';
17+
export { BaggageSpanProcessor } from './baggage-span-processor';
18+
export { ALLOW_ALL_BAGGAGE_KEYS, BaggageKeyPredicate } from './types';

packages/baggage-span-processor/test/baggage-span-processor.test.ts

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,7 @@
1616

1717
import { BaggageSpanProcessor } from '../src/baggage-span-processor';
1818
import { ALLOW_ALL_BAGGAGE_KEYS } from '../src/types';
19-
import {
20-
propagation,
21-
ROOT_CONTEXT,
22-
SpanKind,
23-
TraceFlags,
24-
} from '@opentelemetry/api';
19+
import { propagation, ROOT_CONTEXT, SpanKind } from '@opentelemetry/api';
2520
import { BasicTracerProvider, Span } from '@opentelemetry/sdk-trace-base';
2621
import { expect } from 'expect';
2722

@@ -39,17 +34,14 @@ describe('BaggageSpanProcessor with all keys filter', () => {
3934
let span: Span;
4035

4136
beforeEach(() => {
42-
span = new Span(
43-
new BasicTracerProvider().getTracer('baggage-testing'),
44-
ROOT_CONTEXT,
37+
const tracer = new BasicTracerProvider().getTracer('baggage-testing');
38+
span = tracer.startSpan(
4539
'Edward W. Span',
4640
{
47-
traceId: 'e4cda95b652f4a1592b449d5929fda1b',
48-
spanId: '7e0c63257de34c92',
49-
traceFlags: TraceFlags.SAMPLED,
41+
kind: SpanKind.SERVER,
5042
},
51-
SpanKind.SERVER
52-
);
43+
ROOT_CONTEXT
44+
) as Span;
5345
});
5446

5547
it('onStart adds current Baggage entries to a span as attributes', () => {
@@ -91,17 +83,14 @@ describe('BaggageSpanProcessor with startWith key filter', () => {
9183
let span: Span;
9284

9385
beforeEach(() => {
94-
span = new Span(
95-
new BasicTracerProvider().getTracer('baggage-testing'),
96-
ROOT_CONTEXT,
86+
const tracer = new BasicTracerProvider().getTracer('baggage-testing');
87+
span = tracer.startSpan(
9788
'Edward W. Span',
9889
{
99-
traceId: 'e4cda95b652f4a1592b449d5929fda1b',
100-
spanId: '7e0c63257de34c92',
101-
traceFlags: TraceFlags.SAMPLED,
90+
kind: SpanKind.SERVER,
10291
},
103-
SpanKind.SERVER
104-
);
92+
ROOT_CONTEXT
93+
) as Span;
10594
});
10695

10796
it('should only add baggage entries that match filter', () => {
@@ -131,17 +120,14 @@ describe('BaggageSpanProcessor with regex key filter', () => {
131120
let span: Span;
132121

133122
beforeEach(() => {
134-
span = new Span(
135-
new BasicTracerProvider().getTracer('baggage-testing'),
136-
ROOT_CONTEXT,
123+
const tracer = new BasicTracerProvider().getTracer('baggage-testing');
124+
span = tracer.startSpan(
137125
'Edward W. Span',
138126
{
139-
traceId: 'e4cda95b652f4a1592b449d5929fda1b',
140-
spanId: '7e0c63257de34c92',
141-
traceFlags: TraceFlags.SAMPLED,
127+
kind: SpanKind.SERVER,
142128
},
143-
SpanKind.SERVER
144-
);
129+
ROOT_CONTEXT
130+
) as Span;
145131
});
146132

147133
it('should only add baggage entries that match filter', () => {

plugins/node/opentelemetry-instrumentation-dns/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@
1414
* limitations under the License.
1515
*/
1616

17-
export * from './instrumentation';
18-
export * from './types';
17+
export { DnsInstrumentation } from './instrumentation';
18+
export { DnsInstrumentationConfig, IgnoreMatcher } from './types';

plugins/node/opentelemetry-instrumentation-dns/src/utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { Span, SpanStatusCode, SpanAttributes } from '@opentelemetry/api';
17+
import { Span, SpanStatusCode, Attributes } from '@opentelemetry/api';
1818
import { AttributeNames } from './enums/AttributeNames';
1919
import { AddressFamily } from './enums/AddressFamily';
2020
import * as dns from 'dns';
@@ -31,7 +31,7 @@ export const setError = (err: NodeJS.ErrnoException, span: Span) => {
3131
const attributes = {
3232
[AttributeNames.DNS_ERROR_MESSAGE]: message,
3333
[AttributeNames.DNS_ERROR_NAME]: name,
34-
} as SpanAttributes;
34+
} as Attributes;
3535

3636
span.setAttributes(attributes);
3737

@@ -70,7 +70,7 @@ export const setLookupAttributes = (
7070
address: string | dns.LookupAddress[] | dns.LookupAddress,
7171
family?: number
7272
) => {
73-
const attributes = {} as SpanAttributes;
73+
const attributes = {} as Attributes;
7474
const isObject = typeof address === 'object';
7575
let addresses = address;
7676

plugins/node/opentelemetry-instrumentation-dns/test/functionals/utils.test.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { diag, ROOT_CONTEXT, SpanKind, TraceFlags } from '@opentelemetry/api';
18-
import { BasicTracerProvider, Span } from '@opentelemetry/sdk-trace-base';
17+
import { diag, Span, SpanStatusCode } from '@opentelemetry/api';
1918
import * as assert from 'assert';
2019
import * as sinon from 'sinon';
2120
import { AttributeNames } from '../../src/enums/AttributeNames';
@@ -153,21 +152,28 @@ describe('Utility', () => {
153152

154153
describe('setError()', () => {
155154
it('should have error attributes', () => {
155+
// Prepare
156156
const errorMessage = 'test error';
157-
const span = new Span(
158-
new BasicTracerProvider().getTracer('default'),
159-
ROOT_CONTEXT,
160-
'test',
161-
{ spanId: '', traceId: '', traceFlags: TraceFlags.NONE },
162-
SpanKind.INTERNAL
163-
);
157+
const span = {
158+
setAttributes(attrs) {},
159+
setStatus(status) {},
160+
} as Span;
161+
const mockSpan = sinon.mock(span);
162+
163+
mockSpan.expects('setAttributes').withExactArgs({
164+
[AttributeNames.DNS_ERROR_NAME]: 'Error',
165+
[AttributeNames.DNS_ERROR_MESSAGE]: errorMessage,
166+
});
167+
mockSpan.expects('setStatus').withExactArgs({
168+
code: SpanStatusCode.ERROR,
169+
message: errorMessage,
170+
});
171+
172+
// Act
164173
utils.setError(new Error(errorMessage), span);
165-
const attributes = span.attributes;
166-
assert.strictEqual(
167-
attributes[AttributeNames.DNS_ERROR_MESSAGE],
168-
errorMessage
169-
);
170-
assert.ok(attributes[AttributeNames.DNS_ERROR_NAME]);
174+
175+
// Assert
176+
mockSpan.verify();
171177
});
172178
});
173179
});

0 commit comments

Comments
 (0)