Repository: microsoft/ApplicationInsights-node.js
Pull request #1146 (2023-05-18 16:21:50)

[Beta] Read Resource attributes from env
This is needed for AKS integration and new OTel Resources event


Patch:
diff --git a/src/shared/configuration/applicationInsightsConfig.ts b/src/shared/configuration/applicationInsightsConfig.ts
index 60b2b58..c7e07c8 100644
--- a/src/shared/configuration/applicationInsightsConfig.ts
+++ b/src/shared/configuration/applicationInsightsConfig.ts
@@ -13,7 +13,7 @@ import {
 } from "./types";
 import { JsonConfig } from "./jsonConfig";
 import { Logger } from "../logging";
-import { Resource } from "@opentelemetry/resources";
+import { Resource, ResourceDetectionConfig, detectResourcesSync, envDetectorSync } from "@opentelemetry/resources";
 import { SemanticResourceAttributes, TelemetrySdkLanguageValues } from "@opentelemetry/semantic-conventions";
 
 // Azure Connection String
@@ -165,14 +165,21 @@ export class ApplicationInsightsConfig implements IConfig {
     }
 
     private _getDefaultResource(): Resource {
-        const resource = Resource.EMPTY;
-        resource.attributes[SemanticResourceAttributes.SERVICE_NAME] = DEFAULT_ROLE_NAME;
+        let resource = Resource.EMPTY;
+        // Load resource attributes from env
+        let detectResourceConfig: ResourceDetectionConfig = {
+            detectors: [envDetectorSync]
+        };
+        const envResource =detectResourcesSync(detectResourceConfig);
+        resource = resource.merge(envResource);
+
+        resource.attributes[SemanticResourceAttributes.SERVICE_NAME] = resource.attributes[SemanticResourceAttributes.SERVICE_NAME] || DEFAULT_ROLE_NAME;
         if (process.env.WEBSITE_SITE_NAME) {
             // Azure Web apps and Functions
             resource.attributes[SemanticResourceAttributes.SERVICE_NAME] =
                 process.env.WEBSITE_SITE_NAME;
         }
-        resource.attributes[SemanticResourceAttributes.SERVICE_INSTANCE_ID] = os && os.hostname();
+        resource.attributes[SemanticResourceAttributes.SERVICE_INSTANCE_ID] = resource.attributes[SemanticResourceAttributes.SERVICE_INSTANCE_ID] || os && os.hostname();
         if (process.env.WEBSITE_INSTANCE_ID) {
             resource.attributes[SemanticResourceAttributes.SERVICE_INSTANCE_ID] =
                 process.env.WEBSITE_INSTANCE_ID;


Test patch:
diff --git a/test/unitTests/shared/config.tests.ts b/test/unitTests/shared/config.tests.ts
index 39be671..40b49c9 100644
--- a/test/unitTests/shared/config.tests.ts
+++ b/test/unitTests/shared/config.tests.ts
@@ -275,6 +275,39 @@ describe("Library/Config", () => {
             );
         });
 
+        it("OTEL_RESOURCE_ATTRIBUTES", () => {
+            const env = <{ [id: string]: string }>{};
+            const originalEnv = process.env;
+            env.OTEL_RESOURCE_ATTRIBUTES = "service.name=testServiceName,service.instance.id=testServiceInstance,k8s.cluster.name=testClusterName,k8s.node.name=testNodeName";
+            process.env = env;
+            const config = new ApplicationInsightsConfig();
+            process.env = originalEnv;
+            assert.equal(
+                config.resource.attributes[
+                SemanticResourceAttributes.SERVICE_NAME
+                ],
+                "testServiceName"
+            );
+            assert.equal(
+                config.resource.attributes[
+                SemanticResourceAttributes.SERVICE_INSTANCE_ID
+                ],
+                "testServiceInstance"
+            );
+            assert.equal(
+                config.resource.attributes[
+                SemanticResourceAttributes.K8S_CLUSTER_NAME
+                ],
+                "testClusterName"
+            );
+            assert.equal(
+                config.resource.attributes[
+                SemanticResourceAttributes.K8S_NODE_NAME
+                ],
+                "testNodeName"
+            );
+        });
+
         it("should correctly set Azure attributes", () => {
             const env = <{ [id: string]: string }>{};
             const originalEnv = process.env;