Skip to content
Merged
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
30 changes: 17 additions & 13 deletions src/libs/telemetry/activeSpans.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import type {SpanAttributeValue, StartSpanOptions} from '@sentry/core';
import * as Sentry from '@sentry/react-native';
import {spanToJSON} from '@sentry/react-native';
import {AppState} from 'react-native';
import Log from '@libs/Log';
import CONST from '@src/CONST';

const activeSpans = new Map<string, ReturnType<typeof Sentry.startInactiveSpan>>();
type ActiveSpanEntry = {
span: ReturnType<typeof Sentry.startInactiveSpan>;
startTime: number;
};

const activeSpans = new Map<string, ActiveSpanEntry>();

type StartSpanExtraOptions = Partial<{
/**
Expand All @@ -32,21 +36,21 @@ function startSpan(spanId: string, options: StartSpanOptions, extraOptions: Star
if (extraOptions.minDuration) {
span.setAttribute(CONST.TELEMETRY.ATTRIBUTE_MIN_DURATION, extraOptions.minDuration);
}
activeSpans.set(spanId, span);
activeSpans.set(spanId, {span, startTime: performance.now()});

return span;
}

function endSpan(spanId: string) {
const span = activeSpans.get(spanId);
const entry = activeSpans.get(spanId);

if (!span) {
if (!entry) {
Log.info(`[Sentry][${spanId}] Trying to end span but it does not exist`, undefined, {spanId, timestamp: Date.now()});
return;
}
const now = Date.now();
const startTimestamp = spanToJSON(span).start_timestamp;
const durationMs = Math.round(now - startTimestamp * 1000);
const {span, startTime} = entry;
const now = performance.now();
const durationMs = Math.round(now - startTime);
Log.info(`[Sentry][${spanId}] Ending span (${durationMs}ms)`, undefined, {spanId, durationMs, timestamp: now});
span.setStatus({code: 1});
span.setAttribute(CONST.TELEMETRY.ATTRIBUTE_FINISHED_MANUALLY, true);
Expand All @@ -55,15 +59,15 @@ function endSpan(spanId: string) {
}

function cancelSpan(spanId: string) {
const span = activeSpans.get(spanId);
if (!span) {
const entry = activeSpans.get(spanId);
if (!entry) {
return;
}
Log.info(`[Sentry][${spanId}] Canceling span`, undefined, {spanId, timestamp: Date.now()});
span.setAttribute(CONST.TELEMETRY.ATTRIBUTE_CANCELED, true);
entry.span.setAttribute(CONST.TELEMETRY.ATTRIBUTE_CANCELED, true);
// In Sentry there are only OK or ERROR status codes.
// We treat canceled spans as OK, so we can properly track spans that are not finished at all (their status would be different)
span.setStatus({code: 1});
entry.span.setStatus({code: 1});
endSpan(spanId);
}

Expand Down Expand Up @@ -95,7 +99,7 @@ function tryEndSpan(spanId: string): boolean {
}

function getSpan(spanId: string) {
return activeSpans.get(spanId);
return activeSpans.get(spanId)?.span;
}

function endSpanWithAttributes(spanId: string, attributes: Record<string, SpanAttributeValue>) {
Expand Down
Loading