Skip to content

Commit

Permalink
chore: remove usages of Span constructor (#2458)
Browse files Browse the repository at this point in the history
  • Loading branch information
david-luna authored Oct 2, 2024
1 parent 138c6c5 commit 37009ad
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 52 deletions.
4 changes: 2 additions & 2 deletions packages/baggage-span-processor/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
* limitations under the License.
*/

export * from './baggage-span-processor';
export * from './types';
export { BaggageSpanProcessor } from './baggage-span-processor';
export { ALLOW_ALL_BAGGAGE_KEYS, BaggageKeyPredicate } from './types';
46 changes: 16 additions & 30 deletions packages/baggage-span-processor/test/baggage-span-processor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@

import { BaggageSpanProcessor } from '../src/baggage-span-processor';
import { ALLOW_ALL_BAGGAGE_KEYS } from '../src/types';
import {
propagation,
ROOT_CONTEXT,
SpanKind,
TraceFlags,
} from '@opentelemetry/api';
import { propagation, ROOT_CONTEXT, SpanKind } from '@opentelemetry/api';
import { BasicTracerProvider, Span } from '@opentelemetry/sdk-trace-base';
import { expect } from 'expect';

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

beforeEach(() => {
span = new Span(
new BasicTracerProvider().getTracer('baggage-testing'),
ROOT_CONTEXT,
const tracer = new BasicTracerProvider().getTracer('baggage-testing');
span = tracer.startSpan(
'Edward W. Span',
{
traceId: 'e4cda95b652f4a1592b449d5929fda1b',
spanId: '7e0c63257de34c92',
traceFlags: TraceFlags.SAMPLED,
kind: SpanKind.SERVER,
},
SpanKind.SERVER
);
ROOT_CONTEXT
) as Span;
});

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

beforeEach(() => {
span = new Span(
new BasicTracerProvider().getTracer('baggage-testing'),
ROOT_CONTEXT,
const tracer = new BasicTracerProvider().getTracer('baggage-testing');
span = tracer.startSpan(
'Edward W. Span',
{
traceId: 'e4cda95b652f4a1592b449d5929fda1b',
spanId: '7e0c63257de34c92',
traceFlags: TraceFlags.SAMPLED,
kind: SpanKind.SERVER,
},
SpanKind.SERVER
);
ROOT_CONTEXT
) as Span;
});

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

beforeEach(() => {
span = new Span(
new BasicTracerProvider().getTracer('baggage-testing'),
ROOT_CONTEXT,
const tracer = new BasicTracerProvider().getTracer('baggage-testing');
span = tracer.startSpan(
'Edward W. Span',
{
traceId: 'e4cda95b652f4a1592b449d5929fda1b',
spanId: '7e0c63257de34c92',
traceFlags: TraceFlags.SAMPLED,
kind: SpanKind.SERVER,
},
SpanKind.SERVER
);
ROOT_CONTEXT
) as Span;
});

it('should only add baggage entries that match filter', () => {
Expand Down
4 changes: 2 additions & 2 deletions plugins/node/opentelemetry-instrumentation-dns/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
* limitations under the License.
*/

export * from './instrumentation';
export * from './types';
export { DnsInstrumentation } from './instrumentation';
export { DnsInstrumentationConfig, IgnoreMatcher } from './types';
6 changes: 3 additions & 3 deletions plugins/node/opentelemetry-instrumentation-dns/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

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

span.setAttributes(attributes);

Expand Down Expand Up @@ -70,7 +70,7 @@ export const setLookupAttributes = (
address: string | dns.LookupAddress[] | dns.LookupAddress,
family?: number
) => {
const attributes = {} as SpanAttributes;
const attributes = {} as Attributes;
const isObject = typeof address === 'object';
let addresses = address;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
* limitations under the License.
*/

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

describe('setError()', () => {
it('should have error attributes', () => {
// Prepare
const errorMessage = 'test error';
const span = new Span(
new BasicTracerProvider().getTracer('default'),
ROOT_CONTEXT,
'test',
{ spanId: '', traceId: '', traceFlags: TraceFlags.NONE },
SpanKind.INTERNAL
);
const span = {
setAttributes(attrs) {},
setStatus(status) {},
} as Span;
const mockSpan = sinon.mock(span);

mockSpan.expects('setAttributes').withExactArgs({
[AttributeNames.DNS_ERROR_NAME]: 'Error',
[AttributeNames.DNS_ERROR_MESSAGE]: errorMessage,
});
mockSpan.expects('setStatus').withExactArgs({
code: SpanStatusCode.ERROR,
message: errorMessage,
});

// Act
utils.setError(new Error(errorMessage), span);
const attributes = span.attributes;
assert.strictEqual(
attributes[AttributeNames.DNS_ERROR_MESSAGE],
errorMessage
);
assert.ok(attributes[AttributeNames.DNS_ERROR_NAME]);

// Assert
mockSpan.verify();
});
});
});

0 comments on commit 37009ad

Please sign in to comment.