@monoscopetech/browser 0.3.2 → 0.4.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/dist/index.d.ts CHANGED
@@ -1,10 +1,13 @@
1
1
  import { MonoscopeReplay } from "./replay";
2
- import { MonoscopeConfig } from "./types";
2
+ import { OpenTelemetryManager } from "./tracing";
3
+ import { MonoscopeConfig, MonoscopeUser } from "./types";
3
4
  declare class Monoscope {
4
5
  replay: MonoscopeReplay;
5
6
  config: MonoscopeConfig;
7
+ otel: OpenTelemetryManager;
6
8
  sessionId: string;
7
9
  constructor(config: MonoscopeConfig);
8
10
  getSessionId(): string;
11
+ setUser(u: MonoscopeUser): void;
9
12
  }
10
13
  export default Monoscope;
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { MonoscopeReplay } from "./replay";
2
- import { configureOpenTelemetry } from "./tracing";
2
+ import { OpenTelemetryManager } from "./tracing";
3
3
  import { v4 as uuidv4 } from "uuid";
4
4
  class Monoscope {
5
5
  constructor(config) {
@@ -14,13 +14,18 @@ class Monoscope {
14
14
  this.sessionId = uuidv4();
15
15
  sessionStorage.setItem("monoscope-session-id", this.sessionId);
16
16
  }
17
- configureOpenTelemetry(config, this.sessionId);
18
17
  this.config = config;
19
18
  this.replay = new MonoscopeReplay(config, this.sessionId);
19
+ this.otel = new OpenTelemetryManager(config, this.sessionId);
20
+ this.otel.configure();
20
21
  this.replay.configure();
22
+ window.monoscope = this;
21
23
  }
22
24
  getSessionId() {
23
25
  return this.sessionId;
24
26
  }
27
+ setUser(u) {
28
+ this.otel.setUser(u);
29
+ }
25
30
  }
26
31
  export default Monoscope;
package/dist/replay.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { getRecordConsolePlugin } from "@rrweb/rrweb-plugin-console-record";
1
2
  import * as rrweb from "rrweb";
2
3
  export class MonoscopeReplay {
3
4
  constructor(config, sessionId) {
@@ -14,6 +15,17 @@ export class MonoscopeReplay {
14
15
  emit: (event) => {
15
16
  this.events.push(event);
16
17
  },
18
+ plugins: [
19
+ getRecordConsolePlugin({
20
+ level: ["info", "log", "warn", "error"],
21
+ lengthThreshold: 10000,
22
+ stringifyOptions: {
23
+ stringLengthLimit: 1000,
24
+ numOfKeysLimit: 100,
25
+ depthOfLimit: 1,
26
+ },
27
+ }),
28
+ ],
17
29
  });
18
30
  setInterval(this.save, 15 * 1000);
19
31
  }
@@ -29,7 +41,11 @@ export class MonoscopeReplay {
29
41
  }
30
42
  const events = this.events;
31
43
  this.events = [];
