Skip to content

Commit

Permalink
Merge pull request #465 from mertalev/fix/span-type
Browse files Browse the repository at this point in the history
fix: `Span` being incompatible with `MethodDecorator` type
  • Loading branch information
pragmaticivan authored Feb 24, 2024
2 parents 1f46ee4 + 75dbf0e commit 388bd50
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 388bd50

Please sign in to comment.