@monoscopetech/browser 0.1.0

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 ADDED
File without changes
@@ -0,0 +1,9 @@
1
+ import { MonoscopeReplay } from "./replay";
2
+ import { MonoscopeConfig } from "./types";
3
+ declare class Monoscope {
4
+ replay: MonoscopeReplay;
5
+ config: MonoscopeConfig;
6
+ sessionId: string;
7
+ constructor(config: MonoscopeConfig);
8
+ }
9
+ export default Monoscope;
package/dist/index.js ADDED
@@ -0,0 +1,29 @@
1
+ import { MonoscopeReplay } from "./replay";
2
+ import { configureOpenTelemetry } from "./tracing";
3
+ import { v4 as uuidv4 } from "uuid";
4
+ class Monoscope {
5
+ constructor(config) {
6
+ if (!config.projectId) {
7
+ throw new Error("MonoscopeConfig must include projectId");
8
+ }
9
+ if (!config.replayEndpoint) {
10
+ config.replayEndpoint = `https://app.apitoolkit.io/p/${config.projectId}/replay`;
11
+ }
12
+ if (!config.exporterEndpoint) {
13
+ config.exporterEndpoint = "http://otelcol.apitoolkit.io:4318";
14
+ }
15
+ const storedSessionId = sessionStorage.getItem("monoscope-session-id");
16
+ if (storedSessionId) {
17
+ this.sessionId = storedSessionId;
18
+ }
19
+ else {
20
+ this.sessionId = uuidv4();
21
+ sessionStorage.setItem("monoscope-session-id", this.sessionId);
22
+ }
23
+ configureOpenTelemetry(config, this.sessionId);
24
+ this.config = config;
25
+ this.replay = new MonoscopeReplay(config, this.sessionId);
26
+ this.replay.configure();
27
+ }
28
+ }
29
+ export default Monoscope;
@@ -0,0 +1,9 @@
1
+ import { MonoscopeConfig } from "./types";
2
+ export declare class MonoscopeReplay {
3
+ events: any[];
4
+ config: MonoscopeConfig;
5
+ sessionId: string;
6
+ constructor(config: MonoscopeConfig, sessionId: string);
7
+ configure(): void;
8
+ save(): void;
9
+ }
package/dist/replay.js ADDED
@@ -0,0 +1,40 @@
1
+ import * as rrweb from "rrweb";
2
+ export class MonoscopeReplay {
3
+ constructor(config, sessionId) {
4
+ this.events = [];
5
+ this.sessionId = sessionId;
6
+ this.config = config;
7
+ this.save = this.save.bind(this);
8
+ this.events = [];
9
+ this.configure = this.configure.bind(this);
10
+ }
11
+ configure() {
12
+ rrweb.record({
13
+ emit: (event) => {
14
+ this.events.push(event);
15
+ },
16
+ });
17
+ setInterval(this.save, 15 * 1000);
18
+ }
19
+ save() {
20
+ if (this.events.length === 0)
21
+ return;
22
+ let { replayEndpoint, projectId } = this.config;
23
+ if (!replayEndpoint) {
24
+ replayEndpoint = `https://app.apitoolkit.io/p/${projectId}/rrweb`;
25
+ }
26
+ const events = this.events;
27
+ this.events = [];
28
+ const body = JSON.stringify({ events, sessionId: this.sessionId });
29
+ fetch(replayEndpoint, {
30
+ method: "POST",
31
+ headers: {
32
+ "Content-Type": "application/json",
33
+ },
34
+ body,
35
+ }).catch((error) => {
36
+ console.error("Failed to save replay events:", error);
37
+ this.events = [...events, ...this.events];
38
+ });
39
+ }
40
+ }
@@ -0,0 +1,2 @@
1
+ import { MonoscopeConfig } from "./types";
2
+ export declare const configureOpenTelemetry: (config: MonoscopeConfig, sessionId: string) => void;
@@ -0,0 +1,58 @@
1
+ import { BatchSpanProcessor } from "@opentelemetry/sdk-trace-base";
2
+ import { WebTracerProvider } from "@opentelemetry/sdk-trace-web";
3
+ import { DocumentLoadInstrumentation } from "@opentelemetry/instrumentation-document-load";
4
+ import { ZoneContextManager } from "@opentelemetry/context-zone";
5
+ import { registerInstrumentations } from "@opentelemetry/instrumentation";
6
+ import { XMLHttpRequestInstrumentation } from "@opentelemetry/instrumentation-xml-http-request";
7
+ import { FetchInstrumentation } from "@opentelemetry/instrumentation-fetch";
8
+ import { W3CTraceContextPropagator } from "@opentelemetry/core";
9
+ import { resourceFromAttributes } from "@opentelemetry/resources";
10
+ import { ATTR_SERVICE_NAME } from "@opentelemetry/semantic-conventions";
11
+ import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http";
12
+ export const configureOpenTelemetry = (config, sessionId) => {
13
+ //create a resource with service name and attributes
14
+ const { serviceName, resourceAttributes, instrumentations = [] } = config;
15
+ const SESSION_ID = sessionId;
16
+ const resource = resourceFromAttributes({
17
+ [ATTR_SERVICE_NAME]: serviceName,
18
+ "at-project-key": config.projectId,
19
+ ...(resourceAttributes || {}),
20
+ });
21
+ const otlpExporter = new OTLPTraceExporter({
22
+ url: config.exporterEndpoint || "http://otelcol.apitoolkit.io:4318", // HTTP endpoint (note the :4318 port)
23
+ headers: {},
24
+ });
25
+ const provider = new WebTracerProvider({
26
+ resource: resource,
27
+ spanProcessors: [new BatchSpanProcessor(otlpExporter)],
28
+ });
29
+ provider.register({
30
+ contextManager: new ZoneContextManager(),
31
+ propagator: new W3CTraceContextPropagator(),
32
+ });
33
+ registerInstrumentations({
34
+ tracerProvider: provider,
35
+ instrumentations: [
36
+ ...instrumentations,
37
+ new DocumentLoadInstrumentation({
38
+ applyCustomAttributesOnSpan: {
39
+ documentLoad: (span) => {
40
+ span.setAttribute("session.id", SESSION_ID);
41
+ },
42
+ },
43
+ }),
44
+ new XMLHttpRequestInstrumentation({
45
+ propagateTraceHeaderCorsUrls: [/^http:\/\/localhost:3000/],
46
+ applyCustomAttributesOnSpan: (span, xhr) => {
47
+ span.setAttribute("session.id", SESSION_ID);
48
+ },
49
+ }),
50
+ new FetchInstrumentation({
51
+ propagateTraceHeaderCorsUrls: [/^http:\/\/localhost:3000/],
52
+ applyCustomAttributesOnSpan: (span, request) => {
53
+ span.setAttribute("session.id", SESSION_ID);
54
+ },
55
+ }),
56
+ ],
57
+ });
58
+ };
@@ -0,0 +1 @@
1
+ {"root":["../src/index.ts","../src/replay.ts","../src/tracing.ts","../src/types.ts"],"version":"5.8.3"}
@@ -0,0 +1,12 @@
1
+ export type MonoscopeConfig = {
2
+ serviceName: string;
3
+ exporterEndpoint: string;
4
+ propagateTraceHeaderCorsUrls?: RegExp[];
5
+ projectId: string;
6
+ resourceAttributes?: Record<string, string>;
7
+ documentLoadAttributes?: (span: any) => void;
8
+ xhrAttributes?: (span: any, xhr: any) => void;
9
+ fetchAttributes?: (span: any, request: any) => void;
10
+ instrumentations?: any[];
11
+ replayEndpoint: string;
12
+ };
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "@monoscopetech/browser",
3
+ "version": "0.1.0",
4
+ "main": "dist/index.js",
5
+ "types": "dist/index.d.ts",
6
+ "type": "module",
7
+ "dependencies": {
8
+ "@opentelemetry/context-zone": "^2.0.1",
9
+ "@opentelemetry/core": "^2.0.1",
10
+ "@opentelemetry/exporter-logs-otlp-http": "^0.203.0",
11
+ "@opentelemetry/exporter-metrics-otlp-http": "^0.203.0",
12
+ "@opentelemetry/exporter-trace-otlp-http": "^0.203.0",
13
+ "@opentelemetry/instrumentation": "^0.203.0",
14
+ "@opentelemetry/instrumentation-document-load": "^0.48.0",
15
+ "@opentelemetry/instrumentation-fetch": "^0.203.0",
16
+ "@opentelemetry/instrumentation-xml-http-request": "^0.203.0",
17
+ "@opentelemetry/resources": "^2.0.1",
18
+ "@opentelemetry/sdk-trace-base": "^2.0.1",
19
+ "@opentelemetry/sdk-trace-web": "^2.0.1",
20
+ "@opentelemetry/semantic-conventions": "^1.36.0",
21
+ "rrweb": "2.0.0-alpha.4",
22
+ "uuid": "^11.1.0"
23
+ },
24
+ "files": [
25
+ "dist"
26
+ ],
27
+ "publishConfig": {
28
+ "access": "public"
29
+ },
30
+ "keywords": [],
31
+ "author": "",
32
+ "license": "MIT",
33
+ "devDependencies": {
34
+ "@rollup/plugin-commonjs": "^25.0.0",
35
+ "@rollup/plugin-json": "^6.1.0",
36
+ "@rollup/plugin-node-resolve": "^15.0.0",
37
+ "@rollup/plugin-terser": "^0.4.4",
38
+ "@rollup/plugin-typescript": "^11.0.0",
39
+ "@types/uuid": "^10.0.0",
40
+ "buffer": "^5.5.0||^6.0.0",
41
+ "rollup": "^4.0.0",
42
+ "tslib": "^2.8.1",
43
+ "typescript": "^5.0.0"
44
+ },
45
+ "scripts": {
46
+ "build": "rollup -c",
47
+ "lint": "eslint src --ext .ts"
48
+ }
49
+ }