diff --git a/CHANGELOG.md b/CHANGELOG.md index 72f351d4d..98dd78952 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,15 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). ## [Unreleased] + +## [7.1.1] - 2025-09-08 +### Changed +- Add conditional enabling for diagnostics telemetry signals + +## [7.1.0] - 2025-09-08 +### Changed +- Add metrics instrumentation based on diagnostics lib + ## [7.1.0] - 2025-09-02 ### Changed - Add new scope argument into auth directive diff --git a/package.json b/package.json index 67e5e93ac..c632418e8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@vtex/api", - "version": "7.1.0", + "version": "7.1.1", "description": "VTEX I/O API client", "main": "lib/index.js", "typings": "lib/index.d.ts", @@ -53,7 +53,7 @@ "@opentelemetry/instrumentation-koa": "0.47.1", "@types/koa": "^2.11.0", "@types/koa-compose": "^3.2.3", - "@vtex/diagnostics-nodejs": "0.1.0-io-beta.19", + "@vtex/diagnostics-nodejs": "0.1.0-io-beta.20", "@vtex/node-error-report": "^0.0.3", "@wry/equality": "^0.1.9", "agentkeepalive": "^4.0.2", diff --git a/src/constants.ts b/src/constants.ts index 3860b0af3..156860b6c 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -74,3 +74,9 @@ export const INSPECT_DEBUGGER_PORT = 5858 export const cancellableMethods = new Set(['GET', 'OPTIONS', 'HEAD']) export const LOG_CLIENT_INIT_TIMEOUT_MS = 5000 + +export const OTEL_EXPORTER_OTLP_ENDPOINT = process.env.OTEL_EXPORTER_OTLP_ENDPOINT as string; + +export const DK_APP_ID = process.env.NODE_VTEX_API_DK_APP_ID as string || "apps-team"; + +export const DIAGNOSTICS_TELEMETRY_ENABLED = process.env.VTEX_DIAGNOSTICS_TELEMETRY_ENABLED === 'true'; \ No newline at end of file diff --git a/src/service/logger/logger.ts b/src/service/logger/logger.ts index 1441fb55e..3c4678364 100644 --- a/src/service/logger/logger.ts +++ b/src/service/logger/logger.ts @@ -32,7 +32,7 @@ export class Logger { } } - // this.initLogClient(); + this.initLogClient(); } private initLogClient(): Promise { diff --git a/src/service/telemetry/client.ts b/src/service/telemetry/client.ts index db42e276d..2a5312703 100644 --- a/src/service/telemetry/client.ts +++ b/src/service/telemetry/client.ts @@ -6,14 +6,12 @@ import { Metrics, Traces, } from '@vtex/diagnostics-nodejs'; -import { APP } from '../../constants'; +import { APP, OTEL_EXPORTER_OTLP_ENDPOINT, DK_APP_ID, DIAGNOSTICS_TELEMETRY_ENABLED, WORKSPACE, PRODUCTION } from '../../constants'; import { TelemetryClient } from '@vtex/diagnostics-nodejs/dist/telemetry'; import { KoaInstrumentation } from '@opentelemetry/instrumentation-koa'; import { HostMetricsInstrumentation } from '../metrics/instruments/hostMetrics'; -const CLIENT_NAME = APP.NAME || 'node-vtex-api'; const APPLICATION_ID = APP.ID || 'vtex-io-app'; -const EXPORTER_OTLP_ENDPOINT = process.env.EXPORTER_OTLP_ENDPOINT; interface TelemetryClients { logsClient: Logs.LogClient; @@ -38,14 +36,14 @@ class TelemetryClientSingleton { private initializeTracesClient = async (telemetryClient: TelemetryClient) => await telemetryClient.newTracesClient({ exporter: Exporters.CreateExporter(Exporters.CreateTracesExporterConfig({ - endpoint: EXPORTER_OTLP_ENDPOINT, + endpoint: OTEL_EXPORTER_OTLP_ENDPOINT, }), 'otlp'), }); private initializeMetricsClient = async (telemetryClient: TelemetryClient) => await telemetryClient.newMetricsClient({ exporter: Exporters.CreateExporter(Exporters.CreateMetricsExporterConfig({ - endpoint: EXPORTER_OTLP_ENDPOINT, + endpoint: OTEL_EXPORTER_OTLP_ENDPOINT, interval: 5, timeoutSeconds: 5, }), 'otlp'), @@ -54,21 +52,27 @@ class TelemetryClientSingleton { private initializeLogsClient = async (telemetryClient: TelemetryClient) => await telemetryClient.newLogsClient({ exporter: Exporters.CreateExporter(Exporters.CreateLogsExporterConfig({ - endpoint: EXPORTER_OTLP_ENDPOINT, + endpoint: OTEL_EXPORTER_OTLP_ENDPOINT, }), 'otlp'), loggerName: `node-vtex-api-${APPLICATION_ID}`, }); private async initializeTelemetryClients(): Promise { + try { const telemetryClient = await NewTelemetryClient( - APPLICATION_ID, - CLIENT_NAME, + DK_APP_ID, 'node-vtex-api', + APPLICATION_ID, { + // Use built-in no-op functionality when telemetry is disabled + noop: !DIAGNOSTICS_TELEMETRY_ENABLED, additionalAttrs: { + 'app.id': APPLICATION_ID, + 'vendor': APP.VENDOR, 'version': APP.VERSION || '', - 'environment': process.env.VTEX_WORKSPACE || 'development', + 'workspace': WORKSPACE, + 'production': PRODUCTION.toString(), }, } ); @@ -79,16 +83,20 @@ class TelemetryClientSingleton { this.initializeLogsClient(telemetryClient), ]); - const instrumentations = [ - ...Instrumentation.CommonInstrumentations.minimal(), - new KoaInstrumentation(), - new HostMetricsInstrumentation({ - name: 'host-metrics-instrumentation', - meterProvider: metricsClient.provider(), - }), - ]; - - telemetryClient.registerInstrumentations(instrumentations); + if (DIAGNOSTICS_TELEMETRY_ENABLED) { + console.log(`Telemetry enabled for app: ${APP.ID} (vendor: ${APP.VENDOR})`); + + const instrumentations = [ + ...Instrumentation.CommonInstrumentations.minimal(), + new KoaInstrumentation(), + new HostMetricsInstrumentation({ + name: 'host-metrics-instrumentation', + meterProvider: metricsClient.provider(), + }), + ]; + + telemetryClient.registerInstrumentations(instrumentations); + } const clients: TelemetryClients = { logsClient, diff --git a/yarn.lock b/yarn.lock index 0d548c37f..2aaebf317 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1251,10 +1251,10 @@ dependencies: "@types/yargs-parser" "*" -"@vtex/diagnostics-nodejs@0.1.0-io-beta.19": - version "0.1.0-io-beta.19" - resolved "https://registry.yarnpkg.com/@vtex/diagnostics-nodejs/-/diagnostics-nodejs-0.1.0-io-beta.19.tgz#e8ebc6a0b44014d0ffcda9df736291c52604ee9e" - integrity sha512-p3bcfpCI12k1DXqD2wJvO53dDdrULYsK7muJFHlwiZio4WvnSfPU1Ay5ni9Y8alD/MLX4fP4EEjRYXTGc7wFWQ== +"@vtex/diagnostics-nodejs@0.1.0-io-beta.20": + version "0.1.0-io-beta.20" + resolved "https://registry.yarnpkg.com/@vtex/diagnostics-nodejs/-/diagnostics-nodejs-0.1.0-io-beta.20.tgz#79ee25a88827a501206d1a8ef0715674fa1521b5" + integrity sha512-8DbibPcPEd2yJCRKZOM2JAf4FYr9W90lD+DRV/mOwgMd1ZGdZuXupfoqqOzOdrkOidXhCeLBPhRXne8O2N3QEQ== dependencies: "@grpc/grpc-js" "^1.13.4" "@opentelemetry/api" "^1.9.0"