From 2a9d9ee07537b83469e2b8e9d7e875f8e28081d7 Mon Sep 17 00:00:00 2001 From: Hector Hernandez <39923391+hectorhdzg@users.noreply.github.com> Date: Thu, 18 May 2023 14:44:18 -0700 Subject: [PATCH] Read Resource attributes from env (#1146) --- .../applicationInsightsConfig.ts | 15 ++++++--- test/unitTests/shared/config.tests.ts | 33 +++++++++++++++++++ 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/src/shared/configuration/applicationInsightsConfig.ts b/src/shared/configuration/applicationInsightsConfig.ts index 60b2b584c..c7e07c86b 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; diff --git a/test/unitTests/shared/config.tests.ts b/test/unitTests/shared/config.tests.ts index 39be671e1..40b49c9d3 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;