@pagopa/azure-tracing 0.3.0 → 0.3.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 CHANGED
@@ -43,10 +43,10 @@ For more background on this workaround, see:
43
43
 
44
44
  In order to enable tracing, you also need to set the following environment variables:
45
45
 
46
- | **Name** | **Required** | **Default** |
47
- | ----------------------------------- | ------------ | ----------- |
48
- | **APPINSIGHTS_SAMPLING_PERCENTAGE** | false | 5 |
49
- | **APPINSIGHTS_CONNECTION_STRING** | true | - |
46
+ | **Name** | **Required** | **Default** |
47
+ | ----------------------------------------- | ------------ | ----------- |
48
+ | **APPINSIGHTS_SAMPLING_PERCENTAGE** | false | 5 |
49
+ | **APPLICATIONINSIGHTS_CONNECTION_STRING** | true | - |
50
50
 
51
51
  #### Step 2: Register Azure Function Lifecycle Hooks
52
52
 
@@ -21,7 +21,6 @@ var loadEnv = () => createEnv({
21
21
  },
22
22
  runtimeEnv: process.env,
23
23
  server: {
24
- APPINSIGHTS_CONNECTION_STRING: z.string().describe("The connection string for Application Insights."),
25
24
  APPINSIGHTS_SAMPLING_PERCENTAGE: z.optional(
26
25
  z.coerce.number().min(0).max(100).default(5).describe(
27
26
  "Application Insights sampling percentage between 0 and 100. If not set, defaults to 5."
@@ -29,7 +28,8 @@ var loadEnv = () => createEnv({
29
28
  ).transform((value) => {
30
29
  const percentage = Number(value);
31
30
  return isNaN(percentage) ? 5 : percentage;
32
- }).transform((value) => value / 100)
31
+ }).transform((value) => value / 100),
32
+ APPLICATIONINSIGHTS_CONNECTION_STRING: z.string().describe("The connection string for Application Insights.")
33
33
  }
34
34
  });
35
35
 
