@wisemen/vue-core-telemetry 0.0.1 → 0.0.2

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Wisemen
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/index.d.ts CHANGED
@@ -1,68 +1,70 @@
1
- import * as Sentry from '@sentry/vue';
2
- import { App } from 'vue';
3
- import { Instrumentation } from '@opentelemetry/instrumentation';
1
+ import * as Sentry from "@sentry/vue";
2
+ import { Instrumentation } from "@opentelemetry/instrumentation";
3
+ import { App } from "vue";
4
4
 
5
+ //#region src/types.d.ts
5
6
  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;
7
+ /**
8
+ * debug: If true, enables debug mode for Sentry.
9
+ * Default is false.
10
+ */
11
+ debug?: boolean;
12
+ /**
13
+ * dsn: The Data Source Name for Sentry, which is required for initialization.
14
+ * This is the endpoint where Sentry will send error reports.
15
+ */
16
+ dsn: string;
17
+ /**
18
+ * enabled: If true, enables Sentry error reporting.
19
+ * Default is true.
20
+ */
21
+ enabled?: boolean;
22
+ /**
23
+ * enableReplays: If true, enables session replay functionality.
24
+ * Default is false.
25
+ */
26
+ enableReplays?: boolean;
27
+ /**
28
+ * environment: The environment in which the application is running (e.g., 'production', 'development').
29
+ * Default is 'production'.
30
+ */
31
+ environment?: string;
32
+ /**
33
+ * replaysOnErrorSampleRate: The sample rate for session replays when an error occurs.
34
+ * Default is 1.0 (100%).
35
+ */
36
+ replaysOnErrorSampleRate?: number;
37
+ /**
38
+ * replaysSessionSampleRate: The sample rate for session replays.
39
+ * Default is 0.1 (10%).
40
+ */
41
+ replaysSessionSampleRate?: number;
42
+ /**
43
+ * sampleRate: The sample rate for capturing events.
44
+ * Default is 1.0 (100%).
45
+ */
46
+ sampleRate?: number;
46
47
  }
47
48
  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;
49
+ accessTokenFn: () => Promise<string>;
50
+ buildNumber?: string;
51
+ buildTimestamp?: string;
52
+ commitHash?: string;
53
+ debug?: boolean;
54
+ enabled?: boolean;
55
+ environment?: string;
56
+ logEndpoint?: string;
57
+ metricsEndpoint?: string;
58
+ serviceName: string;
59
+ serviceVersion?: string;
60
+ traceEndpoint?: string;
60
61
  }
61
62
  interface TelemetryOptions {
62
- openTelemetry?: OpenTelemetryOptions;
63
- sentry?: SentryOptions;
63
+ openTelemetry?: OpenTelemetryOptions;
64
+ sentry?: SentryOptions;
64
65
  }
65
-
66
+ //#endregion
67
+ //#region src/opentelemetry/tracing/instrumentation.d.ts
66
68
  /**
67
69
  * Register default OpenTelemetry instrumentations for web applications.
68
70
  * This includes Fetch and User Interaction instrumentations.
@@ -70,21 +72,22 @@ interface TelemetryOptions {
70
72
  * @param instrumentations - Additional instrumentations to register.
71
73
  */
72
74
  declare function registerAppInstrumentations(instrumentations?: Instrumentation[]): void;
73
-
75
+ //#endregion
76
+ //#region src/index.d.ts
74
77
  interface TelemetryUser {
75
- id?: string;
76
- email?: string;
78
+ id?: string;
79
+ email?: string;
77
80
  }
78
81
  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;
82
+ private options;
83
+ constructor(options: TelemetryOptions);
84
+ captureException(error: any, context?: Record<string, any>): void;
85
+ captureMessage(message: string, level?: Sentry.SeverityLevel): void;
86
+ init(app: App): Promise<void>;
87
+ setExtra(key: string, value: any): void;
88
+ setTag(key: string, value: string): void;
89
+ setTags(tags: Record<string, string>): void;
90
+ setUser(user: TelemetryUser): void;
88
91
  }
