Line data Source code
1 : import type { App } from 'vue'
2 : import {
3 : browserApiErrorsIntegration,
4 : eventFiltersIntegration,
5 : init as sentryInit
6 : } from '@sentry/vue'
7 : import type {
8 : Breadcrumb,
9 : Contexts,
10 : ErrorEvent,
11 : Event,
12 : EventHint,
13 : Exception
14 : } from '@sentry/vue'
15 :
16 : import { sentryBeforeSend } from './sentryBeforeSend'
17 : import {
18 : REDACTION_SENTINEL,
19 : redactTelemetryUrls,
20 : redactTelemetryValues
21 : } from '@comfyorg/telemetry/redactTelemetryUrls'
22 :
23 : /** `@sentry/vue` re-exports neither `TransactionEvent` nor `SpanJSON`. */
24 : type SentryOptions = NonNullable<Parameters<typeof sentryInit>[0]>
25 : type SentryTransactionEvent = Parameters<
26 : NonNullable<SentryOptions['beforeSendTransaction']>
27 : >[0]
28 : type SentrySpan = Parameters<NonNullable<SentryOptions['beforeSendSpan']>>[0]
29 :
30 0 : function redactSentryEvent(event: ErrorEvent, hint: EventHint) {
31 0 : const filtered = sentryBeforeSend(event, hint)
32 0 : if (!filtered) return null
33 0 : redactSharedSentryEventFields(filtered)
34 0 : for (const exception of filtered.exception?.values ?? []) {
35 0 : redactSentryException(exception)
36 : }
37 0 : return filtered
38 : }
39 :
40 : /**
41 : * `beforeSend` runs on error events only. A sampled transaction carries the
42 : * same page URL and referrer that `HttpContext` copies onto an error, so it
43 : * needs the shared pass of its own.
44 : */
45 0 : function redactSentryTransaction(event: SentryTransactionEvent) {
46 0 : redactSharedSentryEventFields(event)
47 0 : return event
48 : }
49 :
50 0 : function redactSharedSentryEventFields(event: Event): void {
51 0 : if (event.message) {
52 0 : event.message = redactTelemetryUrls(event.message)
53 : }
54 0 : if (event.transaction) {
55 0 : event.transaction = redactTelemetryUrls(event.transaction)
56 : }
57 0 : if (event.tags) {
58 0 : event.tags = Object.fromEntries(
59 : Object.entries(event.tags).map(([key, value]) => [
60 : redactTelemetryUrls(key),
61 : typeof value === 'string' ? redactTelemetryUrls(value) : value
62 : ])
63 : )
64 : }
65 0 : event.extra = redactTelemetryValues(event.extra)
66 0 : event.contexts = redactSentryContexts(event.contexts)
67 0 : redactSentryRequest(event)
68 : }
69 :
70 0 : function redactSentryRequest(event: Event): void {
71 0 : const request = event.request
72 0 : if (!request) return
73 0 : if (request.url) request.url = redactTelemetryUrls(request.url)
74 0 : request.data = redactUnknownValue(request.data)
75 0 : request.query_string = redactSentryQueryString(request.query_string)
76 0 : const headers = request.headers ?? {}
77 0 : for (const [key, value] of Object.entries(headers)) {
78 0 : if (key.toLowerCase() === 'referer') {
79 0 : headers[key] = redactTelemetryUrls(value)
80 : }
81 : }
82 : }
83 :
84 : type SentryQueryString = NonNullable<Event['request']>['query_string']
85 :
86 0 : function redactSentryQueryString(query: SentryQueryString): SentryQueryString {
87 0 : if (typeof query === 'string') return query && REDACTION_SENTINEL
88 0 : if (Array.isArray(query)) {
89 0 : return query.map(([key, value]) => [
90 : redactTelemetryUrls(key),
91 : value && REDACTION_SENTINEL
92 : ])
93 : }
94 0 : if (!query) return query
95 0 : return Object.fromEntries(
96 : Object.entries(query).map(([key, value]) => [
97 : redactTelemetryUrls(key),
98 : value && REDACTION_SENTINEL
99 : ])
100 : )
101 : }
102 :
103 0 : function redactUnknownValue(value: unknown): unknown {
104 0 : return redactTelemetryValues({ value })?.value
105 : }
106 :
107 0 : function redactSentryContexts(contexts: Contexts | undefined): Contexts {
108 0 : return Object.fromEntries(
109 : Object.entries(contexts ?? {}).map(([key, context]) => [
110 : key,
111 : context ? redactTelemetryValues(context) : context
112 : ])
113 : )
114 : }
115 :
116 47153 : function redactSentryBreadcrumb(breadcrumb: Breadcrumb): Breadcrumb {
117 47153 : return {
118 : ...breadcrumb,
119 : message: breadcrumb.message
120 : ? redactTelemetryUrls(breadcrumb.message)
121 : : breadcrumb.message,
122 : data: redactTelemetryValues(breadcrumb.data)
123 : }
124 : }
125 :
126 0 : function redactSentrySpan(span: SentrySpan): SentrySpan {
127 0 : if (span.description) {
128 0 : span.description = redactTelemetryUrls(span.description)
129 : }
130 0 : redactSpanAttributes(span.data)
131 0 : for (const link of span.links ?? []) {
132 0 : if (link.attributes) redactSpanAttributes(link.attributes)
133 : }
134 0 : return span
135 : }
136 :
137 0 : function redactSpanAttributes(attributes: SentrySpan['data']): void {
138 0 : for (const [key, value] of Object.entries(attributes)) {
139 0 : if (typeof value === 'string') {
140 0 : attributes[key] = redactTelemetryUrls(value)
141 0 : } else if (Array.isArray(value)) {
142 0 : attributes[key] = value.map((item) =>
143 : typeof item === 'string' ? redactTelemetryUrls(item) : item
144 : ) as typeof value
145 : }
146 : }
147 : }
148 :
149 0 : function redactSentryException(exception: Exception): void {
150 0 : if (exception.type) exception.type = redactTelemetryUrls(exception.type)
151 0 : if (exception.value) exception.value = redactTelemetryUrls(exception.value)
152 0 : for (const frame of exception.stacktrace?.frames ?? []) {
153 0 : if (frame.filename) frame.filename = redactTelemetryUrls(frame.filename)
154 0 : if (frame.abs_path) frame.abs_path = redactTelemetryUrls(frame.abs_path)
155 : }
156 : }
157 :
158 2356 : const RESOURCE_TIMING_BUFFER_SIZE = 1000
159 :
160 2355 : function expandResourceTimingBuffer(): void {
161 2355 : if (typeof performance.setResourceTimingBufferSize !== 'function') return
162 2355 : performance.setResourceTimingBufferSize(RESOURCE_TIMING_BUFFER_SIZE)
163 : }
164 :
165 2355 : export function initSentry({
166 : app,
167 : dsn,
168 : enabled,
169 : isCloud
170 : }: {
171 : app: App
172 : dsn: string
173 : enabled: boolean
174 : isCloud: boolean
175 : }) {
176 2355 : expandResourceTimingBuffer()
177 2355 : sentryInit({
178 : app,
179 : dsn,
180 : enabled,
181 : release: __COMFYUI_FRONTEND_VERSION__,
182 : normalizeDepth: 8,
183 : tracesSampleRate: isCloud ? 1.0 : 0,
184 : replaysSessionSampleRate: 0,
185 : replaysOnErrorSampleRate: 0,
186 : beforeSend: redactSentryEvent,
187 : beforeSendTransaction: redactSentryTransaction,
188 : beforeBreadcrumb: redactSentryBreadcrumb,
189 : beforeSendSpan: redactSentrySpan,
190 : denyUrls: [/^(?:chrome|moz|safari(?:-web)?|ms-browser)-extension:\/\//],
191 : ...(isCloud
192 : ? {
193 : integrations: [browserApiErrorsIntegration({ eventTarget: false })]
194 : }
195 : : {
196 : integrations: [eventFiltersIntegration()],
197 : autoSessionTracking: false,
198 : defaultIntegrations: false
199 : })
200 : })
201 : }
|