Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: ensure onStart is called with a writeable span #1621

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,20 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { XMLHttpRequestPlugin } from '../src';
import { Span } from '@opentelemetry/api';
import { HttpAttribute } from '@opentelemetry/semantic-conventions';
import { ReadableSpan, SpanProcessor } from '@opentelemetry/tracing';
import { WebTracerProvider } from '@opentelemetry/web';
import { XMLHttpRequestPlugin } from '../src';
import assert = require('assert');
import { HttpAttribute } from '@opentelemetry/semantic-conventions';

class TestSpanProcessor implements SpanProcessor {
spans: ReadableSpan[] = [];

forceFlush(): Promise<void> {
return Promise.resolve();
}
onStart(span: ReadableSpan): void {}
onStart(span: Span): void {}
shutdown(): Promise<void> {
return Promise.resolve();
}
Expand Down
5 changes: 3 additions & 2 deletions packages/opentelemetry-tracing/src/MultiSpanProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
*/

import { globalErrorHandler } from '@opentelemetry/core';
import { SpanProcessor } from './SpanProcessor';
import { ReadableSpan } from './export/ReadableSpan';
import { Span } from './Span';
import { SpanProcessor } from './SpanProcessor';

/**
* Implementation of the {@link SpanProcessor} that simply forwards all
Expand Down Expand Up @@ -45,7 +46,7 @@ export class MultiSpanProcessor implements SpanProcessor {
});
}

onStart(span: ReadableSpan): void {
onStart(span: Span): void {
for (const spanProcessor of this._spanProcessors) {
spanProcessor.onStart(span);
}
Expand Down
5 changes: 3 additions & 2 deletions packages/opentelemetry-tracing/src/NoopSpanProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
* limitations under the License.
*/

import { SpanProcessor } from './SpanProcessor';
import { ReadableSpan } from './export/ReadableSpan';
import { Span } from './Span';
import { SpanProcessor } from './SpanProcessor';

/** No-op implementation of SpanProcessor */
export class NoopSpanProcessor implements SpanProcessor {
onStart(span: ReadableSpan): void {}
onStart(span: Span): void {}
onEnd(span: ReadableSpan): void {}
shutdown(): Promise<void> {
return Promise.resolve();
Expand Down
5 changes: 3 additions & 2 deletions packages/opentelemetry-tracing/src/SpanProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

import { ReadableSpan } from './export/ReadableSpan';
import { Span } from './Span';

/**
* SpanProcessor is the interface Tracer SDK uses to allow synchronous hooks
Expand All @@ -27,11 +28,11 @@ export interface SpanProcessor {
forceFlush(): Promise<void>;

/**
* Called when a {@link ReadableSpan} is started, if the `span.isRecording()`
* Called when a {@link Span} is started, if the `span.isRecording()`
* returns true.
* @param span the Span that just started.
*/
onStart(span: ReadableSpan): void;
onStart(span: Span): void;

/**
* Called when a {@link ReadableSpan} is ended, if the `span.isRecording()`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import { context, suppressInstrumentation } from '@opentelemetry/api';
import { ExportResult, unrefTimer } from '@opentelemetry/core';
import { Span } from '../Span';
import { SpanProcessor } from '../SpanProcessor';
import { BufferConfig } from '../types';
import { ReadableSpan } from './ReadableSpan';
Expand Down Expand Up @@ -54,7 +55,7 @@ export class BatchSpanProcessor implements SpanProcessor {
}

// does nothing.
onStart(span: ReadableSpan): void {}
onStart(span: Span): void {}

onEnd(span: ReadableSpan): void {
if (this._isShutdown) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
* limitations under the License.
*/

import { context, suppressInstrumentation } from '@opentelemetry/api';
import { Span } from '../Span';
import { SpanProcessor } from '../SpanProcessor';
import { SpanExporter } from './SpanExporter';
import { ReadableSpan } from './ReadableSpan';
import { context, suppressInstrumentation } from '@opentelemetry/api';
import { SpanExporter } from './SpanExporter';

/**
* An implementation of the {@link SpanProcessor} that converts the {@link Span}
Expand All @@ -37,7 +38,7 @@ export class SimpleSpanProcessor implements SpanProcessor {
}

// does nothing.
onStart(span: ReadableSpan): void {}
onStart(span: Span): void {}

onEnd(span: ReadableSpan): void {
if (this._isShutdown) {
Expand Down
82 changes: 73 additions & 9 deletions packages/opentelemetry-tracing/test/Span.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@
* limitations under the License.
*/

import { ExceptionAttribute } from '@opentelemetry/semantic-conventions';
import * as assert from 'assert';
import {
SpanKind,
CanonicalCode,
TraceFlags,
SpanContext,
LinkContext,
Exception,
LinkContext,
SpanContext,
SpanKind,
TraceFlags,
} from '@opentelemetry/api';
import { BasicTracerProvider, Span } from '../src';
import {
hrTime,
hrTimeToNanoseconds,
hrTimeDuration,
hrTimeToMilliseconds,
hrTimeToNanoseconds,
NoopLogger,
hrTimeDuration,
} from '@opentelemetry/core';
import { ExceptionAttribute } from '@opentelemetry/semantic-conventions';
import * as assert from 'assert';
import { BasicTracerProvider, Span, SpanProcessor } from '../src';

const performanceTimeOrigin = hrTime();

Expand Down Expand Up @@ -412,6 +412,70 @@ describe('Span', () => {
assert.strictEqual(span.ended, true);
});

describe('span processor', () => {
it('should call onStart synchronously when span is started', () => {
let started = false;
const processor: SpanProcessor = {
onStart: () => {
started = true;
},
forceFlush: () => Promise.resolve(),
onEnd() {},
shutdown: () => Promise.resolve(),
};

const provider = new BasicTracerProvider({
logger: new NoopLogger(),
});

provider.addSpanProcessor(processor);

provider.getTracer('default').startSpan('test');
assert.ok(started);
});

it('should call onEnd synchronously when span is ended', () => {
let ended = false;
const processor: SpanProcessor = {
onStart: () => {},
forceFlush: () => Promise.resolve(),
onEnd() {
ended = true;
},
shutdown: () => Promise.resolve(),
};

const provider = new BasicTracerProvider({
logger: new NoopLogger(),
});

provider.addSpanProcessor(processor);

provider.getTracer('default').startSpan('test').end();
assert.ok(ended);
});

it('should call onStart with a writeable span', () => {
const processor: SpanProcessor = {
onStart: span => {
span.setAttribute('attr', true);
},
forceFlush: () => Promise.resolve(),
onEnd() {},
shutdown: () => Promise.resolve(),
};

const provider = new BasicTracerProvider({
logger: new NoopLogger(),
});

provider.addSpanProcessor(processor);

const s = provider.getTracer('default').startSpan('test') as Span;
assert.ok(s.attributes.attr);
});
});

describe('recordException', () => {
const invalidExceptions: any[] = [
1,
Expand Down