@ogcio/o11y-sdk-react 0.1.0-beta.2 → 0.1.0-beta.3

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.
@@ -0,0 +1,39 @@
1
+ interface SDKConfig {
2
+ /**
3
+ * The opentelemetry collector entrypoint GRPC url.
4
+ * If the collectoUrl is null or undefined, the instrumentation will not be activated.
5
+ * @example http://alloy:4317
6
+ */
7
+ collectorUrl: string;
8
+ /**
9
+ * Name of your application used for the collector to group logs
10
+ */
11
+ serviceName?: string;
12
+ /**
13
+ * Diagnostic log level for the internal runtime instrumentation
14
+ *
15
+ * @type string
16
+ * @default INFO
17
+ */
18
+ diagLogLevel?: SDKLogLevel;
19
+ /**
20
+ * Collector signals processing mode.
21
+ * signle: makes an http/grpc request for each signal and it is immediately processed inside grafana
22
+ * batch: sends multiple signals within a time window, optimized to reduce http/grpc calls in production
23
+ *
24
+ * @type string
25
+ * @default batch
26
+ */
27
+ collectorMode?: SDKCollectorMode;
28
+ }
29
+ export interface FaroSDKConfig extends SDKConfig {
30
+ /**
31
+ * String with single or multiple URLs separated by comma used by the instrumentation to ignore CORS errors and send signals through it
32
+ * @example http://localhost:9000,http://localhost:8000
33
+ * @example http://localhost:9000
34
+ */
35
+ corsTraceHeaders?: string;
36
+ }
37
+ export type SDKCollectorMode = "single" | "batch";
38
+ export type SDKLogLevel = "NONE" | "ERROR" | "WARN" | "INFO" | "DEBUG" | "VERBOSE" | "ALL";
39
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,3 @@
1
+ import type { FaroSDKConfig } from "./index.js";
2
+ import { type Faro } from "@grafana/faro-react";
3
+ export default function buildFaroInstrumentation(config?: FaroSDKConfig): Faro | undefined;
@@ -0,0 +1,61 @@
1
+ import { ErrorsInstrumentation, getWebInstrumentations, initializeFaro, WebVitalsInstrumentation, } from "@grafana/faro-react";
2
+ import { diag, DiagConsoleLogger, DiagLogLevel } from "@opentelemetry/api";
3
+ import { W3CTraceContextPropagator } from "@opentelemetry/core";
4
+ import isUrl from "is-url";
5
+ import { FetchInstrumentation } from "@opentelemetry/instrumentation-fetch";
6
+ import { UserInteractionInstrumentation } from "@opentelemetry/instrumentation-user-interaction";
7
+ import { DocumentLoadInstrumentation } from "@opentelemetry/instrumentation-document-load";
8
+ import { TracingInstrumentation } from "@grafana/faro-web-tracing";
9
+ export default function buildFaroInstrumentation(config) {
10
+ if (!config) {
11
+ console.warn("observability config not set. Skipping Faro OpenTelemetry instrumentation.");
12
+ return;
13
+ }
14
+ if (!config.collectorUrl) {
15
+ console.warn("collectorUrl not set. Skipping Faro OpenTelemetry instrumentation.");
16
+ return;
17
+ }
18
+ if (!isUrl(config.collectorUrl)) {
19
+ console.error("collectorUrl does not use a valid format. Skipping Faro OpenTelemetry instrumentation.");
20
+ return;
21
+ }
22
+ try {
23
+ diag.setLogger(new DiagConsoleLogger(), config.diagLogLevel
24
+ ? DiagLogLevel[config.diagLogLevel]
25
+ : DiagLogLevel.INFO);
26
+ const faro = initializeFaro({
27
+ url: config.collectorUrl,
28
+ app: {
29
+ name: config.serviceName,
30
+ },
31
+ batching: {
32
+ enabled: !(config.collectorMode === "single"),
33
+ },
34
+ instrumentations: [
35
+ ...getWebInstrumentations({
36
+ captureConsole: true,
37
+ }),
38
+ new ErrorsInstrumentation(),
39
+ new WebVitalsInstrumentation(),
40
+ new TracingInstrumentation({
41
+ propagator: new W3CTraceContextPropagator(),
42
+ instrumentations: [
43
+ new DocumentLoadInstrumentation(),
44
+ new FetchInstrumentation({
45
+ ignoreUrls: [config.collectorUrl], // ignore collector to avoid fetch loop
46
+ propagateTraceHeaderCorsUrls: config.corsTraceHeaders
47
+ ? config.corsTraceHeaders.split(",").map((s) => new RegExp(s))
48
+ : undefined,
49
+ }),
50
+ new UserInteractionInstrumentation(),
51
+ ],
52
+ }),
53
+ ],
54
+ });
55
+ console.log("Faro OpenTelemetry instrumentation started successfully.");
56
+ return faro;
57
+ }
58
+ catch (error) {
59
+ console.error("Error starting Faro OpenTelemetry instrumentation:", error);
60
+ }
61
+ }
@@ -0,0 +1,2 @@
1
+ declare const _default: import("vite").UserConfig;
2
+ export default _default;
@@ -0,0 +1,25 @@
1
+ import { defineConfig } from "vitest/config";
2
+ export default defineConfig({
3
+ test: {
4
+ globals: true,
5
+ watch: false,
6
+ include: ["**/test/*.test.ts"],
7
+ exclude: ["**/fixtures/**", "**/dist/**"],
8
+ poolOptions: {
9
+ threads: {
10
+ maxThreads: 8,
11
+ },
12
+ },
13
+ clearMocks: true,
14
+ testTimeout: 30_000,
15
+ coverage: {
16
+ enabled: true,
17
+ provider: "v8",
18
+ reportsDirectory: "coverage",
19
+ reporter: ["lcov", "cobertura"],
20
+ clean: true,
21
+ },
22
+ reporters: ["default", ["junit", { outputFile: "test-report.xml" }]],
23
+ environment: "node",
24
+ },
25
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ogcio/o11y-sdk-react",
3
- "version": "0.1.0-beta.2",
3
+ "version": "0.1.0-beta.3",
4
4
  "description": "Opentelemetry standard instrumentation SDK for React based project",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
@@ -0,0 +1,17 @@
1
+ <?xml version="1.0" encoding="UTF-8" ?>
2
+ <testsuites name="vitest tests" tests="5" failures="0" errors="0" time="0.015040706">
3
+ <testsuite name="test/index.test.ts" timestamp="2025-01-28T15:51:41.666Z" hostname="fv-az633-589" tests="2" failures="0" errors="0" skipped="0" time="0.006099583">
4
+ <testcase classname="test/index.test.ts" name="instrumentFaro &gt; should call buildFaroInstrumentation with the provided config" time="0.003494948">
5
+ </testcase>
6
+ <testcase classname="test/index.test.ts" name="instrumentFaro &gt; should not throw when called without arguments" time="0.001179316">
7
+ </testcase>
8
+ </testsuite>
9
+ <testsuite name="test/validation.test.ts" timestamp="2025-01-28T15:51:41.667Z" hostname="fv-az633-589" tests="3" failures="0" errors="0" skipped="0" time="0.008941123">
10
+ <testcase classname="test/validation.test.ts" name="validation config: should return without breaking the execution &gt; should call buildFaroInstrumentation without config and skip instrumentation" time="0.004579762">
11
+ </testcase>
12
+ <testcase classname="test/validation.test.ts" name="validation config: should return without breaking the execution &gt; faro instrumentation: url undefined" time="0.001722924">
13
+ </testcase>
14
+ <testcase classname="test/validation.test.ts" name="validation config: should return without breaking the execution &gt; faro instrumentation: invalid url" time="0.000789311">
15
+ </testcase>
16
+ </testsuite>
17
+ </testsuites>