@pagopa/azure-tracing 0.4.18 → 0.5.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 +28 -4
- package/dist/functions/index.cjs +15 -4
- package/dist/functions/index.cjs.map +1 -1
- package/dist/functions/index.js +18 -5
- package/dist/functions/index.js.map +1 -1
- package/dist/monitor/index.cjs +15 -4
- package/dist/monitor/index.cjs.map +1 -1
- package/dist/monitor/index.js +18 -5
- package/dist/monitor/index.js.map +1 -1
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -43,10 +43,16 @@ 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**
|
|
47
|
-
|
|
|
48
|
-
| **APPINSIGHTS_SAMPLING_PERCENTAGE**
|
|
49
|
-
| **APPLICATIONINSIGHTS_CONNECTION_STRING**
|
|
46
|
+
| **Name** | **Required** | **Default** |
|
|
47
|
+
| --------------------------------------------- | ------------ | ----------- |
|
|
48
|
+
| **APPINSIGHTS_SAMPLING_PERCENTAGE** | false | 5 |
|
|
49
|
+
| **APPLICATIONINSIGHTS_CONNECTION_STRING** | true | - |
|
|
50
|
+
| **APPLICATIONINSIGHTS_ENTRA_ID_AUTH_ENABLED** | false | false |
|
|
51
|
+
|
|
52
|
+
> [!WARNING]
|
|
53
|
+
> Connection-string-only authentication (the current default) is **deprecated** and will be removed in the next major version.
|
|
54
|
+
> Set `APPLICATIONINSIGHTS_ENTRA_ID_AUTH_ENABLED=true` to adopt Microsoft Entra ID authentication now.
|
|
55
|
+
> See [Microsoft Entra ID Authentication](#microsoft-entra-id-authentication) for details.
|
|
50
56
|
|
|
51
57
|
#### Step 2: Register Azure Function Lifecycle Hooks
|
|
52
58
|
|
|
@@ -119,6 +125,24 @@ emitCustomEvent("taskCreated", { id: task.id })("CreateTaskHandler");
|
|
|
119
125
|
|
|
120
126
|
This is especially useful for tracing domain-specific actions (e.g., resource creation, user actions, error tracking).
|
|
121
127
|
|
|
128
|
+
## Microsoft Entra ID Authentication
|
|
129
|
+
|
|
130
|
+
> [!IMPORTANT]
|
|
131
|
+
> Microsoft Entra ID authentication is the **recommended** and more secure way to connect to Application Insights.
|
|
132
|
+
> Connection-string-only authentication is deprecated and will be removed in the **next major release**.
|
|
133
|
+
|
|
134
|
+
Set `APPLICATIONINSIGHTS_ENTRA_ID_AUTH_ENABLED=true` to enable [Microsoft Entra ID authentication](https://learn.microsoft.com/en-us/azure/azure-monitor/app/azure-ad-authentication).
|
|
135
|
+
The package uses [`DefaultAzureCredential`](https://learn.microsoft.com/en-us/azure/developer/javascript/sdk/authentication/credential-chains#use-defaultazurecredential-for-flexibility) from `@azure/identity`, which automatically resolves the right credential for the environment:
|
|
136
|
+
|
|
137
|
+
- **Azure (production)**: uses the Managed Identity assigned to the resource (App Service, Azure Function, Container App, etc.)
|
|
138
|
+
- **Local development**: falls back to Azure CLI, Azure Developer CLI, or service principal env vars
|
|
139
|
+
|
|
140
|
+
The connection string (`APPLICATIONINSIGHTS_CONNECTION_STRING`) is still required in both modes — it identifies the Application Insights resource.
|
|
141
|
+
|
|
142
|
+
### Required Azure RBAC role
|
|
143
|
+
|
|
144
|
+
The managed identity (or other principal) must have the **Monitoring Metrics Publisher** role on the Application Insights resource.
|
|
145
|
+
|
|
122
146
|
## Dependency Constraints
|
|
123
147
|
|
|
124
148
|
### `import-in-the-middle` version must match `@azure/monitor-opentelemetry`
|
package/dist/functions/index.cjs
CHANGED
|
@@ -7,6 +7,7 @@ var import_api = require("@opentelemetry/api");
|
|
|
7
7
|
var import_instrumentation = require("@opentelemetry/instrumentation");
|
|
8
8
|
|
|
9
9
|
// src/azure/monitor/start-from-env.ts
|
|
10
|
+
var import_identity = require("@azure/identity");
|
|
10
11
|
var import_monitor_opentelemetry = require("@azure/monitor-opentelemetry");
|
|
11
12
|
|
|
12
13
|
// src/azure/monitor/env.ts
|
|
@@ -31,23 +32,33 @@ var loadEnv = () => (0, import_env_core.createEnv)({
|
|
|
31
32
|
const percentage = Number(value);
|
|
32
33
|
return isNaN(percentage) ? 5 : percentage;
|
|
33
34
|
}).transform((value) => value / 100),
|
|
34
|
-
APPLICATIONINSIGHTS_CONNECTION_STRING: import_zod.z.string().describe("The connection string for Application Insights.")
|
|
35
|
+
APPLICATIONINSIGHTS_CONNECTION_STRING: import_zod.z.string().describe("The connection string for Application Insights."),
|
|
36
|
+
APPLICATIONINSIGHTS_ENTRA_ID_AUTH_ENABLED: import_zod.z.enum(["true", "false"]).optional().transform((value) => value === "true").describe(
|
|
37
|
+
"When set to 'true', enables Microsoft Entra ID (Managed Identity) authentication for Application Insights. Defaults to false (connection-string-only auth). This will become the default in the next major release."
|
|
38
|
+
)
|
|
35
39
|
}
|
|
36
40
|
});
|
|
37
41
|
|
|
38
42
|
// src/azure/monitor/start-from-env.ts
|
|
39
43
|
var initFromEnv = () => {
|
|
40
44
|
const env = loadEnv();
|
|
41
|
-
|
|
45
|
+
const azureMonitorOptions = {
|
|
42
46
|
azureMonitorExporterOptions: {
|
|
43
|
-
connectionString: env.APPLICATIONINSIGHTS_CONNECTION_STRING
|
|
47
|
+
connectionString: env.APPLICATIONINSIGHTS_CONNECTION_STRING,
|
|
48
|
+
...env.APPLICATIONINSIGHTS_ENTRA_ID_AUTH_ENABLED ? {
|
|
49
|
+
// Entra ID (Managed Identity) authentication via DefaultAzureCredential.
|
|
50
|
+
// DefaultAzureCredential tries multiple credential providers in order:
|
|
51
|
+
// Managed Identity in production, Azure CLI / service principal for local dev.
|
|
52
|
+
credential: new import_identity.DefaultAzureCredential()
|
|
53
|
+
} : {}
|
|
44
54
|
},
|
|
45
55
|
enableLiveMetrics: true,
|
|
46
56
|
samplingRatio: env.APPINSIGHTS_SAMPLING_PERCENTAGE,
|
|
47
57
|
// Disable rate limiter introduced in @azure/monitor-opentelemetry@1.16.0
|
|
48
58
|
// (default: 5 req/s). A value of 0 means no limit.
|
|
49
59
|
tracesPerSecond: 0
|
|
50
|
-
}
|
|
60
|
+
};
|
|
61
|
+
return (0, import_monitor_opentelemetry.useAzureMonitor)(azureMonitorOptions);
|
|
51
62
|
};
|
|
52
63
|
|
|
53
64
|
// src/azure/opentelemetry/azure-undici-instrumentation.ts
|
|
@@ -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\n(async () => {\n await waitForAllMessagesAcknowledged();\n})();\n","import {
|
|
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\n(async () => {\n await waitForAllMessagesAcknowledged();\n})();\n","import { DefaultAzureCredential } from \"@azure/identity\";\nimport {\n type AzureMonitorOpenTelemetryOptions,\n useAzureMonitor,\n} from \"@azure/monitor-opentelemetry\";\n\nimport { loadEnv } from \"./env.js\";\n\nexport const initFromEnv = () => {\n const env = loadEnv();\n\n const azureMonitorOptions: AzureMonitorOpenTelemetryOptions = {\n azureMonitorExporterOptions: {\n connectionString: env.APPLICATIONINSIGHTS_CONNECTION_STRING,\n ...(env.APPLICATIONINSIGHTS_ENTRA_ID_AUTH_ENABLED\n ? {\n // Entra ID (Managed Identity) authentication via DefaultAzureCredential.\n // DefaultAzureCredential tries multiple credential providers in order:\n // Managed Identity in production, Azure CLI / service principal for local dev.\n credential: new DefaultAzureCredential(),\n }\n : {}),\n },\n enableLiveMetrics: true,\n samplingRatio: env.APPINSIGHTS_SAMPLING_PERCENTAGE,\n // Disable rate limiter introduced in @azure/monitor-opentelemetry@1.16.0\n // (default: 5 req/s). A value of 0 means no limit.\n tracesPerSecond: 0,\n };\n\n return useAzureMonitor(azureMonitorOptions);\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 APPLICATIONINSIGHTS_ENTRA_ID_AUTH_ENABLED: z\n .enum([\"true\", \"false\"])\n .optional()\n .transform((value) => value === \"true\")\n .describe(\n \"When set to 'true', enables Microsoft Entra ID (Managed Identity) authentication for Application Insights. \" +\n \"Defaults to false (connection-string-only auth). \" +\n \"This will become the default in the next major release.\",\n ),\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,kCAA4C;AAC5C,oBAAyB;AAOzB,iBAA+B;AAC/B,6BAAyC;;;ACfzC,sBAAuC;AACvC,mCAGO;;;ACHP,sBAA0B;AAC1B,iBAAkB;AAEX,IAAM,UAAU,UACrB,2BAAU;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,aAC9B;AAAA,MACC,aAAE,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,aACpC,OAAO,EACP,SAAS,iDAAiD;AAAA,IAC7D,2CAA2C,aACxC,KAAK,CAAC,QAAQ,OAAO,CAAC,EACtB,SAAS,EACT,UAAU,CAAC,UAAU,UAAU,MAAM,EACrC;AAAA,MACC;AAAA,IAGF;AAAA,EACJ;AACF,CAAC;;;ADvCI,IAAM,cAAc,MAAM;AAC/B,QAAM,MAAM,QAAQ;AAEpB,QAAM,sBAAwD;AAAA,IAC5D,6BAA6B;AAAA,MAC3B,kBAAkB,IAAI;AAAA,MACtB,GAAI,IAAI,4CACJ;AAAA;AAAA;AAAA;AAAA,QAIE,YAAY,IAAI,uCAAuB;AAAA,MACzC,IACA,CAAC;AAAA,IACP;AAAA,IACA,mBAAmB;AAAA,IACnB,eAAe,IAAI;AAAA;AAAA;AAAA,IAGnB,iBAAiB;AAAA,EACnB;AAEA,aAAO,8CAAgB,mBAAmB;AAC5C;;;AE/BA,oCAAsC;AAG/B,IAAM,gCAAgC,MAC3C,IAAI,oDAAsB;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;;;AHpBH;AASA,IAAM,EAAE,iBAAiB,+BAA+B,QACtD,yDAA4B;AAAA,IAE9B,wBAAS,iCAAiC,YAAY,KAAK,eAAe;AAQ1E,YAAY;AAAA,IAEZ,iDAAyB;AAAA,EACvB,kBAAkB,CAAC,8BAA8B,CAAC;AAAA,EAClD,eAAe,mBAAQ,iBAAiB;AAAA,EACxC,gBAAgB,iBAAM,kBAAkB;AAC1C,CAAC;AAAA,CAEA,YAAY;AACX,QAAM,+BAA+B;AACvC,GAAG;","names":[]}
|
package/dist/functions/index.js
CHANGED
|
@@ -5,7 +5,10 @@ import { metrics, trace } from "@opentelemetry/api";
|
|
|
5
5
|
import { registerInstrumentations } from "@opentelemetry/instrumentation";
|
|
6
6
|
|
|
7
7
|
// src/azure/monitor/start-from-env.ts
|
|
8
|
-
import {
|
|
8
|
+
import { DefaultAzureCredential } from "@azure/identity";
|
|
9
|
+
import {
|
|
10
|
+
useAzureMonitor
|
|
11
|
+
} from "@azure/monitor-opentelemetry";
|
|
9
12
|
|
|
10
13
|
// src/azure/monitor/env.ts
|
|
11
14
|
import { createEnv } from "@t3-oss/env-core";
|
|
@@ -29,23 +32,33 @@ var loadEnv = () => createEnv({
|
|
|
29
32
|
const percentage = Number(value);
|
|
30
33
|
return isNaN(percentage) ? 5 : percentage;
|
|
31
34
|
}).transform((value) => value / 100),
|
|
32
|
-
APPLICATIONINSIGHTS_CONNECTION_STRING: z.string().describe("The connection string for Application Insights.")
|
|
35
|
+
APPLICATIONINSIGHTS_CONNECTION_STRING: z.string().describe("The connection string for Application Insights."),
|
|
36
|
+
APPLICATIONINSIGHTS_ENTRA_ID_AUTH_ENABLED: z.enum(["true", "false"]).optional().transform((value) => value === "true").describe(
|
|
37
|
+
"When set to 'true', enables Microsoft Entra ID (Managed Identity) authentication for Application Insights. Defaults to false (connection-string-only auth). This will become the default in the next major release."
|
|
38
|
+
)
|
|
33
39
|
}
|
|
34
40
|
});
|
|
35
41
|
|
|
36
42
|
// src/azure/monitor/start-from-env.ts
|
|
37
43
|
var initFromEnv = () => {
|
|
38
44
|
const env = loadEnv();
|
|
39
|
-
|
|
45
|
+
const azureMonitorOptions = {
|
|
40
46
|
azureMonitorExporterOptions: {
|
|
41
|
-
connectionString: env.APPLICATIONINSIGHTS_CONNECTION_STRING
|
|
47
|
+
connectionString: env.APPLICATIONINSIGHTS_CONNECTION_STRING,
|
|
48
|
+
...env.APPLICATIONINSIGHTS_ENTRA_ID_AUTH_ENABLED ? {
|
|
49
|
+
// Entra ID (Managed Identity) authentication via DefaultAzureCredential.
|
|
50
|
+
// DefaultAzureCredential tries multiple credential providers in order:
|
|
51
|
+
// Managed Identity in production, Azure CLI / service principal for local dev.
|
|
52
|
+
credential: new DefaultAzureCredential()
|
|
53
|
+
} : {}
|
|
42
54
|
},
|
|
43
55
|
enableLiveMetrics: true,
|
|
44
56
|
samplingRatio: env.APPINSIGHTS_SAMPLING_PERCENTAGE,
|
|
45
57
|
// Disable rate limiter introduced in @azure/monitor-opentelemetry@1.16.0
|
|
46
58
|
// (default: 5 req/s). A value of 0 means no limit.
|
|
47
59
|
tracesPerSecond: 0
|
|
48
|
-
}
|
|
60
|
+
};
|
|
61
|
+
return useAzureMonitor(azureMonitorOptions);
|
|
49
62
|
};
|
|
50
63
|
|
|
51
64
|
// src/azure/opentelemetry/azure-undici-instrumentation.ts
|
|
@@ -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\n(async () => {\n await waitForAllMessagesAcknowledged();\n})();\n","import {
|
|
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\n(async () => {\n await waitForAllMessagesAcknowledged();\n})();\n","import { DefaultAzureCredential } from \"@azure/identity\";\nimport {\n type AzureMonitorOpenTelemetryOptions,\n useAzureMonitor,\n} from \"@azure/monitor-opentelemetry\";\n\nimport { loadEnv } from \"./env.js\";\n\nexport const initFromEnv = () => {\n const env = loadEnv();\n\n const azureMonitorOptions: AzureMonitorOpenTelemetryOptions = {\n azureMonitorExporterOptions: {\n connectionString: env.APPLICATIONINSIGHTS_CONNECTION_STRING,\n ...(env.APPLICATIONINSIGHTS_ENTRA_ID_AUTH_ENABLED\n ? {\n // Entra ID (Managed Identity) authentication via DefaultAzureCredential.\n // DefaultAzureCredential tries multiple credential providers in order:\n // Managed Identity in production, Azure CLI / service principal for local dev.\n credential: new DefaultAzureCredential(),\n }\n : {}),\n },\n enableLiveMetrics: true,\n samplingRatio: env.APPINSIGHTS_SAMPLING_PERCENTAGE,\n // Disable rate limiter introduced in @azure/monitor-opentelemetry@1.16.0\n // (default: 5 req/s). A value of 0 means no limit.\n tracesPerSecond: 0,\n };\n\n return useAzureMonitor(azureMonitorOptions);\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 APPLICATIONINSIGHTS_ENTRA_ID_AUTH_ENABLED: z\n .enum([\"true\", \"false\"])\n .optional()\n .transform((value) => value === \"true\")\n .describe(\n \"When set to 'true', enables Microsoft Entra ID (Managed Identity) authentication for Application Insights. \" +\n \"Defaults to false (connection-string-only auth). \" +\n \"This will become the default in the next major release.\",\n ),\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,8BAA8B;AACvC;AAAA,EAEE;AAAA,OACK;;;ACHP,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,IAC7D,2CAA2C,EACxC,KAAK,CAAC,QAAQ,OAAO,CAAC,EACtB,SAAS,EACT,UAAU,CAAC,UAAU,UAAU,MAAM,EACrC;AAAA,MACC;AAAA,IAGF;AAAA,EACJ;AACF,CAAC;;;ADvCI,IAAM,cAAc,MAAM;AAC/B,QAAM,MAAM,QAAQ;AAEpB,QAAM,sBAAwD;AAAA,IAC5D,6BAA6B;AAAA,MAC3B,kBAAkB,IAAI;AAAA,MACtB,GAAI,IAAI,4CACJ;AAAA;AAAA;AAAA;AAAA,QAIE,YAAY,IAAI,uBAAuB;AAAA,MACzC,IACA,CAAC;AAAA,IACP;AAAA,IACA,mBAAmB;AAAA,IACnB,eAAe,IAAI;AAAA;AAAA;AAAA,IAGnB,iBAAiB;AAAA,EACnB;AAEA,SAAO,gBAAgB,mBAAmB;AAC5C;;;AE/BA,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;AAAA,CAEA,YAAY;AACX,QAAM,+BAA+B;AACvC,GAAG;","names":[]}
|
package/dist/monitor/index.cjs
CHANGED
|
@@ -45,6 +45,7 @@ var registerUndiciInstrumentation = () => new import_instrumentation_undici.Undi
|
|
|
45
45
|
});
|
|
46
46
|
|
|
47
47
|
// src/azure/monitor/start-from-env.ts
|
|
48
|
+
var import_identity = require("@azure/identity");
|
|
48
49
|
var import_monitor_opentelemetry = require("@azure/monitor-opentelemetry");
|
|
49
50
|
|
|
50
51
|
// src/azure/monitor/env.ts
|
|
@@ -69,23 +70,33 @@ var loadEnv = () => (0, import_env_core.createEnv)({
|
|
|
69
70
|
const percentage = Number(value);
|
|
70
71
|
return isNaN(percentage) ? 5 : percentage;
|
|
71
72
|
}).transform((value) => value / 100),
|
|
72
|
-
APPLICATIONINSIGHTS_CONNECTION_STRING: import_zod.z.string().describe("The connection string for Application Insights.")
|
|
73
|
+
APPLICATIONINSIGHTS_CONNECTION_STRING: import_zod.z.string().describe("The connection string for Application Insights."),
|
|
74
|
+
APPLICATIONINSIGHTS_ENTRA_ID_AUTH_ENABLED: import_zod.z.enum(["true", "false"]).optional().transform((value) => value === "true").describe(
|
|
75
|
+
"When set to 'true', enables Microsoft Entra ID (Managed Identity) authentication for Application Insights. Defaults to false (connection-string-only auth). This will become the default in the next major release."
|
|
76
|
+
)
|
|
73
77
|
}
|
|
74
78
|
});
|
|
75
79
|
|
|
76
80
|
// src/azure/monitor/start-from-env.ts
|
|
77
81
|
var initFromEnv = () => {
|
|
78
82
|
const env = loadEnv();
|
|
79
|
-
|
|
83
|
+
const azureMonitorOptions = {
|
|
80
84
|
azureMonitorExporterOptions: {
|
|
81
|
-
connectionString: env.APPLICATIONINSIGHTS_CONNECTION_STRING
|
|
85
|
+
connectionString: env.APPLICATIONINSIGHTS_CONNECTION_STRING,
|
|
86
|
+
...env.APPLICATIONINSIGHTS_ENTRA_ID_AUTH_ENABLED ? {
|
|
87
|
+
// Entra ID (Managed Identity) authentication via DefaultAzureCredential.
|
|
88
|
+
// DefaultAzureCredential tries multiple credential providers in order:
|
|
89
|
+
// Managed Identity in production, Azure CLI / service principal for local dev.
|
|
90
|
+
credential: new import_identity.DefaultAzureCredential()
|
|
91
|
+
} : {}
|
|
82
92
|
},
|
|
83
93
|
enableLiveMetrics: true,
|
|
84
94
|
samplingRatio: env.APPINSIGHTS_SAMPLING_PERCENTAGE,
|
|
85
95
|
// Disable rate limiter introduced in @azure/monitor-opentelemetry@1.16.0
|
|
86
96
|
// (default: 5 req/s). A value of 0 means no limit.
|
|
87
97
|
tracesPerSecond: 0
|
|
88
|
-
}
|
|
98
|
+
};
|
|
99
|
+
return (0, import_monitor_opentelemetry.useAzureMonitor)(azureMonitorOptions);
|
|
89
100
|
};
|
|
90
101
|
|
|
91
102
|
// src/azure/monitor/index.ts
|
|
@@ -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 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 {
|
|
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 { DefaultAzureCredential } from \"@azure/identity\";\nimport {\n type AzureMonitorOpenTelemetryOptions,\n useAzureMonitor,\n} from \"@azure/monitor-opentelemetry\";\n\nimport { loadEnv } from \"./env.js\";\n\nexport const initFromEnv = () => {\n const env = loadEnv();\n\n const azureMonitorOptions: AzureMonitorOpenTelemetryOptions = {\n azureMonitorExporterOptions: {\n connectionString: env.APPLICATIONINSIGHTS_CONNECTION_STRING,\n ...(env.APPLICATIONINSIGHTS_ENTRA_ID_AUTH_ENABLED\n ? {\n // Entra ID (Managed Identity) authentication via DefaultAzureCredential.\n // DefaultAzureCredential tries multiple credential providers in order:\n // Managed Identity in production, Azure CLI / service principal for local dev.\n credential: new DefaultAzureCredential(),\n }\n : {}),\n },\n enableLiveMetrics: true,\n samplingRatio: env.APPINSIGHTS_SAMPLING_PERCENTAGE,\n // Disable rate limiter introduced in @azure/monitor-opentelemetry@1.16.0\n // (default: 5 req/s). A value of 0 means no limit.\n tracesPerSecond: 0,\n };\n\n return useAzureMonitor(azureMonitorOptions);\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 APPLICATIONINSIGHTS_ENTRA_ID_AUTH_ENABLED: z\n .enum([\"true\", \"false\"])\n .optional()\n .transform((value) => value === \"true\")\n .describe(\n \"When set to 'true', enables Microsoft Entra ID (Managed Identity) authentication for Application Insights. \" +\n \"Defaults to false (connection-string-only auth). \" +\n \"This will become the default in the next major release.\",\n ),\n },\n });\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAAAA,gCAGO;AACP,iBAA+B;AAC/B,6BAGO;;;ACRP,oCAAsC;AAG/B,IAAM,gCAAgC,MAC3C,IAAI,oDAAsB;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,sBAAuC;AACvC,mCAGO;;;ACHP,sBAA0B;AAC1B,iBAAkB;AAEX,IAAM,UAAU,UACrB,2BAAU;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,aAC9B;AAAA,MACC,aAAE,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,aACpC,OAAO,EACP,SAAS,iDAAiD;AAAA,IAC7D,2CAA2C,aACxC,KAAK,CAAC,QAAQ,OAAO,CAAC,EACtB,SAAS,EACT,UAAU,CAAC,UAAU,UAAU,MAAM,EACrC;AAAA,MACC;AAAA,IAGF;AAAA,EACJ;AACF,CAAC;;;ADvCI,IAAM,cAAc,MAAM;AAC/B,QAAM,MAAM,QAAQ;AAEpB,QAAM,sBAAwD;AAAA,IAC5D,6BAA6B;AAAA,MAC3B,kBAAkB,IAAI;AAAA,MACtB,GAAI,IAAI,4CACJ;AAAA;AAAA;AAAA;AAAA,QAIE,YAAY,IAAI,uCAAuB;AAAA,MACzC,IACA,CAAC;AAAA,IACP;AAAA,IACA,mBAAmB;AAAA,IACnB,eAAe,IAAI;AAAA;AAAA;AAAA,IAGnB,iBAAiB;AAAA,EACnB;AAEA,aAAO,8CAAgB,mBAAmB;AAC5C;;;AFuBO,IAAM,mBAAmB,CAC9B,mBAA+C,CAAC,GAChD,wBACG;AACH,MAAI,qBAAqB;AACvB,uDAAgB,mBAAmB;AAAA,EACrC,OAAO;AACL,gBAAY;AAAA,EACd;AAEA,uDAAyB;AAAA,IACvB,kBAAkB,CAAC,8BAA8B,GAAG,GAAG,gBAAgB;AAAA,IACvE,eAAe,mBAAQ,iBAAiB;AAAA,IACxC,gBAAgB,iBAAM,kBAAkB;AAAA,EAC1C,CAAC;AACH;","names":["import_monitor_opentelemetry"]}
|
package/dist/monitor/index.js
CHANGED
|
@@ -25,7 +25,10 @@ var registerUndiciInstrumentation = () => new UndiciInstrumentation({
|
|
|
25
25
|
});
|
|
26
26
|
|
|
27
27
|
// src/azure/monitor/start-from-env.ts
|
|
28
|
-
import {
|
|
28
|
+
import { DefaultAzureCredential } from "@azure/identity";
|
|
29
|
+
import {
|
|
30
|
+
useAzureMonitor
|
|
31
|
+
} from "@azure/monitor-opentelemetry";
|
|
29
32
|
|
|
30
33
|
// src/azure/monitor/env.ts
|
|
31
34
|
import { createEnv } from "@t3-oss/env-core";
|
|
@@ -49,23 +52,33 @@ var loadEnv = () => createEnv({
|
|
|
49
52
|
const percentage = Number(value);
|
|
50
53
|
return isNaN(percentage) ? 5 : percentage;
|
|
51
54
|
}).transform((value) => value / 100),
|
|
52
|
-
APPLICATIONINSIGHTS_CONNECTION_STRING: z.string().describe("The connection string for Application Insights.")
|
|
55
|
+
APPLICATIONINSIGHTS_CONNECTION_STRING: z.string().describe("The connection string for Application Insights."),
|
|
56
|
+
APPLICATIONINSIGHTS_ENTRA_ID_AUTH_ENABLED: z.enum(["true", "false"]).optional().transform((value) => value === "true").describe(
|
|
57
|
+
"When set to 'true', enables Microsoft Entra ID (Managed Identity) authentication for Application Insights. Defaults to false (connection-string-only auth). This will become the default in the next major release."
|
|
58
|
+
)
|
|
53
59
|
}
|
|
54
60
|
});
|
|
55
61
|
|
|
56
62
|
// src/azure/monitor/start-from-env.ts
|
|
57
63
|
var initFromEnv = () => {
|
|
58
64
|
const env = loadEnv();
|
|
59
|
-
|
|
65
|
+
const azureMonitorOptions = {
|
|
60
66
|
azureMonitorExporterOptions: {
|
|
61
|
-
connectionString: env.APPLICATIONINSIGHTS_CONNECTION_STRING
|
|
67
|
+
connectionString: env.APPLICATIONINSIGHTS_CONNECTION_STRING,
|
|
68
|
+
...env.APPLICATIONINSIGHTS_ENTRA_ID_AUTH_ENABLED ? {
|
|
69
|
+
// Entra ID (Managed Identity) authentication via DefaultAzureCredential.
|
|
70
|
+
// DefaultAzureCredential tries multiple credential providers in order:
|
|
71
|
+
// Managed Identity in production, Azure CLI / service principal for local dev.
|
|
72
|
+
credential: new DefaultAzureCredential()
|
|
73
|
+
} : {}
|
|
62
74
|
},
|
|
63
75
|
enableLiveMetrics: true,
|
|
64
76
|
samplingRatio: env.APPINSIGHTS_SAMPLING_PERCENTAGE,
|
|
65
77
|
// Disable rate limiter introduced in @azure/monitor-opentelemetry@1.16.0
|
|
66
78
|
// (default: 5 req/s). A value of 0 means no limit.
|
|
67
79
|
tracesPerSecond: 0
|
|
68
|
-
}
|
|
80
|
+
};
|
|
81
|
+
return useAzureMonitor(azureMonitorOptions);
|
|
69
82
|
};
|
|
70
83
|
|
|
71
84
|
// src/azure/monitor/index.ts
|
|
@@ -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 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 {
|
|
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 { DefaultAzureCredential } from \"@azure/identity\";\nimport {\n type AzureMonitorOpenTelemetryOptions,\n useAzureMonitor,\n} from \"@azure/monitor-opentelemetry\";\n\nimport { loadEnv } from \"./env.js\";\n\nexport const initFromEnv = () => {\n const env = loadEnv();\n\n const azureMonitorOptions: AzureMonitorOpenTelemetryOptions = {\n azureMonitorExporterOptions: {\n connectionString: env.APPLICATIONINSIGHTS_CONNECTION_STRING,\n ...(env.APPLICATIONINSIGHTS_ENTRA_ID_AUTH_ENABLED\n ? {\n // Entra ID (Managed Identity) authentication via DefaultAzureCredential.\n // DefaultAzureCredential tries multiple credential providers in order:\n // Managed Identity in production, Azure CLI / service principal for local dev.\n credential: new DefaultAzureCredential(),\n }\n : {}),\n },\n enableLiveMetrics: true,\n samplingRatio: env.APPINSIGHTS_SAMPLING_PERCENTAGE,\n // Disable rate limiter introduced in @azure/monitor-opentelemetry@1.16.0\n // (default: 5 req/s). A value of 0 means no limit.\n tracesPerSecond: 0,\n };\n\n return useAzureMonitor(azureMonitorOptions);\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 APPLICATIONINSIGHTS_ENTRA_ID_AUTH_ENABLED: z\n .enum([\"true\", \"false\"])\n .optional()\n .transform((value) => value === \"true\")\n .describe(\n \"When set to 'true', enables Microsoft Entra ID (Managed Identity) authentication for Application Insights. \" +\n \"Defaults to false (connection-string-only auth). \" +\n \"This will become the default in the next major release.\",\n ),\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,8BAA8B;AACvC;AAAA,EAEE;AAAA,OACK;;;ACHP,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,IAC7D,2CAA2C,EACxC,KAAK,CAAC,QAAQ,OAAO,CAAC,EACtB,SAAS,EACT,UAAU,CAAC,UAAU,UAAU,MAAM,EACrC;AAAA,MACC;AAAA,IAGF;AAAA,EACJ;AACF,CAAC;;;ADvCI,IAAM,cAAc,MAAM;AAC/B,QAAM,MAAM,QAAQ;AAEpB,QAAM,sBAAwD;AAAA,IAC5D,6BAA6B;AAAA,MAC3B,kBAAkB,IAAI;AAAA,MACtB,GAAI,IAAI,4CACJ;AAAA;AAAA;AAAA;AAAA,QAIE,YAAY,IAAI,uBAAuB;AAAA,MACzC,IACA,CAAC;AAAA,IACP;AAAA,IACA,mBAAmB;AAAA,IACnB,eAAe,IAAI;AAAA;AAAA;AAAA,IAGnB,iBAAiB;AAAA,EACnB;AAEA,SAAO,gBAAgB,mBAAmB;AAC5C;;;AFuBO,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
|
+
"version": "0.5.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "A package that contains some utilities to enable Azure tracing on Node.js applications.",
|
|
6
6
|
"repository": {
|
|
@@ -65,6 +65,7 @@
|
|
|
65
65
|
}
|
|
66
66
|
},
|
|
67
67
|
"dependencies": {
|
|
68
|
+
"@azure/identity": "^4.13.1",
|
|
68
69
|
"@azure/monitor-opentelemetry": "1.16.0",
|
|
69
70
|
"@azure/monitor-opentelemetry-exporter": "^1.0.0-beta.32",
|
|
70
71
|
"@opentelemetry/api": "^1.9.1",
|