Skip to content

Commit 054ef07

Browse files
committed
update sessionflusher
1 parent d7d14d5 commit 054ef07

File tree

2 files changed

+61
-15
lines changed

2 files changed

+61
-15
lines changed

packages/hub/src/sessionflusher.ts

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import {
22
AggregationCounts,
3+
DsnComponents,
4+
NewTransport,
35
RequestSessionStatus,
6+
SdkMetadata,
7+
SentryRequestType,
48
SessionAggregates,
9+
SessionEnvelope,
510
SessionFlusherLike,
6-
Transport,
11+
SessionItem,
712
} from '@sentry/types';
8-
import { dropUndefinedKeys, logger } from '@sentry/utils';
13+
import { createEnvelope, dropUndefinedKeys, dsnToString, logger } from '@sentry/utils';
914

1015
import { IS_DEBUG_BUILD } from './flags';
1116
import { getCurrentHub } from './hub';
@@ -15,6 +20,35 @@ type ReleaseHealthAttributes = {
1520
release: string;
1621
};
1722

23+
/**
24+
* Creates an envelope from a Session
25+
*
26+
* This is copied from @sentry/core/src/request.ts
27+
* TODO(v7): Unify session envelope creation
28+
**/
29+
export function createSessionEnvelope(
30+
sessionAggregates: SessionAggregates,
31+
dsn: DsnComponents,
32+
metadata: SdkMetadata,
33+
tunnel?: string,
34+
): SessionEnvelope {
35+
const sdkInfo = metadata;
36+
const envelopeHeaders = {
37+
sent_at: new Date().toISOString(),
38+
...(sdkInfo && { sdk: sdkInfo }),
39+
...(!!tunnel && { dsn: dsnToString(dsn) }),
40+
};
41+
42+
// I know this is hacky but we don't want to add `sessions` to request type since it's never rate limited
43+
const type = 'aggregates' in sessionAggregates ? ('sessions' as SentryRequestType) : 'session';
44+
45+
// TODO (v7) Have to cast type because envelope items do not accept a `SentryRequestType`
46+
const envelopeItem = [{ type } as { type: 'session' | 'sessions' }, sessionAggregates] as SessionItem;
47+
const envelope = createEnvelope<SessionEnvelope>(envelopeHeaders, [envelopeItem]);
48+
49+
return envelope;
50+
}
51+
1852
/**
1953
* @inheritdoc
2054
*/
@@ -24,9 +58,15 @@ export class SessionFlusher implements SessionFlusherLike {
2458
private _sessionAttrs: ReleaseHealthAttributes;
2559
private _intervalId: ReturnType<typeof setInterval>;
2660
private _isEnabled: boolean = true;
27-
private _transport: Transport;
61+
private _transport: NewTransport;
2862

29-
public constructor(transport: Transport, attrs: ReleaseHealthAttributes) {
63+
public constructor(
64+
transport: NewTransport,
65+
attrs: ReleaseHealthAttributes,
66+
private readonly _dsn: DsnComponents,
67+
private readonly _metadata: SdkMetadata,
68+
private readonly _tunnel?: string,
69+
) {
3070
this._transport = transport;
3171
// Call to setInterval, so that flush is called every 60 seconds
3272
this._intervalId = setInterval(() => this.flush(), this.flushTimeout * 1000);
@@ -35,11 +75,8 @@ export class SessionFlusher implements SessionFlusherLike {
3575

3676
/** Sends session aggregates to Transport */
3777
public sendSessionAggregates(sessionAggregates: SessionAggregates): void {
38-
if (!this._transport.sendSession) {
39-
IS_DEBUG_BUILD && logger.warn("Dropping session because custom transport doesn't implement sendSession");
40-
return;
41-
}
42-
void this._transport.sendSession(sessionAggregates).then(null, reason => {
78+
const env = createSessionEnvelope(sessionAggregates, this._dsn, this._metadata, this._tunnel);
79+
void this._transport.send(env).then(null, reason => {
4380
IS_DEBUG_BUILD && logger.error('Error while sending session:', reason);
4481
});
4582
}

packages/node/src/client.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { BaseClient, NewTransport, Scope, SDK_VERSION } from '@sentry/core';
1+
import { BaseClient, initAPIDetails, NewTransport, Scope, SDK_VERSION } from '@sentry/core';
22
import { SessionFlusher } from '@sentry/hub';
3-
import { Event, EventHint, Severity, SeverityLevel, Transport } from '@sentry/types';
3+
import { Event, EventHint, Severity, SeverityLevel } from '@sentry/types';
44
import { logger, resolvedSyncPromise, stackParserFromOptions } from '@sentry/utils';
55

66
import { eventFromMessage, eventFromUnknownInput } from './eventbuilder';
@@ -99,10 +99,19 @@ export class NodeClient extends BaseClient<NodeOptions> {
9999
if (!release) {
100100
IS_DEBUG_BUILD && logger.warn('Cannot initialise an instance of SessionFlusher if no release is provided!');
101101
} else {
102-
this._sessionFlusher = new SessionFlusher(this.getTransport(), {
103-
release,
104-
environment,
105-
});
102+
if (this._options.dsn) {
103+
const api = initAPIDetails(this._options.dsn, this._options._metadata, this._options.tunnel);
104+
this._sessionFlusher = new SessionFlusher(
105+
this.getTransport(),
106+
{
107+
release,
108+
environment,
109+
},
110+
api.dsn,
111+
api.metadata,
112+
api.tunnel,
113+
);
114+
}
106115
}
107116
}
108117

0 commit comments

Comments
 (0)