-
Notifications
You must be signed in to change notification settings - Fork 407
Expand file tree
/
Copy pathexposures.js
More file actions
238 lines (206 loc) · 6.14 KB
/
Copy pathexposures.js
File metadata and controls
238 lines (206 loc) · 6.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
'use strict'
const {
EXPOSURES_ENDPOINT,
EVP_PAYLOAD_SIZE_LIMIT,
EVP_EVENT_SIZE_LIMIT,
} = require('../constants/constants')
const {
EVP_EVENT_PLATFORM_SUBDOMAIN,
EVP_PROXY_PATH_V2,
EVP_SUBDOMAIN_HEADER_NAME,
} = require('../../evp_proxy/constants')
const { joinEVPProxyPath } = require('../../evp_proxy/path')
const BaseFFEWriter = require('./base')
/**
* @typedef {object} ExposureRoute
* @property {URL} url - Route base URL
* @property {string} basePath - EVP base path
* @property {object} [headers] - Route-specific headers
* @property {import('node:https').Agent} [agent] - Optional HTTPS proxy agent
* @property {ExposureRoute} [fallback] - Optional direct fallback route
*/
/**
* @typedef {object} ExposureEvent
* @property {number} timestamp - Unix timestamp in milliseconds
* @property {object} allocation - Allocation information
* @property {string} allocation.key - Allocation key
* @property {object} flag - Flag information
* @property {string} flag.key - Flag key
* @property {object} variant - Variant information
* @property {string} variant.key - Variant key
* @property {object} subject - Subject (user/entity) information
* @property {string} subject.id - Subject identifier
* @property {string} [subject.type] - Subject type
* @property {object} [subject.attributes] - Additional subject attributes
*/
/**
* @typedef {object} ExposureContext
* @property {string} service - Service name
* @property {string} [version] - Service version
* @property {string} [env] - Service environment
*/
/**
* @typedef {object} ExposureEventPayload
* @property {ExposureContext} context - Service context metadata
* @property {ExposureEvent[]} exposures - Formatted exposure events
*/
/**
* Sends exposure events through the selected local EVP proxy or direct intake route.
*/
class ExposuresWriter extends BaseFFEWriter {
// Disabled until route selection resolves.
#enabled = false
#routeResolved = false
/** @type {ReturnType<typeof setImmediate> | undefined} */
#startupFlushImmediate
/** @type {ExposureContext} */
#context
/**
* @param {import('../../config/config-base')} config - Tracer configuration object
* @param {ExposureRoute} [route] - Caller-supplied route
*/
constructor (config, route) {
route ??= { url: config.url, basePath: EVP_PROXY_PATH_V2 }
const headers = route.headers ?? {
[EVP_SUBDOMAIN_HEADER_NAME]: EVP_EVENT_PLATFORM_SUBDOMAIN,
}
super({
config,
agentUrl: route.url,
endpoint: joinEVPProxyPath(route.basePath, EXPOSURES_ENDPOINT),
payloadSizeLimit: EVP_PAYLOAD_SIZE_LIMIT,
eventSizeLimit: EVP_EVENT_SIZE_LIMIT,
headers,
})
if (route.agent || route.fallback) {
this.#setRoute({ ...route, headers })
}
/** @type {ExposureContext} */
const context = {
service: config.service,
}
if (config.version !== undefined) {
context.version = config.version
}
if (config.env !== undefined) {
context.env = config.env
}
this.#context = context
}
/**
* @param {boolean} enabled - Whether to enable the writer
* @param {ExposureRoute} [route] - Selected EVP route
* @returns {void}
*/
setEnabled (enabled, route) {
this.#routeResolved = true
if (route) {
this.#setRoute(route)
}
this.#enabled = enabled
if (enabled) {
this.#scheduleStartupFlush()
} else {
this.#cancelStartupFlush()
this._buffer = []
this._bufferStart = 0
}
}
/**
* Applies caller-supplied route data without performing discovery.
*
* @param {ExposureRoute} route - Selected EVP route
* @returns {void}
*/
#setRoute (route) {
const fallbackRoute = route.fallback && {
url: route.fallback.url,
endpoint: joinEVPProxyPath(route.fallback.basePath, EXPOSURES_ENDPOINT),
headers: route.fallback.headers ?? {},
agent: route.fallback.agent,
}
const headers = route.headers ?? {
[EVP_SUBDOMAIN_HEADER_NAME]: EVP_EVENT_PLATFORM_SUBDOMAIN,
}
this._setRoutes({
url: route.url,
endpoint: joinEVPProxyPath(route.basePath, EXPOSURES_ENDPOINT),
headers,
agent: route.agent,
}, fallbackRoute)
}
/**
* Appends exposure event(s) to the buffer.
*
* @param {ExposureEvent|ExposureEvent[]} events - Exposure event(s) to append
* @returns {void}
*/
append (events) {
if (this.#routeResolved && !this.#enabled) return
super.append(events)
}
/**
* Flushes buffered exposure events through the selected route.
*
* @returns {void}
*/
flush () {
if (!this.#enabled) return
this.#cancelStartupFlush()
super.flush()
}
/**
* Flushes startup events after route selection completes.
*
* @returns {void}
*/
#scheduleStartupFlush () {
if (this.#startupFlushImmediate || this._buffer.length === 0) return
this.#startupFlushImmediate = setImmediate(() => {
this.#startupFlushImmediate = undefined
this.flush()
})
}
/**
* Cancels a scheduled drain.
*
* @returns {void}
*/
#cancelStartupFlush () {
if (!this.#startupFlushImmediate) return
clearImmediate(this.#startupFlushImmediate)
this.#startupFlushImmediate = undefined
}
/**
* Formats exposure events with service context metadata
* @param {Array<ExposureEvent>} events - Array of exposure events to format
* @returns {ExposureEventPayload} Formatted payload with service context
*/
makePayload (events) {
const formattedEvents = events.map(event => {
/** @type {ExposureEvent} */
return {
timestamp: event.timestamp || Date.now(),
allocation: {
key: event.allocation?.key || event['allocation.key'],
},
flag: {
key: event.flag?.key || event['flag.key'],
},
variant: {
key: event.variant?.key || event['variant.key'],
},
subject: {
id: event.subject?.id || event['subject.id'],
type: event.subject?.type,
attributes: event.subject?.attributes,
},
}
})
return {
context: this.#context,
exposures: formattedEvents,
}
}
}
module.exports = ExposuresWriter