89
-
90
- export { Telemetry, registerAppInstrumentations };
92
+ //#endregion
93
+ export { Telemetry, registerAppInstrumentations };
package/dist/index.js ADDED
@@ -0,0 +1,150 @@
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 { BatchSpanProcessor, WebTracerProvider } 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
+ //#region src/opentelemetry/tracing/tracer.ts
12
+ async function initOpenTelemetry(options) {
13
+ console.log("Initializing OpenTelemetry tracing...", options);
14
+ const endpoint = options.traceEndpoint;
15
+ if (endpoint == null) {
16
+ console.warn("OpenTelemetry is disabled. No trace endpoint provided.");
17
+ return;
18
+ }
19
+ const accessToken = await options.accessTokenFn();
20
+ const customOTLPTraceExporter = new CustomOTLPTraceExporter({
21
+ headers: {
22
+ "Authorization": `Bearer ${accessToken}`,
23
+ "Content-Type": "application/json"
24
+ },
25
+ url: `${endpoint}`
26
+ }, options.accessTokenFn);
27
+ new WebTracerProvider({
28
+ resource: resourceFromAttributes({
29
+ "deployment.build": options.buildNumber ?? "unknown",
30
+ "deployment.commit": options.commitHash ?? "unknown",
31
+ "deployment.environment": options.environment ?? "unknown",
32
+ "deployment.timestamp": options.buildTimestamp ?? "unknown",
33
+ "service.name": options.serviceName
34
+ }),
35
+ spanProcessors: [new BatchSpanProcessor(customOTLPTraceExporter)]
36
+ }).register({ contextManager: new ZoneContextManager() });
37
+ }
38
+ var CustomOTLPTraceExporter = class extends OTLPTraceExporter {
39
+ _headers;
40
+ accessTokenFn;
41
+ constructor(config, accessTokenFn) {
42
+ super(config);
43
+ this._headers = { ...config.headers };
44
+ this.accessTokenFn = accessTokenFn;
45
+ }
46
+ async export(items, resultCallback) {
47
+ const token = await this.accessTokenFn();
48
+ if (token !== null) this._headers = {
49
+ ...this._headers,
50
+ Authorization: `Bearer ${token}`
51
+ };
52
+ if (!this._headers?.Authorization) {
53
+ resultCallback({ code: ExportResultCode.FAILED });
54
+ return;
55
+ }
56
+ const params = this._delegate?._transport?._transport?._parameters;
57
+ if (typeof params === "object" && params !== null) params.headers = () => {
58
+ return this._headers;
59
+ };
60
+ return super.export(items, (result) => {
61
+ return resultCallback(result);
62
+ });
63
+ }
64
+ };
65
+
66
+ //#endregion
67
+ //#region src/sentry/sentry.ts
68
+ function initSentry(app, options) {
69
+ function logWarning(message) {
70
+ if (options.debug) console.warn(`[Sentry] ${message}`);
71
+ }
72
+ const { debug = false, dsn, enabled = true, enableReplays = false, environment = "production", replaysOnErrorSampleRate = 1, replaysSessionSampleRate = .1, sampleRate = 1 } = options;
73
+ if (!enabled || !dsn) {
74
+ if (debug) logWarning(`Initialization skipped: Sentry is disabled or DSN is not provided. enabled=${enabled}, dsn=${dsn}`);
75
+ return;
76
+ }
77
+ const integrations = [];
78
+ if (enableReplays) {
79
+ const replayIntegration = Sentry.replayIntegration({
80
+ blockAllMedia: true,
81
+ maskAllInputs: true,
82
+ maskAllText: true
83
+ });
84
+ integrations.push(replayIntegration);
85
+ }
86
+ Sentry.init({
87
+ app,
88
+ debug,
89
+ dsn,
90
+ environment,
91
+ integrations,
92
+ replaysOnErrorSampleRate,
93
+ replaysSessionSampleRate,
94
+ sampleRate
95
+ });
96
+ if (debug) logWarning(`Initialized with DSN: ${dsn}`);
97
+ }
98
+
99
+ //#endregion
100
+ //#region src/opentelemetry/tracing/instrumentation.ts
101
+ /**
102
+ * Register default OpenTelemetry instrumentations for web applications.
103
+ * This includes Fetch and User Interaction instrumentations.
104
+ *
105
+ * @param instrumentations - Additional instrumentations to register.
106
+ */
107
+ function registerAppInstrumentations(instrumentations) {
108
+ registerInstrumentations({ instrumentations: [
109
+ new UserInteractionInstrumentation({ eventNames: [
110
+ "click",
111
+ "change",
112
+ "keydown"
113
+ ] }),
114
+ new FetchInstrumentation({ propagateTraceHeaderCorsUrls: /.*/ }),
115
+ ...instrumentations ?? []
116
+ ] });
117
+ }
118
+
119
+ //#endregion
120
+ //#region src/index.ts
121
+ var Telemetry = class {
122
+ constructor(options) {
123
+ this.options = options;
124
+ }
125
+ captureException(error, context) {
126
+ Sentry.captureException(error, { contexts: { additional: context } });
127
+ }
128
+ captureMessage(message, level) {
129
+ Sentry.captureMessage(message, level);
130
+ }
131
+ async init(app) {
132
+ if (this.options.sentry?.dsn !== void 0) initSentry(app, this.options.sentry);
133
+ if (this.options.openTelemetry !== void 0 && this.options.openTelemetry?.enabled !== false) await initOpenTelemetry(this.options.openTelemetry);
134
+ }
135
+ setExtra(key, value) {
136
+ Sentry.setExtra(key, value);
137
+ }
138
+ setTag(key, value) {
139
+ Sentry.setTag(key, value);
140
+ }
141
+ setTags(tags) {
142
+ Sentry.setTags(tags);
143
+ }
144
+ setUser(user) {
145
+ Sentry.setUser(user);
146
+ }
147
+ };
148
+
149
+ //#endregion
150
+ export { Telemetry, registerAppInstrumentations };
package/package.json CHANGED
@@ -1,7 +1,10 @@
1
1
  {
2
2
  "name": "@wisemen/vue-core-telemetry",
3
3
  "type": "module",
4
- "version": "0.0.1",
4
+ "version": "0.0.2",
5
+ "publishConfig": {
6
+ "access": "public"
7
+ },
5
8
  "license": "MIT",
6
9
  "sideEffects": false,
7
10
  "exports": {
@@ -25,47 +28,37 @@
25
28
  "files": [
26
29
  "dist"
27
30
  ],
31
+ "peerDependencies": {
32
+ "vue": ">=3.5.22"
33
+ },
34
+ "dependencies": {
35
+ "@opentelemetry/context-zone": "2.1.0",
36
+ "@opentelemetry/core": "2.1.0",
37
+ "@opentelemetry/exporter-trace-otlp-http": "0.206.0",
38
+ "@opentelemetry/instrumentation": "0.206.0",
39
+ "@opentelemetry/instrumentation-fetch": "0.206.0",
40
+ "@opentelemetry/instrumentation-user-interaction": "0.51.0",
41
+ "@opentelemetry/otlp-exporter-base": "0.206.0",
42
+ "@opentelemetry/resources": "2.1.0",
43
+ "@opentelemetry/sdk-trace-base": "2.1.0",
44
+ "@opentelemetry/sdk-trace-web": "2.1.0",
45
+ "@sentry/vue": "10.20.0"
46
+ },
47
+ "devDependencies": {
48
+ "@wisemen/eslint-config-vue": "1.7.3",
49
+ "bumpp": "10.3.1",
50
+ "eslint": "9.38.0",
51
+ "rimraf": "6.0.1",
52
+ "tsdown": "0.15.8",
53
+ "typescript": "5.9.3"
54
+ },
28
55
  "scripts": {
29
- "build": "unbuild",
56
+ "build": "tsdown",
30
57
  "cleanup": "rimraf dist/ node_modules/ .turbo/",
31
- "dev": "unbuild --stub",
58
+ "dev": "tsdown --watch",
32
59
  "lint": "eslint .",
33
60
  "lint:fix": "eslint . --fix",
34
61
  "release": "bumpp && npm publish",
35
- "start": "esno src/index.ts",
36
62
  "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
63
  }
71
- }
64
+ }
package/dist/index.cjs DELETED
@@ -1,195 +0,0 @@
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 DELETED
@@ -1,90 +0,0 @@
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 DELETED
@@ -1,90 +0,0 @@
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 DELETED
@@ -1,178 +0,0 @@
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 };