@@ -38,7 +38,7 @@ var initFromEnv = () => {
38
38
  const env = loadEnv();
39
39
  return useAzureMonitor({
40
40
  azureMonitorExporterOptions: {
41
- connectionString: env.APPINSIGHTS_CONNECTION_STRING
41
+ connectionString: env.APPLICATIONINSIGHTS_CONNECTION_STRING
42
42
  },
43
43
  enableLiveMetrics: true,
44
44
  samplingRatio: env.APPINSIGHTS_SAMPLING_PERCENTAGE
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/azure/functions/index.mts","../../src/azure/monitor/start-from-env.ts","../../src/azure/monitor/env.ts","../../src/azure/opentelemetry/azure-undici-instrumentation.ts"],"sourcesContent":["/*\nThis file is required to instrument ESM application to use OpenTelemetry.\nThis file must be pre-loaded through the `NODE_OPTIONS` environment variable to\nhave a fully instrumented application.\n */\n\nimport { createAddHookMessageChannel } from \"import-in-the-middle\";\nimport { register } from \"module\";\n\nconst { registerOptions, waitForAllMessagesAcknowledged } =\n createAddHookMessageChannel();\n\nregister(\"import-in-the-middle/hook.mjs\", import.meta.url, registerOptions);\n\nimport { metrics, trace } from \"@opentelemetry/api\";\nimport { registerInstrumentations } from \"@opentelemetry/instrumentation\";\n\nimport { initFromEnv } from \"../monitor/start-from-env\";\nimport { registerUndiciInstrumentation } from \"../opentelemetry/azure-undici-instrumentation\";\n\ninitFromEnv();\n\nregisterInstrumentations({\n instrumentations: [registerUndiciInstrumentation()],\n meterProvider: metrics.getMeterProvider(),\n tracerProvider: trace.getTracerProvider(),\n});\n\nawait waitForAllMessagesAcknowledged();\n","import { useAzureMonitor } from \"@azure/monitor-opentelemetry\";\n\nimport { loadEnv } from \"./env\";\n\nexport const initFromEnv = () => {\n const env = loadEnv();\n\n return useAzureMonitor({\n azureMonitorExporterOptions: {\n connectionString: env.APPINSIGHTS_CONNECTION_STRING,\n },\n enableLiveMetrics: true,\n samplingRatio: env.APPINSIGHTS_SAMPLING_PERCENTAGE,\n });\n};\n","// Load and type check environment variables on runtime\nimport { createEnv } from \"@t3-oss/env-core\";\nimport { z } from \"zod\";\n\nexport const loadEnv = () =>\n createEnv({\n emptyStringAsUndefined: true,\n onValidationError: (errors) => {\n throw new Error(\n errors\n .map(\n (error) => `Environment variable ${error.path} - ${error.message}`,\n )\n .join(\", \"),\n );\n },\n runtimeEnv: process.env,\n server: {\n APPINSIGHTS_CONNECTION_STRING: z\n .string()\n .describe(\"The connection string for Application Insights.\"),\n APPINSIGHTS_SAMPLING_PERCENTAGE: z\n .optional(\n z.coerce\n .number()\n .min(0)\n .max(100)\n .default(5)\n .describe(\n \"Application Insights sampling percentage between 0 and 100. If not set, defaults to 5.\",\n ),\n )\n .transform((value) => {\n const percentage = Number(value);\n return isNaN(percentage) ? 5 : percentage;\n })\n .transform((value) => value / 100),\n },\n });\n","import { UndiciInstrumentation } from \"@opentelemetry/instrumentation-undici\";\n\n// instrument native node fetch\nexport const registerUndiciInstrumentation = () =>\n new UndiciInstrumentation({\n requestHook: (span, requestInfo) => {\n const { method, origin, path } = requestInfo;\n // Default instrumented attributes don't feed well into AppInsights,\n // so we set them manually.\n span.setAttributes({\n \"http.host\": origin,\n \"http.method\": method,\n \"http.target\": path,\n \"http.url\": `${origin}${path}`,\n });\n },\n responseHook: (span, { response }) => {\n // Same as above, set the status code manually.\n span.setAttribute(\"http.status_code\", response.statusCode);\n },\n });\n"],"mappings":";AAMA,SAAS,mCAAmC;AAC5C,SAAS,gBAAgB;AAOzB,SAAS,SAAS,aAAa;AAC/B,SAAS,gCAAgC;;;ACfzC,SAAS,uBAAuB;;;ACChC,SAAS,iBAAiB;AAC1B,SAAS,SAAS;AAEX,IAAM,UAAU,MACrB,UAAU;AAAA,EACR,wBAAwB;AAAA,EACxB,mBAAmB,CAAC,WAAW;AAC7B,UAAM,IAAI;AAAA,MACR,OACG;AAAA,QACC,CAAC,UAAU,wBAAwB,MAAM,IAAI,MAAM,MAAM,OAAO;AAAA,MAClE,EACC,KAAK,IAAI;AAAA,IACd;AAAA,EACF;AAAA,EACA,YAAY,QAAQ;AAAA,EACpB,QAAQ;AAAA,IACN,+BAA+B,EAC5B,OAAO,EACP,SAAS,iDAAiD;AAAA,IAC7D,iCAAiC,EAC9B;AAAA,MACC,EAAE,OACC,OAAO,EACP,IAAI,CAAC,EACL,IAAI,GAAG,EACP,QAAQ,CAAC,EACT;AAAA,QACC;AAAA,MACF;AAAA,IACJ,EACC,UAAU,CAAC,UAAU;AACpB,YAAM,aAAa,OAAO,KAAK;AAC/B,aAAO,MAAM,UAAU,IAAI,IAAI;AAAA,IACjC,CAAC,EACA,UAAU,CAAC,UAAU,QAAQ,GAAG;AAAA,EACrC;AACF,CAAC;;;ADlCI,IAAM,cAAc,MAAM;AAC/B,QAAM,MAAM,QAAQ;AAEpB,SAAO,gBAAgB;AAAA,IACrB,6BAA6B;AAAA,MAC3B,kBAAkB,IAAI;AAAA,IACxB;AAAA,IACA,mBAAmB;AAAA,IACnB,eAAe,IAAI;AAAA,EACrB,CAAC;AACH;;;AEdA,SAAS,6BAA6B;AAG/B,IAAM,gCAAgC,MAC3C,IAAI,sBAAsB;AAAA,EACxB,aAAa,CAAC,MAAM,gBAAgB;AAClC,UAAM,EAAE,QAAQ,QAAQ,KAAK,IAAI;AAGjC,SAAK,cAAc;AAAA,MACjB,aAAa;AAAA,MACb,eAAe;AAAA,MACf,eAAe;AAAA,MACf,YAAY,GAAG,MAAM,GAAG,IAAI;AAAA,IAC9B,CAAC;AAAA,EACH;AAAA,EACA,cAAc,CAAC,MAAM,EAAE,SAAS,MAAM;AAEpC,SAAK,aAAa,oBAAoB,SAAS,UAAU;AAAA,EAC3D;AACF,CAAC;;;AHXH,IAAM,EAAE,iBAAiB,+BAA+B,IACtD,4BAA4B;AAE9B,SAAS,iCAAiC,YAAY,KAAK,eAAe;AAQ1E,YAAY;AAEZ,yBAAyB;AAAA,EACvB,kBAAkB,CAAC,8BAA8B,CAAC;AAAA,EAClD,eAAe,QAAQ,iBAAiB;AAAA,EACxC,gBAAgB,MAAM,kBAAkB;AAC1C,CAAC;AAED,MAAM,+BAA+B;","names":[]}
1
+ {"version":3,"sources":["../../src/azure/functions/index.mts","../../src/azure/monitor/start-from-env.ts","../../src/azure/monitor/env.ts","../../src/azure/opentelemetry/azure-undici-instrumentation.ts"],"sourcesContent":["/*\nThis file is required to instrument ESM application to use OpenTelemetry.\nThis file must be pre-loaded through the `NODE_OPTIONS` environment variable to\nhave a fully instrumented application.\n */\n\nimport { createAddHookMessageChannel } from \"import-in-the-middle\";\nimport { register } from \"module\";\n\nconst { registerOptions, waitForAllMessagesAcknowledged } =\n createAddHookMessageChannel();\n\nregister(\"import-in-the-middle/hook.mjs\", import.meta.url, registerOptions);\n\nimport { metrics, trace } from \"@opentelemetry/api\";\nimport { registerInstrumentations } from \"@opentelemetry/instrumentation\";\n\nimport { initFromEnv } from \"../monitor/start-from-env\";\nimport { registerUndiciInstrumentation } from \"../opentelemetry/azure-undici-instrumentation\";\n\ninitFromEnv();\n\nregisterInstrumentations({\n instrumentations: [registerUndiciInstrumentation()],\n meterProvider: metrics.getMeterProvider(),\n tracerProvider: trace.getTracerProvider(),\n});\n\nawait waitForAllMessagesAcknowledged();\n","import { useAzureMonitor } from \"@azure/monitor-opentelemetry\";\n\nimport { loadEnv } from \"./env\";\n\nexport const initFromEnv = () => {\n const env = loadEnv();\n\n return useAzureMonitor({\n azureMonitorExporterOptions: {\n connectionString: env.APPLICATIONINSIGHTS_CONNECTION_STRING,\n },\n enableLiveMetrics: true,\n samplingRatio: env.APPINSIGHTS_SAMPLING_PERCENTAGE,\n });\n};\n","// Load and type check environment variables on runtime\nimport { createEnv } from \"@t3-oss/env-core\";\nimport { z } from \"zod\";\n\nexport const loadEnv = () =>\n createEnv({\n emptyStringAsUndefined: true,\n onValidationError: (errors) => {\n throw new Error(\n errors\n .map(\n (error) => `Environment variable ${error.path} - ${error.message}`,\n )\n .join(\", \"),\n );\n },\n runtimeEnv: process.env,\n server: {\n APPINSIGHTS_SAMPLING_PERCENTAGE: z\n .optional(\n z.coerce\n .number()\n .min(0)\n .max(100)\n .default(5)\n .describe(\n \"Application Insights sampling percentage between 0 and 100. If not set, defaults to 5.\",\n ),\n )\n .transform((value) => {\n const percentage = Number(value);\n return isNaN(percentage) ? 5 : percentage;\n })\n .transform((value) => value / 100),\n APPLICATIONINSIGHTS_CONNECTION_STRING: z\n .string()\n .describe(\"The connection string for Application Insights.\"),\n },\n });\n","import { UndiciInstrumentation } from \"@opentelemetry/instrumentation-undici\";\n\n// instrument native node fetch\nexport const registerUndiciInstrumentation = () =>\n new UndiciInstrumentation({\n requestHook: (span, requestInfo) => {\n const { method, origin, path } = requestInfo;\n // Default instrumented attributes don't feed well into AppInsights,\n // so we set them manually.\n span.setAttributes({\n \"http.host\": origin,\n \"http.method\": method,\n \"http.target\": path,\n \"http.url\": `${origin}${path}`,\n });\n },\n responseHook: (span, { response }) => {\n // Same as above, set the status code manually.\n span.setAttribute(\"http.status_code\", response.statusCode);\n },\n });\n"],"mappings":";AAMA,SAAS,mCAAmC;AAC5C,SAAS,gBAAgB;AAOzB,SAAS,SAAS,aAAa;AAC/B,SAAS,gCAAgC;;;ACfzC,SAAS,uBAAuB;;;ACChC,SAAS,iBAAiB;AAC1B,SAAS,SAAS;AAEX,IAAM,UAAU,MACrB,UAAU;AAAA,EACR,wBAAwB;AAAA,EACxB,mBAAmB,CAAC,WAAW;AAC7B,UAAM,IAAI;AAAA,MACR,OACG;AAAA,QACC,CAAC,UAAU,wBAAwB,MAAM,IAAI,MAAM,MAAM,OAAO;AAAA,MAClE,EACC,KAAK,IAAI;AAAA,IACd;AAAA,EACF;AAAA,EACA,YAAY,QAAQ;AAAA,EACpB,QAAQ;AAAA,IACN,iCAAiC,EAC9B;AAAA,MACC,EAAE,OACC,OAAO,EACP,IAAI,CAAC,EACL,IAAI,GAAG,EACP,QAAQ,CAAC,EACT;AAAA,QACC;AAAA,MACF;AAAA,IACJ,EACC,UAAU,CAAC,UAAU;AACpB,YAAM,aAAa,OAAO,KAAK;AAC/B,aAAO,MAAM,UAAU,IAAI,IAAI;AAAA,IACjC,CAAC,EACA,UAAU,CAAC,UAAU,QAAQ,GAAG;AAAA,IACnC,uCAAuC,EACpC,OAAO,EACP,SAAS,iDAAiD;AAAA,EAC/D;AACF,CAAC;;;ADlCI,IAAM,cAAc,MAAM;AAC/B,QAAM,MAAM,QAAQ;AAEpB,SAAO,gBAAgB;AAAA,IACrB,6BAA6B;AAAA,MAC3B,kBAAkB,IAAI;AAAA,IACxB;AAAA,IACA,mBAAmB;AAAA,IACnB,eAAe,IAAI;AAAA,EACrB,CAAC;AACH;;;AEdA,SAAS,6BAA6B;AAG/B,IAAM,gCAAgC,MAC3C,IAAI,sBAAsB;AAAA,EACxB,aAAa,CAAC,MAAM,gBAAgB;AAClC,UAAM,EAAE,QAAQ,QAAQ,KAAK,IAAI;AAGjC,SAAK,cAAc;AAAA,MACjB,aAAa;AAAA,MACb,eAAe;AAAA,MACf,eAAe;AAAA,MACf,YAAY,GAAG,MAAM,GAAG,IAAI;AAAA,IAC9B,CAAC;AAAA,EACH;AAAA,EACA,cAAc,CAAC,MAAM,EAAE,SAAS,MAAM;AAEpC,SAAK,aAAa,oBAAoB,SAAS,UAAU;AAAA,EAC3D;AACF,CAAC;;;AHXH,IAAM,EAAE,iBAAiB,+BAA+B,IACtD,4BAA4B;AAE9B,SAAS,iCAAiC,YAAY,KAAK,eAAe;AAQ1E,YAAY;AAEZ,yBAAyB;AAAA,EACvB,kBAAkB,CAAC,8BAA8B,CAAC;AAAA,EAClD,eAAe,QAAQ,iBAAiB;AAAA,EACxC,gBAAgB,MAAM,kBAAkB;AAC1C,CAAC;AAED,MAAM,+BAA+B;","names":[]}
@@ -2,21 +2,45 @@ import { AzureMonitorOpenTelemetryOptions } from '@azure/monitor-opentelemetry';
2
2
  import { Instrumentation } from '@opentelemetry/instrumentation';
3
3
 
4
4
  /**
5
- * Initialize the Azure Monitor with the given instrumentations.
6
- * Call this function to register the instrumentations with the Azure Monitor.
7
- * By default, the Undici instrumentation is included; if you need to add more,
8
- * you can pass them as an array of instrumentations.
5
+ * Initialize the Azure Monitor with the given instrumentations and options.
6
+ * This function sets up telemetry collection for your application by registering
7
+ * the provided instrumentations and configuring Azure Monitor OpenTelemetry.
8
+ *
9
+ * By default, the Undici instrumentation is included. You can extend the functionality
10
+ * by passing additional instrumentations as an array.
9
11
  *
10
12
  * @remarks
11
- * This function will register the given instrumentations with the Azure Monitor.
12
- * It is recommended to call this function at the beginning of your application.
13
+ * - This function should be called at the start of your application to ensure
14
+ * telemetry is collected from the beginning.
15
+ * - If `azureMonitorOptions` is not provided, the configuration will be initialized
16
+ * using environment variables. @see README for more details.
13
17
  *
14
18
  * @example
19
+ * // Basic usage with default settings
15
20
  * import { initAzureMonitor } from "@pagopa/azure-tracing/azure-monitor";
16
- *
17
21
  * initAzureMonitor();
18
- * @param instrumentations the list of instrumentations to register with the Azure Monitor.
19
- * @param azureMonitorOptions custom configuration for Azure Monitor. If not provided, then it will be initialized using the environment variables.
22
+ *
23
+ * @example
24
+ * // Usage with custom instrumentations
25
+ * import { initAzureMonitor } from "@pagopa/azure-tracing/azure-monitor";
26
+ * import { MyCustomInstrumentation } from "my-custom-instrumentation";
27
+ *
28
+ * initAzureMonitor([new MyCustomInstrumentation()]);
29
+ *
30
+ * @example
31
+ * // Usage with custom Azure Monitor options
32
+ * import { initAzureMonitor } from "@pagopa/azure-tracing/azure-monitor";
33
+ *
34
+ * initAzureMonitor([], {
35
+ * azureMonitorExporterOptions: {
36
+ * connectionString: "theConnectionString",
37
+ * },
38
+ * // other options...
39
+ * });
40
+ *
41
+ * @param instrumentations - The list of instrumentations to register with the Azure Monitor.
42
+ * @param azureMonitorOptions - Custom configuration for Azure Monitor. If not provided,
43
+ * it will be initialized using environment variables.
20
44
  */
21
45
  declare const initAzureMonitor: (instrumentations?: readonly Instrumentation[], azureMonitorOptions?: AzureMonitorOpenTelemetryOptions) => void;
22
46
 
@@ -41,7 +41,6 @@ var loadEnv = () => createEnv({
41
41
  },
42
42
  runtimeEnv: process.env,
43
43
  server: {
44
- APPINSIGHTS_CONNECTION_STRING: z.string().describe("The connection string for Application Insights."),
45
44
  APPINSIGHTS_SAMPLING_PERCENTAGE: z.optional(
46
45
  z.coerce.number().min(0).max(100).default(5).describe(
47
46
  "Application Insights sampling percentage between 0 and 100. If not set, defaults to 5."
@@ -49,7 +48,8 @@ var loadEnv = () => createEnv({
49
48
  ).transform((value) => {
50
49
  const percentage = Number(value);
51
50
  return isNaN(percentage) ? 5 : percentage;
52
- }).transform((value) => value / 100)
51
+ }).transform((value) => value / 100),
52
+ APPLICATIONINSIGHTS_CONNECTION_STRING: z.string().describe("The connection string for Application Insights.")
53
53
  }
54
54
  });
55
55
 
@@ -58,7 +58,7 @@ var initFromEnv = () => {
58
58
  const env = loadEnv();
59
59
  return useAzureMonitor({
60
60
  azureMonitorExporterOptions: {
61
- connectionString: env.APPINSIGHTS_CONNECTION_STRING
61
+ connectionString: env.APPLICATIONINSIGHTS_CONNECTION_STRING
62
62
  },
63
63
  enableLiveMetrics: true,
64
64
  samplingRatio: env.APPINSIGHTS_SAMPLING_PERCENTAGE
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/azure/monitor/index.ts","../../src/azure/opentelemetry/azure-undici-instrumentation.ts","../../src/azure/monitor/start-from-env.ts","../../src/azure/monitor/env.ts"],"sourcesContent":["import {\n AzureMonitorOpenTelemetryOptions,\n useAzureMonitor,\n} from \"@azure/monitor-opentelemetry\";\nimport { metrics, trace } from \"@opentelemetry/api\";\nimport {\n Instrumentation,\n registerInstrumentations,\n} from \"@opentelemetry/instrumentation\";\n\nimport { registerUndiciInstrumentation } from \"../opentelemetry/azure-undici-instrumentation\";\nimport { initFromEnv } from \"./start-from-env\";\n\n/**\n * Initialize the Azure Monitor with the given instrumentations.\n * Call this function to register the instrumentations with the Azure Monitor.\n * By default, the Undici instrumentation is included; if you need to add more,\n * you can pass them as an array of instrumentations.\n *\n * @remarks\n * This function will register the given instrumentations with the Azure Monitor.\n * It is recommended to call this function at the beginning of your application.\n *\n * @example\n * import { initAzureMonitor } from \"@pagopa/azure-tracing/azure-monitor\";\n *\n * initAzureMonitor();\n * @param instrumentations the list of instrumentations to register with the Azure Monitor.\n * @param azureMonitorOptions custom configuration for Azure Monitor. If not provided, then it will be initialized using the environment variables.\n */\nexport const initAzureMonitor = (\n instrumentations: readonly Instrumentation[] = [],\n azureMonitorOptions?: AzureMonitorOpenTelemetryOptions,\n) => {\n if (azureMonitorOptions) {\n useAzureMonitor(azureMonitorOptions);\n } else {\n initFromEnv();\n }\n\n registerInstrumentations({\n instrumentations: [registerUndiciInstrumentation(), ...instrumentations],\n meterProvider: metrics.getMeterProvider(),\n tracerProvider: trace.getTracerProvider(),\n });\n};\n","import { UndiciInstrumentation } from \"@opentelemetry/instrumentation-undici\";\n\n// instrument native node fetch\nexport const registerUndiciInstrumentation = () =>\n new UndiciInstrumentation({\n requestHook: (span, requestInfo) => {\n const { method, origin, path } = requestInfo;\n // Default instrumented attributes don't feed well into AppInsights,\n // so we set them manually.\n span.setAttributes({\n \"http.host\": origin,\n \"http.method\": method,\n \"http.target\": path,\n \"http.url\": `${origin}${path}`,\n });\n },\n responseHook: (span, { response }) => {\n // Same as above, set the status code manually.\n span.setAttribute(\"http.status_code\", response.statusCode);\n },\n });\n","import { useAzureMonitor } from \"@azure/monitor-opentelemetry\";\n\nimport { loadEnv } from \"./env\";\n\nexport const initFromEnv = () => {\n const env = loadEnv();\n\n return useAzureMonitor({\n azureMonitorExporterOptions: {\n connectionString: env.APPINSIGHTS_CONNECTION_STRING,\n },\n enableLiveMetrics: true,\n samplingRatio: env.APPINSIGHTS_SAMPLING_PERCENTAGE,\n });\n};\n","// Load and type check environment variables on runtime\nimport { createEnv } from \"@t3-oss/env-core\";\nimport { z } from \"zod\";\n\nexport const loadEnv = () =>\n createEnv({\n emptyStringAsUndefined: true,\n onValidationError: (errors) => {\n throw new Error(\n errors\n .map(\n (error) => `Environment variable ${error.path} - ${error.message}`,\n )\n .join(\", \"),\n );\n },\n runtimeEnv: process.env,\n server: {\n APPINSIGHTS_CONNECTION_STRING: z\n .string()\n .describe(\"The connection string for Application Insights.\"),\n APPINSIGHTS_SAMPLING_PERCENTAGE: z\n .optional(\n z.coerce\n .number()\n .min(0)\n .max(100)\n .default(5)\n .describe(\n \"Application Insights sampling percentage between 0 and 100. If not set, defaults to 5.\",\n ),\n )\n .transform((value) => {\n const percentage = Number(value);\n return isNaN(percentage) ? 5 : percentage;\n })\n .transform((value) => value / 100),\n },\n });\n"],"mappings":";AAAA;AAAA,EAEE,mBAAAA;AAAA,OACK;AACP,SAAS,SAAS,aAAa;AAC/B;AAAA,EAEE;AAAA,OACK;;;ACRP,SAAS,6BAA6B;AAG/B,IAAM,gCAAgC,MAC3C,IAAI,sBAAsB;AAAA,EACxB,aAAa,CAAC,MAAM,gBAAgB;AAClC,UAAM,EAAE,QAAQ,QAAQ,KAAK,IAAI;AAGjC,SAAK,cAAc;AAAA,MACjB,aAAa;AAAA,MACb,eAAe;AAAA,MACf,eAAe;AAAA,MACf,YAAY,GAAG,MAAM,GAAG,IAAI;AAAA,IAC9B,CAAC;AAAA,EACH;AAAA,EACA,cAAc,CAAC,MAAM,EAAE,SAAS,MAAM;AAEpC,SAAK,aAAa,oBAAoB,SAAS,UAAU;AAAA,EAC3D;AACF,CAAC;;;ACpBH,SAAS,uBAAuB;;;ACChC,SAAS,iBAAiB;AAC1B,SAAS,SAAS;AAEX,IAAM,UAAU,MACrB,UAAU;AAAA,EACR,wBAAwB;AAAA,EACxB,mBAAmB,CAAC,WAAW;AAC7B,UAAM,IAAI;AAAA,MACR,OACG;AAAA,QACC,CAAC,UAAU,wBAAwB,MAAM,IAAI,MAAM,MAAM,OAAO;AAAA,MAClE,EACC,KAAK,IAAI;AAAA,IACd;AAAA,EACF;AAAA,EACA,YAAY,QAAQ;AAAA,EACpB,QAAQ;AAAA,IACN,+BAA+B,EAC5B,OAAO,EACP,SAAS,iDAAiD;AAAA,IAC7D,iCAAiC,EAC9B;AAAA,MACC,EAAE,OACC,OAAO,EACP,IAAI,CAAC,EACL,IAAI,GAAG,EACP,QAAQ,CAAC,EACT;AAAA,QACC;AAAA,MACF;AAAA,IACJ,EACC,UAAU,CAAC,UAAU;AACpB,YAAM,aAAa,OAAO,KAAK;AAC/B,aAAO,MAAM,UAAU,IAAI,IAAI;AAAA,IACjC,CAAC,EACA,UAAU,CAAC,UAAU,QAAQ,GAAG;AAAA,EACrC;AACF,CAAC;;;ADlCI,IAAM,cAAc,MAAM;AAC/B,QAAM,MAAM,QAAQ;AAEpB,SAAO,gBAAgB;AAAA,IACrB,6BAA6B;AAAA,MAC3B,kBAAkB,IAAI;AAAA,IACxB;AAAA,IACA,mBAAmB;AAAA,IACnB,eAAe,IAAI;AAAA,EACrB,CAAC;AACH;;;AFgBO,IAAM,mBAAmB,CAC9B,mBAA+C,CAAC,GAChD,wBACG;AACH,MAAI,qBAAqB;AACvB,IAAAC,iBAAgB,mBAAmB;AAAA,EACrC,OAAO;AACL,gBAAY;AAAA,EACd;AAEA,2BAAyB;AAAA,IACvB,kBAAkB,CAAC,8BAA8B,GAAG,GAAG,gBAAgB;AAAA,IACvE,eAAe,QAAQ,iBAAiB;AAAA,IACxC,gBAAgB,MAAM,kBAAkB;AAAA,EAC1C,CAAC;AACH;","names":["useAzureMonitor","useAzureMonitor"]}
1
+ {"version":3,"sources":["../../src/azure/monitor/index.ts","../../src/azure/opentelemetry/azure-undici-instrumentation.ts","../../src/azure/monitor/start-from-env.ts","../../src/azure/monitor/env.ts"],"sourcesContent":["import {\n AzureMonitorOpenTelemetryOptions,\n useAzureMonitor,\n} from \"@azure/monitor-opentelemetry\";\nimport { metrics, trace } from \"@opentelemetry/api\";\nimport {\n Instrumentation,\n registerInstrumentations,\n} from \"@opentelemetry/instrumentation\";\n\nimport { registerUndiciInstrumentation } from \"../opentelemetry/azure-undici-instrumentation\";\nimport { initFromEnv } from \"./start-from-env\";\n\n/**\n * Initialize the Azure Monitor with the given instrumentations and options.\n * This function sets up telemetry collection for your application by registering\n * the provided instrumentations and configuring Azure Monitor OpenTelemetry.\n *\n * By default, the Undici instrumentation is included. You can extend the functionality\n * by passing additional instrumentations as an array.\n *\n * @remarks\n * - This function should be called at the start of your application to ensure\n * telemetry is collected from the beginning.\n * - If `azureMonitorOptions` is not provided, the configuration will be initialized\n * using environment variables. @see README for more details.\n *\n * @example\n * // Basic usage with default settings\n * import { initAzureMonitor } from \"@pagopa/azure-tracing/azure-monitor\";\n * initAzureMonitor();\n *\n * @example\n * // Usage with custom instrumentations\n * import { initAzureMonitor } from \"@pagopa/azure-tracing/azure-monitor\";\n * import { MyCustomInstrumentation } from \"my-custom-instrumentation\";\n *\n * initAzureMonitor([new MyCustomInstrumentation()]);\n *\n * @example\n * // Usage with custom Azure Monitor options\n * import { initAzureMonitor } from \"@pagopa/azure-tracing/azure-monitor\";\n *\n * initAzureMonitor([], {\n * azureMonitorExporterOptions: {\n * connectionString: \"theConnectionString\",\n * },\n * // other options...\n * });\n *\n * @param instrumentations - The list of instrumentations to register with the Azure Monitor.\n * @param azureMonitorOptions - Custom configuration for Azure Monitor. If not provided,\n * it will be initialized using environment variables.\n */\nexport const initAzureMonitor = (\n instrumentations: readonly Instrumentation[] = [],\n azureMonitorOptions?: AzureMonitorOpenTelemetryOptions,\n) => {\n if (azureMonitorOptions) {\n useAzureMonitor(azureMonitorOptions);\n } else {\n initFromEnv();\n }\n\n registerInstrumentations({\n instrumentations: [registerUndiciInstrumentation(), ...instrumentations],\n meterProvider: metrics.getMeterProvider(),\n tracerProvider: trace.getTracerProvider(),\n });\n};\n","import { UndiciInstrumentation } from \"@opentelemetry/instrumentation-undici\";\n\n// instrument native node fetch\nexport const registerUndiciInstrumentation = () =>\n new UndiciInstrumentation({\n requestHook: (span, requestInfo) => {\n const { method, origin, path } = requestInfo;\n // Default instrumented attributes don't feed well into AppInsights,\n // so we set them manually.\n span.setAttributes({\n \"http.host\": origin,\n \"http.method\": method,\n \"http.target\": path,\n \"http.url\": `${origin}${path}`,\n });\n },\n responseHook: (span, { response }) => {\n // Same as above, set the status code manually.\n span.setAttribute(\"http.status_code\", response.statusCode);\n },\n });\n","import { useAzureMonitor } from \"@azure/monitor-opentelemetry\";\n\nimport { loadEnv } from \"./env\";\n\nexport const initFromEnv = () => {\n const env = loadEnv();\n\n return useAzureMonitor({\n azureMonitorExporterOptions: {\n connectionString: env.APPLICATIONINSIGHTS_CONNECTION_STRING,\n },\n enableLiveMetrics: true,\n samplingRatio: env.APPINSIGHTS_SAMPLING_PERCENTAGE,\n });\n};\n","// Load and type check environment variables on runtime\nimport { createEnv } from \"@t3-oss/env-core\";\nimport { z } from \"zod\";\n\nexport const loadEnv = () =>\n createEnv({\n emptyStringAsUndefined: true,\n onValidationError: (errors) => {\n throw new Error(\n errors\n .map(\n (error) => `Environment variable ${error.path} - ${error.message}`,\n )\n .join(\", \"),\n );\n },\n runtimeEnv: process.env,\n server: {\n APPINSIGHTS_SAMPLING_PERCENTAGE: z\n .optional(\n z.coerce\n .number()\n .min(0)\n .max(100)\n .default(5)\n .describe(\n \"Application Insights sampling percentage between 0 and 100. If not set, defaults to 5.\",\n ),\n )\n .transform((value) => {\n const percentage = Number(value);\n return isNaN(percentage) ? 5 : percentage;\n })\n .transform((value) => value / 100),\n APPLICATIONINSIGHTS_CONNECTION_STRING: z\n .string()\n .describe(\"The connection string for Application Insights.\"),\n },\n });\n"],"mappings":";AAAA;AAAA,EAEE,mBAAAA;AAAA,OACK;AACP,SAAS,SAAS,aAAa;AAC/B;AAAA,EAEE;AAAA,OACK;;;ACRP,SAAS,6BAA6B;AAG/B,IAAM,gCAAgC,MAC3C,IAAI,sBAAsB;AAAA,EACxB,aAAa,CAAC,MAAM,gBAAgB;AAClC,UAAM,EAAE,QAAQ,QAAQ,KAAK,IAAI;AAGjC,SAAK,cAAc;AAAA,MACjB,aAAa;AAAA,MACb,eAAe;AAAA,MACf,eAAe;AAAA,MACf,YAAY,GAAG,MAAM,GAAG,IAAI;AAAA,IAC9B,CAAC;AAAA,EACH;AAAA,EACA,cAAc,CAAC,MAAM,EAAE,SAAS,MAAM;AAEpC,SAAK,aAAa,oBAAoB,SAAS,UAAU;AAAA,EAC3D;AACF,CAAC;;;ACpBH,SAAS,uBAAuB;;;ACChC,SAAS,iBAAiB;AAC1B,SAAS,SAAS;AAEX,IAAM,UAAU,MACrB,UAAU;AAAA,EACR,wBAAwB;AAAA,EACxB,mBAAmB,CAAC,WAAW;AAC7B,UAAM,IAAI;AAAA,MACR,OACG;AAAA,QACC,CAAC,UAAU,wBAAwB,MAAM,IAAI,MAAM,MAAM,OAAO;AAAA,MAClE,EACC,KAAK,IAAI;AAAA,IACd;AAAA,EACF;AAAA,EACA,YAAY,QAAQ;AAAA,EACpB,QAAQ;AAAA,IACN,iCAAiC,EAC9B;AAAA,MACC,EAAE,OACC,OAAO,EACP,IAAI,CAAC,EACL,IAAI,GAAG,EACP,QAAQ,CAAC,EACT;AAAA,QACC;AAAA,MACF;AAAA,IACJ,EACC,UAAU,CAAC,UAAU;AACpB,YAAM,aAAa,OAAO,KAAK;AAC/B,aAAO,MAAM,UAAU,IAAI,IAAI;AAAA,IACjC,CAAC,EACA,UAAU,CAAC,UAAU,QAAQ,GAAG;AAAA,IACnC,uCAAuC,EACpC,OAAO,EACP,SAAS,iDAAiD;AAAA,EAC/D;AACF,CAAC;;;ADlCI,IAAM,cAAc,MAAM;AAC/B,QAAM,MAAM,QAAQ;AAEpB,SAAO,gBAAgB;AAAA,IACrB,6BAA6B;AAAA,MAC3B,kBAAkB,IAAI;AAAA,IACxB;AAAA,IACA,mBAAmB;AAAA,IACnB,eAAe,IAAI;AAAA,EACrB,CAAC;AACH;;;AFwCO,IAAM,mBAAmB,CAC9B,mBAA+C,CAAC,GAChD,wBACG;AACH,MAAI,qBAAqB;AACvB,IAAAC,iBAAgB,mBAAmB;AAAA,EACrC,OAAO;AACL,gBAAY;AAAA,EACd;AAEA,2BAAyB;AAAA,IACvB,kBAAkB,CAAC,8BAA8B,GAAG,GAAG,gBAAgB;AAAA,IACvE,eAAe,QAAQ,iBAAiB;AAAA,IACxC,gBAAgB,MAAM,kBAAkB;AAAA,EAC1C,CAAC;AACH;","names":["useAzureMonitor","useAzureMonitor"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pagopa/azure-tracing",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "type": "module",
5
5
  "description": "A package that contains some utilities to enable Azure tracing on Node.js applications.",
6
6
  "keywords": [