Skip to content

Commit

Permalink
fix: Span being incompatible with MethodDecorator type
Browse files Browse the repository at this point in the history
  • Loading branch information
mertalev committed Feb 23, 2024
1 parent 1f46ee4 commit 75dbf0e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
14 changes: 14 additions & 0 deletions src/tracing/decorators/span.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { Span } from './span';

const TestDecoratorThatSetsMetadata = () => SetMetadata('some-metadata', true);

const symbol = Symbol('testSymbol');

class TestSpan {
@Span()
singleSpan() { }
Expand All @@ -26,6 +28,9 @@ class TestSpan {
@Span()
@TestDecoratorThatSetsMetadata()
metadata() { }

@Span()
[symbol]() { }
}

describe('Span', () => {
Expand Down Expand Up @@ -114,4 +119,13 @@ describe('Span', () => {
time: expect.anything(),
});
});

it('should handle symbols', () => {
instance[symbol]();

const spans = traceExporter.getFinishedSpans();

expect(spans).toHaveLength(1);
expect(spans.map(span => span.name)).toEqual(['TestSpan.Symbol(testSymbol)']);
});
});
4 changes: 2 additions & 2 deletions src/tracing/decorators/span.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ const recordException = (span: ApiSpan, error: any) => {
};

export function Span(name?: string, options: SpanOptions = {}) {
return (target: any, propertyKey: string, propertyDescriptor: PropertyDescriptor) => {
return (target: any, propertyKey: PropertyKey, propertyDescriptor: PropertyDescriptor) => {
const originalFunction = propertyDescriptor.value;
const wrappedFunction = function PropertyDescriptor(...args: any[]) {
const tracer = trace.getTracer('default');
const spanName = name || `${target.constructor.name}.${propertyKey}`;
const spanName = name || `${target.constructor.name}.${String(propertyKey)}`;

return tracer.startActiveSpan(spanName, options, span => {
if (originalFunction.constructor.name === 'AsyncFunction') {
Expand Down

0 comments on commit 75dbf0e

Please sign in to comment.