Skip to content

Commit

Permalink
Read Resource attributes from env (#1146)
Browse files Browse the repository at this point in the history
  • Loading branch information
hectorhdzg committed May 18, 2023
1 parent 08648d9 commit 2a9d9ee
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/shared/configuration/applicationInsightsConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
33 changes: 33 additions & 0 deletions test/unitTests/shared/config.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 2a9d9ee

Please sign in to comment.