Skip to content

Commit

Permalink
feat(sdk): added SpanHandle & NoopSpanHandle types (#1374)
Browse files Browse the repository at this point in the history
- added support for TS e.g. `instana.currentSpan().getTraceId()`
  • Loading branch information
kirrg001 authored Oct 10, 2024
1 parent b618268 commit 5e762a3
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
6 changes: 3 additions & 3 deletions packages/collector/src/types/shared.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { CollectorConfig } from './collector';
import { GenericLogger, InstanaBaseSpan } from '@instana/core/src/core';
import { GenericLogger, SpanHandle, NoopSpanHandle } from '@instana/core/src/core';

export interface Init {
currentSpan(): { span: InstanaBaseSpan };
currentSpan(): SpanHandle | NoopSpanHandle;
isTracing(): boolean;
isConnected(): boolean;
setLogger(logger: GenericLogger): void;
Expand All @@ -15,7 +15,7 @@ export interface Init {

export type InitFunction = {
(config?: CollectorConfig): Init;
currentSpan(): { span: InstanaBaseSpan };
currentSpan(): SpanHandle | NoopSpanHandle;
isTracing(): boolean;
isConnected(): boolean;
setLogger(logger: GenericLogger): void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ app.get('/request', async (req, res) => {
throw new Error('No current span available.');
}

if (!currentSpan.getTraceId()) {
throw new Error('getTraceId fn not available.');
}

await fetch(`http://127.0.0.1:${agentPort}`);
res.json({ success: true });
});
Expand Down
63 changes: 63 additions & 0 deletions packages/core/src/core.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,66 @@ export interface InstanaBaseSpan {
/** function to perform cleanup */
cleanup?: () => void;
}

export interface SpanHandle {
span: InstanaBaseSpan;

getTraceId(): string | null;
getSpanId(): string | null;
getParentSpanId(): string | null;
getName(): string | null;

isEntrySpan(): boolean;
isExitSpan(): boolean;
isIntermediateSpan(): boolean;

getTimestamp(): number;
getDuration(): number;
getErrorCount(): number;

getCorrelationId(): string | null;
setCorrelationId(correlationId: string): void;

getCorrelationType(): string | null;
setCorrelationType(correlationType: string): void;

annotate(path: string | string[], value: any): void;

markAsErroneous(errorMessage?: string, errorMessagePath?: string | string[]): void;
markAsNonErroneous(errorMessagePath?: string | string[]): void;

disableAutoEnd(): void;
end(errorCount?: boolean | number): void;
cancel(): void;
}

export interface NoopSpanHandle {
span: undefined;
getTraceId(): null;
getSpanId(): null;
getParentSpanId(): null;
getName(): null;

isEntrySpan(): boolean;
isExitSpan(): boolean;
isIntermediateSpan(): boolean;

getTimestamp(): number;
getDuration(): number;
getErrorCount(): number;

getCorrelationId(): null;
setCorrelationId(correlationId: string): void;

getCorrelationType(): null;
setCorrelationType(correlationType: string): void;

annotate(path: string | string[], value: any): void;

markAsErroneous(errorMessage?: string, errorMessagePath?: string | string[]): void;
markAsNonErroneous(errorMessagePath?: string | string[]): void;

disableAutoEnd(): void;
end(errorCount?: boolean | number): void;
cancel(): void;
}

0 comments on commit 5e762a3

Please sign in to comment.