Skip to content

Commit

Permalink
fix last r nav test and lint
Browse files Browse the repository at this point in the history
  • Loading branch information
krystofwoldrich committed May 3, 2024
1 parent e726ea3 commit f40c974
Show file tree
Hide file tree
Showing 13 changed files with 196 additions and 188 deletions.
2 changes: 1 addition & 1 deletion src/js/profiling/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { logger, uuid4 } from '@sentry/utils';
import { Platform } from 'react-native';

import { isHermesEnabled } from '../utils/environment';
import { isCurrentlyActiveSpan, isRootSpan } from '../utils/span';
import { isRootSpan } from '../utils/span';
import { NATIVE } from '../wrapper';
import { PROFILE_QUEUE } from './cache';
import { MAX_PROFILE_DURATION_MS } from './constants';
Expand Down
13 changes: 7 additions & 6 deletions src/js/tracing/onSpanEndUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,11 @@ export const cancelInBackground = (client: Client, span: Span): void => {
}
});

subscription && client.on('spanEnd', (endedSpan: Span) => {
if (endedSpan === span) {
logger.debug(`Removing AppState listener for ${spanToJSON(span).op} transaction.`);
subscription && subscription.remove && subscription.remove();
}
});
subscription &&
client.on('spanEnd', (endedSpan: Span) => {
if (endedSpan === span) {
logger.debug(`Removing AppState listener for ${spanToJSON(span).op} transaction.`);
subscription && subscription.remove && subscription.remove();
}
});
};
4 changes: 1 addition & 3 deletions src/js/tracing/reactnavigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ import { RN_GLOBAL_OBJ } from '../utils/worldwide';
import { NATIVE } from '../wrapper';
import type { OnConfirmRoute, TransactionCreator } from './routingInstrumentation';
import { InternalRoutingInstrumentation } from './routingInstrumentation';
import {
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
} from './semanticAttributes';
import { SEMANTIC_ATTRIBUTE_SENTRY_SOURCE } from './semanticAttributes';
import { manualInitialDisplaySpans, startTimeToInitialDisplaySpan } from './timetodisplay';
import type { BeforeNavigate } from './types';

Expand Down
1 change: 0 additions & 1 deletion src/js/tracing/semanticAttributes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// TODO: Export used RN Attributes and re-export JS


