diff --git a/package-lock.json b/package-lock.json index cf03a1553..411d6861a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1217,6 +1217,15 @@ "resolved": "https://registry.npmjs.org/@opentelemetry/redis-common/-/redis-common-0.36.1.tgz", "integrity": "sha512-YjfNEr7DK1Ymc5H0bzhmqVvMcCs+PUEUerzrpTFdHfZxj3HpnnjZTIFKx/gxiL/sajQ8dxycjlreoYTVYKBXlw==" }, + "@opentelemetry/resource-detector-azure": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/@opentelemetry/resource-detector-azure/-/resource-detector-azure-0.2.0.tgz", + "integrity": "sha512-r5BZtXnnebm1HevGEiQfAJM2SjpPVH4EUocypB1hf9peYeMB/ZznRHbwyv48NdTnUa2mS/XlTnPapfd6qTVaiA==", + "requires": { + "@opentelemetry/resources": "^1.10.1", + "@opentelemetry/semantic-conventions": "^1.0.0" + } + }, "@opentelemetry/resources": { "version": "1.17.1", "resolved": "https://registry.npmjs.org/@opentelemetry/resources/-/resources-1.17.1.tgz", diff --git a/package.json b/package.json index 2fd23bae2..d7c22b236 100644 --- a/package.json +++ b/package.json @@ -79,6 +79,7 @@ "@opentelemetry/exporter-metrics-otlp-http": "^0.43.0", "@opentelemetry/exporter-trace-otlp-http": "^0.43.0", "@opentelemetry/otlp-exporter-base": "^0.43.0", + "@opentelemetry/resource-detector-azure": "^0.2.0", "@opentelemetry/resources": "^1.16.0", "@opentelemetry/sdk-logs": "^0.43.0", "@opentelemetry/sdk-metrics": "^1.16.0", diff --git a/src/agent/appServicesLoader.ts b/src/agent/appServicesLoader.ts index a5b2f9204..776426821 100644 --- a/src/agent/appServicesLoader.ts +++ b/src/agent/appServicesLoader.ts @@ -3,14 +3,13 @@ import * as os from 'os'; import * as path from 'path'; -import { Attributes } from '@opentelemetry/api'; -import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions'; -import { Resource } from '@opentelemetry/resources'; import { DiagnosticLogger } from './diagnostics/diagnosticLogger'; import { FileWriter } from "./diagnostics/writers/fileWriter"; import { StatusLogger } from "./diagnostics/statusLogger"; import { AgentLoader } from "./agentLoader"; import { AgentResourceProviderType, AZURE_MONITOR_AGENT_PREFIX } from './types'; +import { detectResourcesSync } from '@opentelemetry/resources'; +import { azureAppServiceDetector } from '@opentelemetry/resource-detector-azure'; export class AppServicesLoader extends AgentLoader { @@ -19,16 +18,7 @@ export class AppServicesLoader extends AgentLoader { super(); if (this._canLoad) { // Azure App Services specific configuration - const resourceAttributes: Attributes = {}; - if (process.env.WEBSITE_SITE_NAME) { - resourceAttributes[SemanticResourceAttributes.SERVICE_NAME] = - process.env.WEBSITE_SITE_NAME; - } - if (process.env.WEBSITE_INSTANCE_ID) { - resourceAttributes[SemanticResourceAttributes.SERVICE_INSTANCE_ID] = - process.env.WEBSITE_INSTANCE_ID; - } - const resource = new Resource(resourceAttributes); + const resource = detectResourcesSync({ detectors: [azureAppServiceDetector] }); this._options.resource = resource; let statusLogDir = '/var/log/applicationinsights/'; diff --git a/src/agent/azureFunctionsLoader.ts b/src/agent/azureFunctionsLoader.ts index 0e6f69061..abcb20dd7 100644 --- a/src/agent/azureFunctionsLoader.ts +++ b/src/agent/azureFunctionsLoader.ts @@ -1,14 +1,13 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. -import { Resource } from "@opentelemetry/resources"; import { AgentLoader } from "./agentLoader"; import { DiagnosticLogger } from "./diagnostics/diagnosticLogger"; import { StatusLogger } from "./diagnostics/statusLogger"; import { AzureFunctionsWriter } from "./diagnostics/writers/azureFunctionsWriter"; import { AgentResourceProviderType, AZURE_MONITOR_AGENT_PREFIX } from "./types"; -import { Attributes } from "@opentelemetry/api"; -import { SemanticResourceAttributes } from "@opentelemetry/semantic-conventions"; +import { detectResourcesSync } from '@opentelemetry/resources'; +import { azureFunctionsDetector } from '@opentelemetry/resource-detector-azure/build/src/detectors/AzureFunctionsDetector'; export class AzureFunctionsLoader extends AgentLoader { @@ -17,17 +16,9 @@ export class AzureFunctionsLoader extends AgentLoader { if (this._canLoad) { // Azure Fn specific configuration this._options.enableAutoCollectPerformance = false; - process.env["APPLICATION_INSIGHTS_NO_STANDARD_METRICS"] = "disable"; - const resourceAttributes: Attributes = {}; - if (process.env.WEBSITE_SITE_NAME) { - resourceAttributes[SemanticResourceAttributes.SERVICE_NAME] = - process.env.WEBSITE_SITE_NAME; - } - if (process.env.WEBSITE_INSTANCE_ID) { - resourceAttributes[SemanticResourceAttributes.SERVICE_INSTANCE_ID] = - process.env.WEBSITE_INSTANCE_ID; - } - const resource = new Resource(resourceAttributes); + const resource = detectResourcesSync({ + detectors: [azureFunctionsDetector], + }); this._options.resource = resource; const writer = new AzureFunctionsWriter(this._instrumentationKey); diff --git a/test/unitTests/agent/azureFunctionsLoader.ts b/test/unitTests/agent/azureFunctionsLoader.ts index d95152254..cbbf03724 100644 --- a/test/unitTests/agent/azureFunctionsLoader.ts +++ b/test/unitTests/agent/azureFunctionsLoader.ts @@ -71,11 +71,11 @@ describe("agent/AzureFunctionsLoader", () => { // Agent Loader called assert.ok(stub.calledOnce); assert.equal( - agent["_options"].resource.attributes[SemanticResourceAttributes.SERVICE_INSTANCE_ID], + agent["_options"].resource.attributes[SemanticResourceAttributes.FAAS_INSTANCE], "testRoleInstanceId" ); assert.equal( - agent["_options"].resource.attributes[SemanticResourceAttributes.SERVICE_NAME], + agent["_options"].resource.attributes[SemanticResourceAttributes.FAAS_NAME], "testRole" ); });