32
- const body = JSON.stringify({ events, sessionId: this.sessionId });
44
+ const body = JSON.stringify({
45
+ events,
46
+ sessionId: this.sessionId,
47
+ timestamp: new Date().toISOString(),
48
+ });
33
49
  fetch(replayEventsBaseUrl, {
34
50
  mode: "no-cors",
35
51
  method: "POST",
package/dist/tracing.d.ts CHANGED
@@ -1,2 +1,12 @@
1
- import { MonoscopeConfig } from "./types";
2
- export declare const configureOpenTelemetry: (config: MonoscopeConfig, sessionId: string) => void;
1
+ import { MonoscopeConfig, MonoscopeUser } from "./types";
2
+ export declare class OpenTelemetryManager {
3
+ private config;
4
+ private sessionId;
5
+ private provider;
6
+ constructor(config: MonoscopeConfig, sessionId: string);
7
+ private createProvider;
8
+ configure(): void;
9
+ private setUserAttributes;
10
+ shutdown(): Promise<void>;
11
+ setUser(newConfig: MonoscopeUser): void;
12
+ }
package/dist/tracing.js CHANGED
@@ -9,62 +9,93 @@ import { W3CTraceContextPropagator } from "@opentelemetry/core";
9
9
  import { resourceFromAttributes } from "@opentelemetry/resources";
10
10
  import { ATTR_SERVICE_NAME } from "@opentelemetry/semantic-conventions";
11
11
  import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http";
12
- export const configureOpenTelemetry = (config, sessionId) => {
13
- const { serviceName, resourceAttributes, instrumentations = [], propagateTraceHeaderCorsUrls, } = config;
14
- const SESSION_ID = sessionId;
15
- const resource = resourceFromAttributes({
16
- [ATTR_SERVICE_NAME]: serviceName,
17
- "at-project-id": config.projectId,
18
- ...(resourceAttributes || {}),
19
- });
20
- const otlpExporter = new OTLPTraceExporter({
21
- url: config.exporterEndpoint || "http://otelcol.apitoolkit.io:4318/v1/traces",
22
- headers: {},
23
- });
24
- const provider = new WebTracerProvider({
25
- resource: resource,
26
- spanProcessors: [new BatchSpanProcessor(otlpExporter)],
27
- });
28
- provider.register({
29
- contextManager: new ZoneContextManager(),
30
- propagator: new W3CTraceContextPropagator(),
31
- });
32
- const headerUrls = propagateTraceHeaderCorsUrls || [/^https?:\/\/.*/];
33
- const ignoreUrls = [
34
- /^https?:\/\/(?:[^\/]+\.)?apitoolkit\.io\//,
35
- /^https?:\/\/(?:[^\/]+\.)?monoscope\.tech\//,
36
- ];
37
- registerInstrumentations({
38
- tracerProvider: provider,
39
- instrumentations: [
40
- ...instrumentations,
41
- new DocumentLoadInstrumentation({
42
- applyCustomAttributesOnSpan: {
43
- documentLoad: (span) => {
44
- span.setAttribute("session.id", SESSION_ID);
12
+ export class OpenTelemetryManager {
13
+ constructor(config, sessionId) {
14
+ this.config = config;
15
+ this.sessionId = sessionId;
16
+ this.provider = this.createProvider();
17
+ }
18
+ createProvider() {
19
+ const { serviceName, resourceAttributes, exporterEndpoint, projectId } = this.config;
20
+ const resource = resourceFromAttributes({
21
+ [ATTR_SERVICE_NAME]: serviceName,
22
+ "at-project-id": projectId,
23
+ ...(resourceAttributes || {}),
24
+ });
25
+ const otlpExporter = new OTLPTraceExporter({
26
+ url: exporterEndpoint || "http://otelcol.apitoolkit.io:4318/v1/traces",
27
+ headers: {},
28
+ });
29
+ return new WebTracerProvider({
30
+ resource,
31
+ spanProcessors: [new BatchSpanProcessor(otlpExporter)],
32
+ });
33
+ }
34
+ configure() {
35
+ this.provider.register({
36
+ contextManager: new ZoneContextManager(),
37
+ propagator: new W3CTraceContextPropagator(),
38
+ });
39
+ const headerUrls = this.config.propagateTraceHeaderCorsUrls || [
40
+ /^https?:\/\/.*/,
41
+ ];
42
+ const ignoreUrls = [
43
+ /^https?:\/\/(?:[^\/]+\.)?apitoolkit\.io\//,
44
+ /^https?:\/\/(?:[^\/]+\.)?monoscope\.tech\//,
45
+ ];
46
+ registerInstrumentations({
47
+ tracerProvider: this.provider,
48
+ instrumentations: [
49
+ ...(this.config.instrumentations || []),
50
+ new DocumentLoadInstrumentation({
51
+ applyCustomAttributesOnSpan: {
52
+ documentLoad: (span) => {
53
+ span.setAttribute("session.id", this.sessionId);
54
+ this.setUserAttributes(span);
55
+ },
56
+ documentFetch: (span) => {
57
+ span.setAttribute("session.id", this.sessionId);
58
+ this.setUserAttributes(span);
59
+ },
60
+ resourceFetch: (span) => {
61
+ span.setAttribute("session.id", this.sessionId);
62
+ this.setUserAttributes(span);
63
+ },
45
64
  },
46
- documentFetch: (span) => {
47
- span.setAttribute("session.id", SESSION_ID);
65
+ }),
66
+ new XMLHttpRequestInstrumentation({
67
+ propagateTraceHeaderCorsUrls: headerUrls,
68
+ ignoreUrls,
69
+ applyCustomAttributesOnSpan: (span) => {
70
+ span.setAttribute("session.id", this.sessionId);
71
+ this.setUserAttributes(span);
48
72
  },
49
- resourceFetch: (span) => {
50
- span.setAttribute("session.id", SESSION_ID);
73
+ }),
74
+ new FetchInstrumentation({
75
+ propagateTraceHeaderCorsUrls: headerUrls,
76
+ ignoreUrls,
77
+ applyCustomAttributesOnSpan: (span) => {
78
+ span.setAttribute("session.id", this.sessionId);
79
+ this.setUserAttributes(span);
51
80
  },
52
- },
53
- }),
54
- new XMLHttpRequestInstrumentation({
55
- propagateTraceHeaderCorsUrls: headerUrls,
56
- ignoreUrls,
57
- applyCustomAttributesOnSpan: (span, xhr) => {
58
- span.setAttribute("session.id", SESSION_ID);
59
- },
60
- }),
61
- new FetchInstrumentation({
62
- propagateTraceHeaderCorsUrls: headerUrls,
63
- ignoreUrls,
64
- applyCustomAttributesOnSpan: (span, request) => {
65
- span.setAttribute("session.id", SESSION_ID);
66
- },
67
- }),
68
- ],
69
- });
70
- };
81
+ }),
82
+ ],
83
+ });
84
+ }
85
+ setUserAttributes(span) {
86
+ if (this.config.user) {
87
+ for (let k in this.config.user) {
88
+ span.setAttribute(`user.${k}`, this.config.user[k]);
89
+ }
90
+ }
91
+ }
92
+ async shutdown() {
93
+ await this.provider.shutdown();
94
+ }
95
+ setUser(newConfig) {
96
+ this.config = {
97
+ ...this.config,
98
+ user: { ...this.config.user, ...newConfig },
99
+ };
100
+ }
101
+ }
package/dist/types.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import Monoscope from ".";
1
2
  export type MonoscopeConfig = {
2
3
  serviceName: string;
3
4
  exporterEndpoint?: string;
@@ -6,4 +7,17 @@ export type MonoscopeConfig = {
6
7
  resourceAttributes?: Record<string, string>;
7
8
  instrumentations?: any[];
8
9
  replayEventsBaseUrl?: string;
10
+ user?: MonoscopeUser;
9
11
  };
12
+ export type MonoscopeUser = {
13
+ email?: string;
14
+ fullName?: string;
15
+ name?: string;
16
+ id?: string;
17
+ roles?: string[];
18
+ } & Record<string, string>;
19
+ declare global {
20
+ interface Window {
21
+ monoscope: Monoscope;
22
+ }
23
+ }
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@monoscopetech/browser",
3
- "version": "0.3.2",
3
+ "version": "0.4.2",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "type": "module",
7
- "unpkg": "dis/monoscopetech-browser.umd.js",
7
+ "unpkg": "dist/monoscopetech-browser.umd.js",
8
8
  "dependencies": {
9
9
  "@opentelemetry/context-zone": "^2.0.1",
10
10
  "@opentelemetry/core": "^2.0.1",
@@ -19,6 +19,7 @@
19
19
  "@opentelemetry/sdk-trace-base": "^2.0.1",
20
20
  "@opentelemetry/sdk-trace-web": "^2.0.1",
21
21
  "@opentelemetry/semantic-conventions": "^1.36.0",
22
+ "@rrweb/rrweb-plugin-console-record": "2.0.0-alpha.18",
22
23
  "rrweb": "2.0.0-alpha.4",
23
24
  "uuid": "^11.1.0"
24
25
  },
@@ -45,6 +46,7 @@
45
46
  },
46
47
  "scripts": {
47
48
  "build": "rollup -c",
48
- "lint": "eslint src --ext .ts"
49
+ "lint": "eslint src --ext .ts",
50
+ "prePublishOnly": "npx tsc --build"
49
51
  }
50
52
  }