export {
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
Expand Down
8 changes: 7 additions & 1 deletion src/js/tracing/stalltracking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,13 @@ export class StallTrackingInstrumentation implements Integration {

if (!statsOnFinish) {
if (typeof endTimestamp !== 'undefined') {
logger.log('[StallTracking] Stall measurements not added due to `endTimestamp` not being close to now.', 'endTimestamp', endTimestamp, 'now', timestampInSeconds());
logger.log(
'[StallTracking] Stall measurements not added due to `endTimestamp` not being close to now.',
'endTimestamp',
endTimestamp,
'now',
timestampInSeconds(),
);
} else if (trimEnd) {
logger.log(
'[StallTracking] Stall measurements not added due to `trimEnd` being set but we could not determine the stall measurements at that time.',
Expand Down
8 changes: 6 additions & 2 deletions src/js/tracing/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { SEMANTIC_ATTRIBUTE_SENTRY_MEASUREMENT_UNIT, SEMANTIC_ATTRIBUTE_SENTRY_MEASUREMENT_VALUE, setMeasurement, spanToJSON } from '@sentry/core';
import {
SEMANTIC_ATTRIBUTE_SENTRY_MEASUREMENT_UNIT,
SEMANTIC_ATTRIBUTE_SENTRY_MEASUREMENT_VALUE,
setMeasurement,
spanToJSON,
} from '@sentry/core';
import type { MeasurementUnit, Span, TransactionSource } from '@sentry/types';
import { timestampInSeconds } from '@sentry/utils';

Expand Down Expand Up @@ -59,7 +64,6 @@ export function setSpanDurationAsMeasurement(name: string, span: Span): void {
setMeasurement(name, (spanEnd - spanStart) * 1000, 'millisecond');
}


/**
* Sets measurement on the give span.
*/
Expand Down
12 changes: 1 addition & 11 deletions src/js/utils/span.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
import { getActiveSpan, getRootSpan, SentrySpan, spanToJSON } from '@sentry/core';
import { getRootSpan, SentrySpan } from '@sentry/core';
import type { Span } from '@sentry/types';

/**
*
*/
export function isCurrentlyActiveSpan(span: Span): boolean {
const spanId = spanToJSON(span).span_id;
const activeSpan = getActiveSpan();
const activeSpanId = activeSpan ? spanToJSON(activeSpan).span_id : undefined;
return span === getActiveSpan();
}

/**
*
*/
Expand Down
51 changes: 36 additions & 15 deletions test/profiling/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,17 @@ describe('profiling integration', () => {
(span: Span) => {
addIntegrationAndForceSetupOnce(new HermesProfiling());
return span;
});
},
);

transaction.end();
jest.runAllTimers();

expectEnvelopeToContainProfile(mock.transportSendMock.mock.lastCall?.[0], 'test-name', spanToJSON(transaction).trace_id);
expectEnvelopeToContainProfile(
mock.transportSendMock.mock.lastCall?.[0],
'test-name',
spanToJSON(transaction).trace_id,
);
});

describe('environment', () => {
Expand Down Expand Up @@ -99,7 +104,7 @@ describe('profiling integration', () => {
test('should use default environment for transaction and profile', () => {
mock = initTestClient();

Sentry.startSpan({ name: 'test-name' }, () => { });
Sentry.startSpan({ name: 'test-name' }, () => {});

jest.runAllTimers();

Expand Down Expand Up @@ -162,7 +167,7 @@ describe('profiling integration', () => {
nativeProfile: createMockMinimalValidAppleProfile(),
});

const transaction = Sentry.startSpan({ name: 'test-name' }, (span) => span);
const transaction = Sentry.startSpan({ name: 'test-name' }, span => span);

jest.runAllTimers();

Expand Down Expand Up @@ -213,7 +218,7 @@ describe('profiling integration', () => {
androidProfile: createMockMinimalValidAndroidProfile(),
});

const transaction = Sentry.startSpan({ name: 'test-name' }, (span) => span);
const transaction = Sentry.startSpan({ name: 'test-name' }, span => span);

jest.runAllTimers();

Expand Down Expand Up @@ -248,23 +253,35 @@ describe('profiling integration', () => {
});

test('should create a new profile and add in to the transaction envelope', () => {
const transaction = Sentry.startSpan({ name: 'test-name' }, (span) => span);
const transaction = Sentry.startSpan({ name: 'test-name' }, span => span);

jest.runAllTimers();

expectEnvelopeToContainProfile(mock.transportSendMock.mock.lastCall?.[0], 'test-name', spanToJSON(transaction).trace_id);
expectEnvelopeToContainProfile(
mock.transportSendMock.mock.lastCall?.[0],
'test-name',
spanToJSON(transaction).trace_id,
);
});

test('should finish previous profile when a new transaction starts', () => {
const transaction1 = Sentry.startSpanManual({ name: 'test-name-1' }, (span) => span);
const transaction2 = Sentry.startSpanManual({ name: 'test-name-2' }, (span) => span);
const transaction1 = Sentry.startSpanManual({ name: 'test-name-1' }, span => span);
const transaction2 = Sentry.startSpanManual({ name: 'test-name-2' }, span => span);
transaction1.end();
transaction2.end();

jest.runAllTimers();

expectEnvelopeToContainProfile(mock.transportSendMock.mock.calls[0][0], 'test-name-1', spanToJSON(transaction1).trace_id);
expectEnvelopeToContainProfile(mock.transportSendMock.mock.calls[1][0], 'test-name-2', spanToJSON(transaction2).trace_id);
expectEnvelopeToContainProfile(
mock.transportSendMock.mock.calls[0][0],
'test-name-1',
spanToJSON(transaction1).trace_id,
);
expectEnvelopeToContainProfile(
mock.transportSendMock.mock.calls[1][0],
'test-name-2',
spanToJSON(transaction2).trace_id,
);
});

test('profile should start at the same time as transaction', () => {
Expand All @@ -281,7 +298,7 @@ describe('profiling integration', () => {
});

test('profile is only recorded until max duration is reached', () => {
const transaction = Sentry.startSpanManual({ name: 'test-name' }, (span) => span);
const transaction = Sentry.startSpanManual({ name: 'test-name' }, span => span);
jest.clearAllMocks();

jest.advanceTimersByTime(40 * 1e6);
Expand All @@ -292,20 +309,24 @@ describe('profiling integration', () => {
});

test('profile that reached max duration is sent', () => {
const transaction = Sentry.startSpanManual({ name: 'test-name' }, (span) => span);
const transaction = Sentry.startSpanManual({ name: 'test-name' }, span => span);

jest.advanceTimersByTime(40 * 1e6);

transaction.end();

jest.runAllTimers();

expectEnvelopeToContainProfile(mock.transportSendMock.mock.lastCall?.[0], 'test-name', spanToJSON(transaction).trace_id);
expectEnvelopeToContainProfile(
mock.transportSendMock.mock.lastCall?.[0],
'test-name',
spanToJSON(transaction).trace_id,
);
});

test('profile timeout is reset when transaction is finished', () => {
const integration = getCurrentHermesProfilingIntegration();
const transaction = Sentry.startSpanManual({ name: 'test-name' }, (span) => span);
const transaction = Sentry.startSpanManual({ name: 'test-name' }, span => span);

const timeoutAfterProfileStarted = integration._currentProfileTimeout;

Expand Down
6 changes: 4 additions & 2 deletions test/tracing/reactnativetracing.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import * as SentryBrowser from '@sentry/browser';
import type { Client, Event, Span } from '@sentry/types';
import type { Event, Span } from '@sentry/types';

import type { NativeAppStartResponse } from '../../src/js/NativeRNSentry';
import { RoutingInstrumentation } from '../../src/js/tracing/routingInstrumentation';
Expand Down Expand Up @@ -796,7 +796,9 @@ describe('ReactNativeTracing', () => {
op: 'different.op',
}),
);
expect(firstTransactionEvent!.timestamp).toBeGreaterThanOrEqual(spanToJSON(secondTransaction!).start_timestamp!);
expect(firstTransactionEvent!.timestamp).toBeGreaterThanOrEqual(
spanToJSON(secondTransaction!).start_timestamp!,
);
});

test('different UI event and same element finish first transaction with last span', () => {
Expand Down
8 changes: 1 addition & 7 deletions test/tracing/reactnavigation.stalltracking.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,7 @@ jest.mock('../../src/js/tracing/utils', () => ({
isNearToNow: jest.fn(),
}));

import {
getCurrentScope,
getGlobalScope,
getIsolationScope,
setCurrentClient,
startSpanManual,
} from '@sentry/core';
import { getCurrentScope, getGlobalScope, getIsolationScope, setCurrentClient, startSpanManual } from '@sentry/core';

import { ReactNativeTracing, ReactNavigationInstrumentation } from '../../src/js';
import { _addTracingExtensions } from '../../src/js/tracing/addTracingExtensions';
Expand Down
Loading

0 comments on commit f40c974

Please sign in to comment.