@wisemen/vue-core-telemetry 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/dist/index.cjs +195 -0
- package/dist/index.d.cts +90 -0
- package/dist/index.d.mts +90 -0
- package/dist/index.d.ts +90 -0
- package/dist/index.mjs +178 -0
- package/package.json +71 -0
package/README.md
ADDED
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const Sentry = require('@sentry/vue');
|
|
4
|
+
const contextZone = require('@opentelemetry/context-zone');
|
|
5
|
+
const core = require('@opentelemetry/core');
|
|
6
|
+
const exporterTraceOtlpHttp = require('@opentelemetry/exporter-trace-otlp-http');
|
|
7
|
+
const resources = require('@opentelemetry/resources');
|
|
8
|
+
const sdkTraceWeb = require('@opentelemetry/sdk-trace-web');
|
|
9
|
+
const instrumentation = require('@opentelemetry/instrumentation');
|
|
10
|
+
const instrumentationFetch = require('@opentelemetry/instrumentation-fetch');
|
|
11
|
+
const instrumentationUserInteraction = require('@opentelemetry/instrumentation-user-interaction');
|
|
12
|
+
|
|
13
|
+
function _interopNamespaceCompat(e) {
|
|
14
|
+
if (e && typeof e === 'object' && 'default' in e) return e;
|
|
15
|
+
const n = Object.create(null);
|
|
16
|
+
if (e) {
|
|
17
|
+
for (const k in e) {
|
|
18
|
+
n[k] = e[k];
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
n.default = e;
|
|
22
|
+
return n;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const Sentry__namespace = /*#__PURE__*/_interopNamespaceCompat(Sentry);
|
|
26
|
+
|
|
27
|
+
async function initOpenTelemetry(options) {
|
|
28
|
+
console.log("Initializing OpenTelemetry tracing...", options);
|
|
29
|
+
const endpoint = options.traceEndpoint;
|
|
30
|
+
if (endpoint == null) {
|
|
31
|
+
console.warn("OpenTelemetry is disabled. No trace endpoint provided.");
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
const accessToken = await options.accessTokenFn();
|
|
35
|
+
const customOTLPTraceExporter = new CustomOTLPTraceExporter({
|
|
36
|
+
headers: {
|
|
37
|
+
"Authorization": `Bearer ${accessToken}`,
|
|
38
|
+
"Content-Type": "application/json"
|
|
39
|
+
},
|
|
40
|
+
url: `${endpoint}`
|
|
41
|
+
}, options.accessTokenFn);
|
|
42
|
+
const tracerProvider = new sdkTraceWeb.WebTracerProvider({
|
|
43
|
+
resource: resources.resourceFromAttributes({
|
|
44
|
+
"deployment.build": options.buildNumber ?? "unknown",
|
|
45
|
+
"deployment.commit": options.commitHash ?? "unknown",
|
|
46
|
+
"deployment.environment": options.environment ?? "unknown",
|
|
47
|
+
"deployment.timestamp": options.buildTimestamp ?? "unknown",
|
|
48
|
+
"service.name": options.serviceName
|
|
49
|
+
}),
|
|
50
|
+
spanProcessors: [
|
|
51
|
+
new sdkTraceWeb.BatchSpanProcessor(customOTLPTraceExporter)
|
|
52
|
+
]
|
|
53
|
+
});
|
|
54
|
+
tracerProvider.register({
|
|
55
|
+
contextManager: new contextZone.ZoneContextManager()
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
class CustomOTLPTraceExporter extends exporterTraceOtlpHttp.OTLPTraceExporter {
|
|
59
|
+
_headers;
|
|
60
|
+
accessTokenFn;
|
|
61
|
+
constructor(config, accessTokenFn) {
|
|
62
|
+
super(config);
|
|
63
|
+
this._headers = {
|
|
64
|
+
...config.headers
|
|
65
|
+
};
|
|
66
|
+
this.accessTokenFn = accessTokenFn;
|
|
67
|
+
}
|
|
68
|
+
async export(items, resultCallback) {
|
|
69
|
+
const token = await this.accessTokenFn();
|
|
70
|
+
if (token !== null) {
|
|
71
|
+
this._headers = {
|
|
72
|
+
...this._headers,
|
|
73
|
+
Authorization: `Bearer ${token}`
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
if (!this._headers?.Authorization) {
|
|
77
|
+
resultCallback({
|
|
78
|
+
code: core.ExportResultCode.FAILED
|
|
79
|
+
});
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
const params = this._delegate?._transport?._transport?._parameters;
|
|
83
|
+
if (typeof params === "object" && params !== null) {
|
|
84
|
+
params.headers = () => {
|
|
85
|
+
return this._headers;
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
return super.export(items, (result) => {
|
|
89
|
+
return resultCallback(result);
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function initSentry(app, options) {
|
|
95
|
+
function logWarning(message) {
|
|
96
|
+
if (options.debug) {
|
|
97
|
+
console.warn(`[Sentry] ${message}`);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
const {
|
|
101
|
+
debug = false,
|
|
102
|
+
dsn,
|
|
103
|
+
enabled = true,
|
|
104
|
+
enableReplays = false,
|
|
105
|
+
environment = "production",
|
|
106
|
+
replaysOnErrorSampleRate = 1,
|
|
107
|
+
replaysSessionSampleRate = 0.1,
|
|
108
|
+
sampleRate = 1
|
|
109
|
+
} = options;
|
|
110
|
+
if (!enabled || !dsn) {
|
|
111
|
+
if (debug) {
|
|
112
|
+
logWarning(`Initialization skipped: Sentry is disabled or DSN is not provided. enabled=${enabled}, dsn=${dsn}`);
|
|
113
|
+
}
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
const integrations = [];
|
|
117
|
+
if (enableReplays) {
|
|
118
|
+
const replayIntegration = Sentry__namespace.replayIntegration({
|
|
119
|
+
blockAllMedia: true,
|
|
120
|
+
maskAllInputs: true,
|
|
121
|
+
maskAllText: true
|
|
122
|
+
});
|
|
123
|
+
integrations.push(replayIntegration);
|
|
124
|
+
}
|
|
125
|
+
Sentry__namespace.init({
|
|
126
|
+
app,
|
|
127
|
+
debug,
|
|
128
|
+
dsn,
|
|
129
|
+
environment,
|
|
130
|
+
integrations,
|
|
131
|
+
replaysOnErrorSampleRate,
|
|
132
|
+
replaysSessionSampleRate,
|
|
133
|
+
sampleRate
|
|
134
|
+
});
|
|
135
|
+
if (debug) {
|
|
136
|
+
logWarning(`Initialized with DSN: ${dsn}`);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function registerAppInstrumentations(instrumentations) {
|
|
141
|
+
instrumentation.registerInstrumentations({
|
|
142
|
+
instrumentations: [
|
|
143
|
+
new instrumentationUserInteraction.UserInteractionInstrumentation({
|
|
144
|
+
eventNames: [
|
|
145
|
+
"click",
|
|
146
|
+
"change",
|
|
147
|
+
"keydown"
|
|
148
|
+
]
|
|
149
|
+
}),
|
|
150
|
+
new instrumentationFetch.FetchInstrumentation({
|
|
151
|
+
propagateTraceHeaderCorsUrls: /.*/
|
|
152
|
+
}),
|
|
153
|
+
...instrumentations ?? []
|
|
154
|
+
]
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
class Telemetry {
|
|
159
|
+
constructor(options) {
|
|
160
|
+
this.options = options;
|
|
161
|
+
}
|
|
162
|
+
captureException(error, context) {
|
|
163
|
+
Sentry__namespace.captureException(error, {
|
|
164
|
+
contexts: {
|
|
165
|
+
additional: context
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
captureMessage(message, level) {
|
|
170
|
+
Sentry__namespace.captureMessage(message, level);
|
|
171
|
+
}
|
|
172
|
+
async init(app) {
|
|
173
|
+
if (this.options.sentry?.dsn !== void 0) {
|
|
174
|
+
initSentry(app, this.options.sentry);
|
|
175
|
+
}
|
|
176
|
+
if (this.options.openTelemetry !== void 0 && this.options.openTelemetry?.enabled !== false) {
|
|
177
|
+
await initOpenTelemetry(this.options.openTelemetry);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
setExtra(key, value) {
|
|
181
|
+
Sentry__namespace.setExtra(key, value);
|
|
182
|
+
}
|
|
183
|
+
setTag(key, value) {
|
|
184
|
+
Sentry__namespace.setTag(key, value);
|
|
185
|
+
}
|
|
186
|
+
setTags(tags) {
|
|
187
|
+
Sentry__namespace.setTags(tags);
|
|
188
|
+
}
|
|
189
|
+
setUser(user) {
|
|
190
|
+
Sentry__namespace.setUser(user);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
exports.Telemetry = Telemetry;
|
|
195
|
+
exports.registerAppInstrumentations = registerAppInstrumentations;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import * as Sentry from '@sentry/vue';
|
|
2
|
+
import { App } from 'vue';
|
|
3
|
+
import { Instrumentation } from '@opentelemetry/instrumentation';
|
|
4
|
+
|
|
5
|
+
interface SentryOptions {
|
|
6
|
+
/**
|
|
7
|
+
* debug: If true, enables debug mode for Sentry.
|
|
8
|
+
* Default is false.
|
|
9
|
+
*/
|
|
10
|
+
debug?: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* dsn: The Data Source Name for Sentry, which is required for initialization.
|
|
13
|
+
* This is the endpoint where Sentry will send error reports.
|
|
14
|
+
*/
|
|
15
|
+
dsn: string;
|
|
16
|
+
/**
|
|
17
|
+
* enabled: If true, enables Sentry error reporting.
|
|
18
|
+
* Default is true.
|
|
19
|
+
*/
|
|
20
|
+
enabled?: boolean;
|
|
21
|
+
/**
|
|
22
|
+
* enableReplays: If true, enables session replay functionality.
|
|
23
|
+
* Default is false.
|
|
24
|
+
*/
|
|
25
|
+
enableReplays?: boolean;
|
|
26
|
+
/**
|
|
27
|
+
* environment: The environment in which the application is running (e.g., 'production', 'development').
|
|
28
|
+
* Default is 'production'.
|
|
29
|
+
*/
|
|
30
|
+
environment?: string;
|
|
31
|
+
/**
|
|
32
|
+
* replaysOnErrorSampleRate: The sample rate for session replays when an error occurs.
|
|
33
|
+
* Default is 1.0 (100%).
|
|
34
|
+
*/
|
|
35
|
+
replaysOnErrorSampleRate?: number;
|
|
36
|
+
/**
|
|
37
|
+
* replaysSessionSampleRate: The sample rate for session replays.
|
|
38
|
+
* Default is 0.1 (10%).
|
|
39
|
+
*/
|
|
40
|
+
replaysSessionSampleRate?: number;
|
|
41
|
+
/**
|
|
42
|
+
* sampleRate: The sample rate for capturing events.
|
|
43
|
+
* Default is 1.0 (100%).
|
|
44
|
+
*/
|
|
45
|
+
sampleRate?: number;
|
|
46
|
+
}
|
|
47
|
+
interface OpenTelemetryOptions {
|
|
48
|
+
accessTokenFn: () => Promise<string>;
|
|
49
|
+
buildNumber?: string;
|
|
50
|
+
buildTimestamp?: string;
|
|
51
|
+
commitHash?: string;
|
|
52
|
+
debug?: boolean;
|
|
53
|
+
enabled?: boolean;
|
|
54
|
+
environment?: string;
|
|
55
|
+
logEndpoint?: string;
|
|
56
|
+
metricsEndpoint?: string;
|
|
57
|
+
serviceName: string;
|
|
58
|
+
serviceVersion?: string;
|
|
59
|
+
traceEndpoint?: string;
|
|
60
|
+
}
|
|
61
|
+
interface TelemetryOptions {
|
|
62
|
+
openTelemetry?: OpenTelemetryOptions;
|
|
63
|
+
sentry?: SentryOptions;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Register default OpenTelemetry instrumentations for web applications.
|
|
68
|
+
* This includes Fetch and User Interaction instrumentations.
|
|
69
|
+
*
|
|
70
|
+
* @param instrumentations - Additional instrumentations to register.
|
|
71
|
+
*/
|
|
72
|
+
declare function registerAppInstrumentations(instrumentations?: Instrumentation[]): void;
|
|
73
|
+
|
|
74
|
+
interface TelemetryUser {
|
|
75
|
+
id?: string;
|
|
76
|
+
email?: string;
|
|
77
|
+
}
|
|
78
|
+
declare class Telemetry {
|
|
79
|
+
private options;
|
|
80
|
+
constructor(options: TelemetryOptions);
|
|
81
|
+
captureException(error: any, context?: Record<string, any>): void;
|
|
82
|
+
captureMessage(message: string, level?: Sentry.SeverityLevel): void;
|
|
83
|
+
init(app: App): Promise<void>;
|
|
84
|
+
setExtra(key: string, value: any): void;
|
|
85
|
+
setTag(key: string, value: string): void;
|
|
86
|
+
setTags(tags: Record<string, string>): void;
|
|
87
|
+
setUser(user: TelemetryUser): void;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export { Telemetry, registerAppInstrumentations };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import * as Sentry from '@sentry/vue';
|
|
2
|
+
import { App } from 'vue';
|
|
3
|
+
import { Instrumentation } from '@opentelemetry/instrumentation';
|
|
4
|
+
|
|
5
|
+
interface SentryOptions {
|
|
6
|
+
/**
|
|
7
|
+
* debug: If true, enables debug mode for Sentry.
|
|
8
|
+
* Default is false.
|
|
9
|
+
*/
|
|
10
|
+
debug?: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* dsn: The Data Source Name for Sentry, which is required for initialization.
|
|
13
|
+
* This is the endpoint where Sentry will send error reports.
|
|
14
|
+
*/
|
|
15
|
+
dsn: string;
|
|
16
|
+
/**
|
|
17
|
+
* enabled: If true, enables Sentry error reporting.
|
|
18
|
+
* Default is true.
|
|
19
|
+
*/
|
|
20
|
+
enabled?: boolean;
|
|
21
|
+
/**
|
|
22
|
+
* enableReplays: If true, enables session replay functionality.
|
|
23
|
+
* Default is false.
|
|
24
|
+
*/
|
|
25
|
+
enableReplays?: boolean;
|
|
26
|
+
/**
|
|
27
|
+
* environment: The environment in which the application is running (e.g., 'production', 'development').
|
|
28
|
+
* Default is 'production'.
|
|
29
|
+
*/
|
|
30
|
+
environment?: string;
|
|
31
|
+
/**
|
|
32
|
+
* replaysOnErrorSampleRate: The sample rate for session replays when an error occurs.
|
|
33
|
+
* Default is 1.0 (100%).
|
|
34
|
+
*/
|
|
35
|
+
replaysOnErrorSampleRate?: number;
|
|
36
|
+
/**
|
|
37
|
+
* replaysSessionSampleRate: The sample rate for session replays.
|
|
38
|
+
* Default is 0.1 (10%).
|
|
39
|
+
*/
|
|
40
|
+
replaysSessionSampleRate?: number;
|
|
41
|
+
/**
|
|
42
|
+
* sampleRate: The sample rate for capturing events.
|
|
43
|
+
* Default is 1.0 (100%).
|
|
44
|
+
*/
|
|
45
|
+
sampleRate?: number;
|
|
46
|
+
}
|
|
47
|
+
interface OpenTelemetryOptions {
|
|
48
|
+
accessTokenFn: () => Promise<string>;
|
|
49
|
+
buildNumber?: string;
|
|
50
|
+
buildTimestamp?: string;
|
|
51
|
+
commitHash?: string;
|
|
52
|
+
debug?: boolean;
|
|
53
|
+
enabled?: boolean;
|
|
54
|
+
environment?: string;
|
|
55
|
+
logEndpoint?: string;
|
|
56
|
+
metricsEndpoint?: string;
|
|
57
|
+
serviceName: string;
|
|
58
|
+
serviceVersion?: string;
|
|
59
|
+
traceEndpoint?: string;
|
|
60
|
+
}
|
|
61
|
+
interface TelemetryOptions {
|
|
62
|
+
openTelemetry?: OpenTelemetryOptions;
|
|
63
|
+
sentry?: SentryOptions;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Register default OpenTelemetry instrumentations for web applications.
|
|
68
|
+
* This includes Fetch and User Interaction instrumentations.
|
|
69
|
+
*
|
|
70
|
+
* @param instrumentations - Additional instrumentations to register.
|
|
71
|
+
*/
|
|
72
|
+
declare function registerAppInstrumentations(instrumentations?: Instrumentation[]): void;
|
|
73
|
+
|
|
74
|
+
interface TelemetryUser {
|
|
75
|
+
id?: string;
|
|
76
|
+
email?: string;
|
|
77
|
+
}
|
|
78
|
+
declare class Telemetry {
|
|
79
|
+
private options;
|
|
80
|
+
constructor(options: TelemetryOptions);
|
|
81
|
+
captureException(error: any, context?: Record<string, any>): void;
|
|
82
|
+
captureMessage(message: string, level?: Sentry.SeverityLevel): void;
|
|
83
|
+
init(app: App): Promise<void>;
|
|
84
|
+
setExtra(key: string, value: any): void;
|
|
85
|
+
setTag(key: string, value: string): void;
|
|
86
|
+
setTags(tags: Record<string, string>): void;
|
|
87
|
+
setUser(user: TelemetryUser): void;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export { Telemetry, registerAppInstrumentations };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import * as Sentry from '@sentry/vue';
|
|
2
|
+
import { App } from 'vue';
|
|
3
|
+
import { Instrumentation } from '@opentelemetry/instrumentation';
|
|
4
|
+
|
|
5
|
+
interface SentryOptions {
|
|
6
|
+
/**
|
|
7
|
+
* debug: If true, enables debug mode for Sentry.
|
|
8
|
+
* Default is false.
|
|
9
|
+
*/
|
|
10
|
+
debug?: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* dsn: The Data Source Name for Sentry, which is required for initialization.
|
|
13
|
+
* This is the endpoint where Sentry will send error reports.
|
|
14
|
+
*/
|
|
15
|
+
dsn: string;
|
|
16
|
+
/**
|
|
17
|
+
* enabled: If true, enables Sentry error reporting.
|
|
18
|
+
* Default is true.
|
|
19
|
+
*/
|
|
20
|
+
enabled?: boolean;
|
|
21
|
+
/**
|
|
22
|
+
* enableReplays: If true, enables session replay functionality.
|
|
23
|
+
* Default is false.
|
|
24
|
+
*/
|
|
25
|
+
enableReplays?: boolean;
|
|
26
|
+
/**
|
|
27
|
+
* environment: The environment in which the application is running (e.g., 'production', 'development').
|
|
28
|
+
* Default is 'production'.
|
|
29
|
+
*/
|
|
30
|
+
environment?: string;
|
|
31
|
+
/**
|
|
32
|
+
* replaysOnErrorSampleRate: The sample rate for session replays when an error occurs.
|
|
33
|
+
* Default is 1.0 (100%).
|
|
34
|
+
*/
|
|
35
|
+
replaysOnErrorSampleRate?: number;
|
|
36
|
+
/**
|
|
37
|
+
* replaysSessionSampleRate: The sample rate for session replays.
|
|
38
|
+
* Default is 0.1 (10%).
|
|
39
|
+
*/
|
|
40
|
+
replaysSessionSampleRate?: number;
|
|
41
|
+
/**
|
|
42
|
+
* sampleRate: The sample rate for capturing events.
|
|
43
|
+
* Default is 1.0 (100%).
|
|
44
|
+
*/
|
|
45
|
+
sampleRate?: number;
|
|
46
|
+
}
|
|
47
|
+
interface OpenTelemetryOptions {
|
|
48
|
+
accessTokenFn: () => Promise<string>;
|
|
49
|
+
buildNumber?: string;
|
|
50
|
+
buildTimestamp?: string;
|
|
51
|
+
commitHash?: string;
|
|
52
|
+
debug?: boolean;
|
|
53
|
+
enabled?: boolean;
|
|
54
|
+
environment?: string;
|
|
55
|
+
logEndpoint?: string;
|
|
56
|
+
metricsEndpoint?: string;
|
|
57
|
+
serviceName: string;
|
|
58
|
+
serviceVersion?: string;
|
|
59
|
+
traceEndpoint?: string;
|
|
60
|
+
}
|
|
61
|
+
interface TelemetryOptions {
|
|
62
|
+
openTelemetry?: OpenTelemetryOptions;
|
|
63
|
+
sentry?: SentryOptions;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Register default OpenTelemetry instrumentations for web applications.
|
|
68
|
+
* This includes Fetch and User Interaction instrumentations.
|
|
69
|
+
*
|
|
70
|
+
* @param instrumentations - Additional instrumentations to register.
|
|
71
|
+
*/
|
|
72
|
+
declare function registerAppInstrumentations(instrumentations?: Instrumentation[]): void;
|
|
73
|
+
|
|
74
|
+
interface TelemetryUser {
|
|
75
|
+
id?: string;
|
|
76
|
+
email?: string;
|
|
77
|
+
}
|
|
78
|
+
declare class Telemetry {
|
|
79
|
+
private options;
|
|
80
|
+
constructor(options: TelemetryOptions);
|
|
81
|
+
captureException(error: any, context?: Record<string, any>): void;
|
|
82
|
+
captureMessage(message: string, level?: Sentry.SeverityLevel): void;
|
|
83
|
+
init(app: App): Promise<void>;
|
|
84
|
+
setExtra(key: string, value: any): void;
|
|
85
|
+
setTag(key: string, value: string): void;
|
|
86
|
+
setTags(tags: Record<string, string>): void;
|
|
87
|
+
setUser(user: TelemetryUser): void;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export { Telemetry, registerAppInstrumentations };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import * as Sentry from '@sentry/vue';
|
|
2
|
+
import { ZoneContextManager } from '@opentelemetry/context-zone';
|
|
3
|
+
import { ExportResultCode } from '@opentelemetry/core';
|
|
4
|
+
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';
|
|
5
|
+
import { resourceFromAttributes } from '@opentelemetry/resources';
|
|
6
|
+
import { WebTracerProvider, BatchSpanProcessor } from '@opentelemetry/sdk-trace-web';
|
|
7
|
+
import { registerInstrumentations } from '@opentelemetry/instrumentation';
|
|
8
|
+
import { FetchInstrumentation } from '@opentelemetry/instrumentation-fetch';
|
|
9
|
+
import { UserInteractionInstrumentation } from '@opentelemetry/instrumentation-user-interaction';
|
|
10
|
+
|
|
11
|
+
async function initOpenTelemetry(options) {
|
|
12
|
+
console.log("Initializing OpenTelemetry tracing...", options);
|
|
13
|
+
const endpoint = options.traceEndpoint;
|
|
14
|
+
if (endpoint == null) {
|
|
15
|
+
console.warn("OpenTelemetry is disabled. No trace endpoint provided.");
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
const accessToken = await options.accessTokenFn();
|
|
19
|
+
const customOTLPTraceExporter = new CustomOTLPTraceExporter({
|
|
20
|
+
headers: {
|
|
21
|
+
"Authorization": `Bearer ${accessToken}`,
|
|
22
|
+
"Content-Type": "application/json"
|
|
23
|
+
},
|
|
24
|
+
url: `${endpoint}`
|
|
25
|
+
}, options.accessTokenFn);
|
|
26
|
+
const tracerProvider = new WebTracerProvider({
|
|
27
|
+
resource: resourceFromAttributes({
|
|
28
|
+
"deployment.build": options.buildNumber ?? "unknown",
|
|
29
|
+
"deployment.commit": options.commitHash ?? "unknown",
|
|
30
|
+
"deployment.environment": options.environment ?? "unknown",
|
|
31
|
+
"deployment.timestamp": options.buildTimestamp ?? "unknown",
|
|
32
|
+
"service.name": options.serviceName
|
|
33
|
+
}),
|
|
34
|
+
spanProcessors: [
|
|
35
|
+
new BatchSpanProcessor(customOTLPTraceExporter)
|
|
36
|
+
]
|
|
37
|
+
});
|
|
38
|
+
tracerProvider.register({
|
|
39
|
+
contextManager: new ZoneContextManager()
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
class CustomOTLPTraceExporter extends OTLPTraceExporter {
|
|
43
|
+
_headers;
|
|
44
|
+
accessTokenFn;
|
|
45
|
+
constructor(config, accessTokenFn) {
|
|
46
|
+
super(config);
|
|
47
|
+
this._headers = {
|
|
48
|
+
...config.headers
|
|
49
|
+
};
|
|
50
|
+
this.accessTokenFn = accessTokenFn;
|
|
51
|
+
}
|
|
52
|
+
async export(items, resultCallback) {
|
|
53
|
+
const token = await this.accessTokenFn();
|
|
54
|
+
if (token !== null) {
|
|
55
|
+
this._headers = {
|
|
56
|
+
...this._headers,
|
|
57
|
+
Authorization: `Bearer ${token}`
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
if (!this._headers?.Authorization) {
|
|
61
|
+
resultCallback({
|
|
62
|
+
code: ExportResultCode.FAILED
|
|
63
|
+
});
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
const params = this._delegate?._transport?._transport?._parameters;
|
|
67
|
+
if (typeof params === "object" && params !== null) {
|
|
68
|
+
params.headers = () => {
|
|
69
|
+
return this._headers;
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
return super.export(items, (result) => {
|
|
73
|
+
return resultCallback(result);
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function initSentry(app, options) {
|
|
79
|
+
function logWarning(message) {
|
|
80
|
+
if (options.debug) {
|
|
81
|
+
console.warn(`[Sentry] ${message}`);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
const {
|
|
85
|
+
debug = false,
|
|
86
|
+
dsn,
|
|
87
|
+
enabled = true,
|
|
88
|
+
enableReplays = false,
|
|
89
|
+
environment = "production",
|
|
90
|
+
replaysOnErrorSampleRate = 1,
|
|
91
|
+
replaysSessionSampleRate = 0.1,
|
|
92
|
+
sampleRate = 1
|
|
93
|
+
} = options;
|
|
94
|
+
if (!enabled || !dsn) {
|
|
95
|
+
if (debug) {
|
|
96
|
+
logWarning(`Initialization skipped: Sentry is disabled or DSN is not provided. enabled=${enabled}, dsn=${dsn}`);
|
|
97
|
+
}
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
const integrations = [];
|
|
101
|
+
if (enableReplays) {
|
|
102
|
+
const replayIntegration = Sentry.replayIntegration({
|
|
103
|
+
blockAllMedia: true,
|
|
104
|
+
maskAllInputs: true,
|
|
105
|
+
maskAllText: true
|
|
106
|
+
});
|
|
107
|
+
integrations.push(replayIntegration);
|
|
108
|
+
}
|
|
109
|
+
Sentry.init({
|
|
110
|
+
app,
|
|
111
|
+
debug,
|
|
112
|
+
dsn,
|
|
113
|
+
environment,
|
|
114
|
+
integrations,
|
|
115
|
+
replaysOnErrorSampleRate,
|
|
116
|
+
replaysSessionSampleRate,
|
|
117
|
+
sampleRate
|
|
118
|
+
});
|
|
119
|
+
if (debug) {
|
|
120
|
+
logWarning(`Initialized with DSN: ${dsn}`);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function registerAppInstrumentations(instrumentations) {
|
|
125
|
+
registerInstrumentations({
|
|
126
|
+
instrumentations: [
|
|
127
|
+
new UserInteractionInstrumentation({
|
|
128
|
+
eventNames: [
|
|
129
|
+
"click",
|
|
130
|
+
"change",
|
|
131
|
+
"keydown"
|
|
132
|
+
]
|
|
133
|
+
}),
|
|
134
|
+
new FetchInstrumentation({
|
|
135
|
+
propagateTraceHeaderCorsUrls: /.*/
|
|
136
|
+
}),
|
|
137
|
+
...instrumentations ?? []
|
|
138
|
+
]
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
class Telemetry {
|
|
143
|
+
constructor(options) {
|
|
144
|
+
this.options = options;
|
|
145
|
+
}
|
|
146
|
+
captureException(error, context) {
|
|
147
|
+
Sentry.captureException(error, {
|
|
148
|
+
contexts: {
|
|
149
|
+
additional: context
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
captureMessage(message, level) {
|
|
154
|
+
Sentry.captureMessage(message, level);
|
|
155
|
+
}
|
|
156
|
+
async init(app) {
|
|
157
|
+
if (this.options.sentry?.dsn !== void 0) {
|
|
158
|
+
initSentry(app, this.options.sentry);
|
|
159
|
+
}
|
|
160
|
+
if (this.options.openTelemetry !== void 0 && this.options.openTelemetry?.enabled !== false) {
|
|
161
|
+
await initOpenTelemetry(this.options.openTelemetry);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
setExtra(key, value) {
|
|
165
|
+
Sentry.setExtra(key, value);
|
|
166
|
+
}
|
|
167
|
+
setTag(key, value) {
|
|
168
|
+
Sentry.setTag(key, value);
|
|
169
|
+
}
|
|
170
|
+
setTags(tags) {
|
|
171
|
+
Sentry.setTags(tags);
|
|
172
|
+
}
|
|
173
|
+
setUser(user) {
|
|
174
|
+
Sentry.setUser(user);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export { Telemetry, registerAppInstrumentations };
|
package/package.json
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@wisemen/vue-core-telemetry",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.0.1",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"sideEffects": false,
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"import": "./dist/index.mjs",
|
|
11
|
+
"require": "./dist/index.cjs"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"main": "./dist/index.mjs",
|
|
15
|
+
"module": "./dist/index.mjs",
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"typesVersions": {
|
|
18
|
+
"*": {
|
|
19
|
+
"*": [
|
|
20
|
+
"./dist/*",
|
|
21
|
+
"./dist/index.d.ts"
|
|
22
|
+
]
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist"
|
|
27
|
+
],
|
|
28
|
+
"scripts": {
|
|
29
|
+
"build": "unbuild",
|
|
30
|
+
"cleanup": "rimraf dist/ node_modules/ .turbo/",
|
|
31
|
+
"dev": "unbuild --stub",
|
|
32
|
+
"lint": "eslint .",
|
|
33
|
+
"lint:fix": "eslint . --fix",
|
|
34
|
+
"release": "bumpp && npm publish",
|
|
35
|
+
"start": "esno src/index.ts",
|
|
36
|
+
"type-check": "tsc --noEmit"
|
|
37
|
+
},
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"vue": "3.5.18"
|
|
40
|
+
},
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"@opentelemetry/context-zone": "2.0.1",
|
|
43
|
+
"@opentelemetry/core": "2.0.1",
|
|
44
|
+
"@opentelemetry/exporter-trace-otlp-http": "0.203.0",
|
|
45
|
+
"@opentelemetry/instrumentation": "0.203.0",
|
|
46
|
+
"@opentelemetry/instrumentation-fetch": "0.203.0",
|
|
47
|
+
"@opentelemetry/instrumentation-user-interaction": "0.48.0",
|
|
48
|
+
"@opentelemetry/otlp-exporter-base": "0.203.0",
|
|
49
|
+
"@opentelemetry/resources": "2.0.1",
|
|
50
|
+
"@opentelemetry/sdk-trace-base": "2.0.1",
|
|
51
|
+
"@opentelemetry/sdk-trace-web": "2.0.1",
|
|
52
|
+
"@opentelemetry/semantic-conventions": "1.36.0",
|
|
53
|
+
"@sentry/integrations": "7.114.0",
|
|
54
|
+
"@sentry/tracing": "7.120.4",
|
|
55
|
+
"@sentry/vue": "10.0.0"
|
|
56
|
+
},
|
|
57
|
+
"devDependencies": {
|
|
58
|
+
"@number-flow/vue": "0.4.8",
|
|
59
|
+
"@types/node": "catalog:",
|
|
60
|
+
"@wisemen/eslint-config-vue": "catalog:",
|
|
61
|
+
"bumpp": "catalog:",
|
|
62
|
+
"eslint": "catalog:",
|
|
63
|
+
"esno": "catalog:",
|
|
64
|
+
"rimraf": "catalog:",
|
|
65
|
+
"typescript": "catalog:",
|
|
66
|
+
"unbuild": "catalog:",
|
|
67
|
+
"vite": "catalog:",
|
|
68
|
+
"vitest": "catalog:",
|
|
69
|
+
"vitest-localstorage-mock": "0.1.2"
|
|
70
|
+
}
|
|
71
|
+
}
|