1
1
import {
2
2
AggregationCounts ,
3
+ DsnComponents ,
4
+ NewTransport ,
3
5
RequestSessionStatus ,
6
+ SdkMetadata ,
7
+ SentryRequestType ,
4
8
SessionAggregates ,
9
+ SessionEnvelope ,
5
10
SessionFlusherLike ,
6
- Transport ,
11
+ SessionItem ,
7
12
} from '@sentry/types' ;
8
- import { dropUndefinedKeys , logger } from '@sentry/utils' ;
13
+ import { createEnvelope , dropUndefinedKeys , dsnToString , logger } from '@sentry/utils' ;
9
14
10
15
import { IS_DEBUG_BUILD } from './flags' ;
11
16
import { getCurrentHub } from './hub' ;
@@ -15,6 +20,35 @@ type ReleaseHealthAttributes = {
15
20
release : string ;
16
21
} ;
17
22
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
+
18
52
/**
19
53
* @inheritdoc
20
54
*/
@@ -24,9 +58,15 @@ export class SessionFlusher implements SessionFlusherLike {
24
58
private _sessionAttrs : ReleaseHealthAttributes ;
25
59
private _intervalId : ReturnType < typeof setInterval > ;
26
60
private _isEnabled : boolean = true ;
27
- private _transport : Transport ;
61
+ private _transport : NewTransport ;
28
62
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
+ ) {
30
70
this . _transport = transport ;
31
71
// Call to setInterval, so that flush is called every 60 seconds
32
72
this . _intervalId = setInterval ( ( ) => this . flush ( ) , this . flushTimeout * 1000 ) ;
@@ -35,11 +75,8 @@ export class SessionFlusher implements SessionFlusherLike {
35
75
36
76
/** Sends session aggregates to Transport */
37
77
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 => {
43
80
IS_DEBUG_BUILD && logger . error ( 'Error while sending session:' , reason ) ;
44
81
} ) ;
45
82
}
0 commit comments