Skip to content

Commit 41c8ccb

Browse files
authored
Enable telemetry control from env vars (#605)
* Bump diagnostics-nodejs version * Refactor telemetry client to support all signals Enables this client to support logs, metrics, and tracing signals from the diagnostics-nodejs library, making them available already initialized. Additionally, this change enables registration of built-in and community instrumentation. * Refactor getLogClient to simplify initialization Remove unused parameters and use logClient already initialized * Refactor logger client types Refactor client types to use Types.LogClient for consistency * Add resolution for @grpc/grpc-js dependency * Update package version to 6.49.8-beta.0 * Refactor singleton to use dedicated initialization methods for telemetry clients * Refactor telemetry client initialization logic Improve telemetry client initialization using Promise.all to optimize asynchronous calls * Update package.json and yarn.lock Adds OpenTelemetry dependencies for host metrics and Koa instrumentation * Update startApp function to proper init telemetry Updates startApp function to initialize telemetry beforehand and uses dynamic imports for startMaster and startWorker. This ensures that telemetry clients and libraries are proper initialized so they can use hooks with application libraries that will be loaded later * Add metrics client Add getMetricClient function to make the client available and creates the asynchronous initialization function to retrieve it * Add metrics instruments for monitoring HTTP requests * Add middleware for request metrics This commit creates a middleware for request metrics using OpenTelemetry instruments. This change is inspired by `requestMetricsMiddleware.ts` and follows the same logic, only changing the way it is instrumented to use OTel standard * Add middleware usage on app Add the use of OpenTelemetry metrics middleware for request monitoring * Add Koa instrumentation to telemetry client Add Koa instrumentation to the list of instruments that will be registered and used by the telemetry client. This enables automatic instrumentation for the Koa module, as well as automatic collection and export of telemetry data. * Add Koa context propagation middleware to app worker * Add host-metrics instrumentation This commit creates a wrapper for the host-metrics module, which provides automatic collection for system metrics - such as CPU, memory, and network * Add host-metrics instrumentation to telemetry client * Release v7.0.1 * Release v7.1.0-beta.0 * Improves code formatting for setTimeout * Refactor instrument init logic on middleware Refactors the instrument initialization logic to improve readability and code structure * Refactor metric client module Refactor MetricClient to use Singleton pattern for improved client management * Refactor metrics instruments module Refactor OTel instruments initialization to use singleton pattern * Add error handling when init instruments * Bump @vtex/diagnostics-nodejs to 0.1.0-io-beta.20 * Add new constants for diagnostics configuration This commit creates new constants based on environment variables that will be used to proper configure diagnostics-nodejs on node-vtex-api * Enable log client initialization * Add new constants to client.ts Make the new constants available on clients.ts file, which is responsible for create telemetry client. * Make clients use new constant for endpoint This commit replaces and updates the telemetry clients to utilize the OTEL_EXPORTER_OTLP_ENDPOINT constant for exporting telemetry data * Use DK_APP_ID on telemetry client creation Small change on parameter to follow o11y team guidelines regarding this field * Refactor telemetry client parameters Updates parameters on telemetry client creation, making use of APPLICATION_ID and adding new additional attributes. Also, we remove unused constant CLIENT_NAME. * Refactor telemetry client initialization with checks This update adds console logging for diagnostics configuration and checks if telemetry is enabled before registering instrumentations. It ensures that the telemetry client operates correctly based on the DIAGNOSTICS_TELEMETRY_ENABLED constant. Also, whenever the app vendor is not enabled it will use a no-op client for diagnostics, meaning any telemetry signal will be ignored * Update constant default value This change modifies the DK_APP_ID constant to default to 'apps-team' instead of 'apps' * improve logging approach for telemetry status Replace negative warning message when telemetry is disabled with positive confirmation message when enabled. This change aligns with the principle of treating disabled telemetry as the default state. * Add new attributes to telemetry client * release v7.1.1 * update CHANGELOG
1 parent e27cbcb commit 41c8ccb

6 files changed

Lines changed: 49 additions & 26 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
66
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
77

88
## [Unreleased]
9+
10+
## [7.1.1] - 2025-09-08
11+
### Changed
12+
- Add conditional enabling for diagnostics telemetry signals
13+
14+
## [7.1.0] - 2025-09-08
15+
### Changed
16+
- Add metrics instrumentation based on diagnostics lib
17+
918
## [7.1.0] - 2025-09-02
1019
### Changed
1120
- Add new scope argument into auth directive

‎package.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@vtex/api",
3-
"version": "7.1.0",
3+
"version": "7.1.1",
44
"description": "VTEX I/O API client",
55
"main": "lib/index.js",
66
"typings": "lib/index.d.ts",
@@ -53,7 +53,7 @@
5353
"@opentelemetry/instrumentation-koa": "0.47.1",
5454
"@types/koa": "^2.11.0",
5555
"@types/koa-compose": "^3.2.3",
56-
"@vtex/diagnostics-nodejs": "0.1.0-io-beta.19",
56+
"@vtex/diagnostics-nodejs": "0.1.0-io-beta.20",
5757
"@vtex/node-error-report": "^0.0.3",
5858
"@wry/equality": "^0.1.9",
5959
"agentkeepalive": "^4.0.2",

‎src/constants.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,9 @@ export const INSPECT_DEBUGGER_PORT = 5858
7474
export const cancellableMethods = new Set(['GET', 'OPTIONS', 'HEAD'])
7575

7676
export const LOG_CLIENT_INIT_TIMEOUT_MS = 5000
77+
78+
export const OTEL_EXPORTER_OTLP_ENDPOINT = process.env.OTEL_EXPORTER_OTLP_ENDPOINT as string;
79+
80+
export const DK_APP_ID = process.env.NODE_VTEX_API_DK_APP_ID as string || "apps-team";
81+
82+
export const DIAGNOSTICS_TELEMETRY_ENABLED = process.env.VTEX_DIAGNOSTICS_TELEMETRY_ENABLED === 'true';

‎src/service/logger/logger.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export class Logger {
3232
}
3333
}
3434

35-
// this.initLogClient();
35+
this.initLogClient();
3636
}
3737

3838
private initLogClient(): Promise<Types.LogClient | undefined> {

‎src/service/telemetry/client.ts‎

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@ import {
66
Metrics,
77
Traces,
88
} from '@vtex/diagnostics-nodejs';
9-
import { APP } from '../../constants';
9+
import { APP, OTEL_EXPORTER_OTLP_ENDPOINT, DK_APP_ID, DIAGNOSTICS_TELEMETRY_ENABLED, WORKSPACE, PRODUCTION } from '../../constants';
1010
import { TelemetryClient } from '@vtex/diagnostics-nodejs/dist/telemetry';
1111
import { KoaInstrumentation } from '@opentelemetry/instrumentation-koa';
1212
import { HostMetricsInstrumentation } from '../metrics/instruments/hostMetrics';
1313

14-
const CLIENT_NAME = APP.NAME || 'node-vtex-api';
1514
const APPLICATION_ID = APP.ID || 'vtex-io-app';
16-
const EXPORTER_OTLP_ENDPOINT = process.env.EXPORTER_OTLP_ENDPOINT;
1715

1816
interface TelemetryClients {
1917
logsClient: Logs.LogClient;
@@ -38,14 +36,14 @@ class TelemetryClientSingleton {
3836
private initializeTracesClient = async (telemetryClient: TelemetryClient) =>
3937
await telemetryClient.newTracesClient({
4038
exporter: Exporters.CreateExporter(Exporters.CreateTracesExporterConfig({
41-
endpoint: EXPORTER_OTLP_ENDPOINT,
39+
endpoint: OTEL_EXPORTER_OTLP_ENDPOINT,
4240
}), 'otlp'),
4341
});
4442

4543
private initializeMetricsClient = async (telemetryClient: TelemetryClient) =>
4644
await telemetryClient.newMetricsClient({
4745
exporter: Exporters.CreateExporter(Exporters.CreateMetricsExporterConfig({
48-
endpoint: EXPORTER_OTLP_ENDPOINT,
46+
endpoint: OTEL_EXPORTER_OTLP_ENDPOINT,
4947
interval: 5,
5048
timeoutSeconds: 5,
5149
}), 'otlp'),
@@ -54,21 +52,27 @@ class TelemetryClientSingleton {
5452
private initializeLogsClient = async (telemetryClient: TelemetryClient) =>
5553
await telemetryClient.newLogsClient({
5654
exporter: Exporters.CreateExporter(Exporters.CreateLogsExporterConfig({
57-
endpoint: EXPORTER_OTLP_ENDPOINT,
55+
endpoint: OTEL_EXPORTER_OTLP_ENDPOINT,
5856
}), 'otlp'),
5957
loggerName: `node-vtex-api-${APPLICATION_ID}`,
6058
});
6159

6260
private async initializeTelemetryClients(): Promise<TelemetryClients> {
61+
6362
try {
6463
const telemetryClient = await NewTelemetryClient(
65-
APPLICATION_ID,
66-
CLIENT_NAME,
64+
DK_APP_ID,
6765
'node-vtex-api',
66+
APPLICATION_ID,
6867
{
68+
// Use built-in no-op functionality when telemetry is disabled
69+
noop: !DIAGNOSTICS_TELEMETRY_ENABLED,
6970
additionalAttrs: {
71+
'app.id': APPLICATION_ID,
72+
'vendor': APP.VENDOR,
7073
'version': APP.VERSION || '',
71-
'environment': process.env.VTEX_WORKSPACE || 'development',
74+
'workspace': WORKSPACE,
75+
'production': PRODUCTION.toString(),
7276
},
7377
}
7478
);
@@ -79,16 +83,20 @@ class TelemetryClientSingleton {
7983
this.initializeLogsClient(telemetryClient),
8084
]);
8185

82-
const instrumentations = [
83-
...Instrumentation.CommonInstrumentations.minimal(),
84-
new KoaInstrumentation(),
85-
new HostMetricsInstrumentation({
86-
name: 'host-metrics-instrumentation',
87-
meterProvider: metricsClient.provider(),
88-
}),
89-
];
90-
91-
telemetryClient.registerInstrumentations(instrumentations);
86+
if (DIAGNOSTICS_TELEMETRY_ENABLED) {
87+
console.log(`Telemetry enabled for app: ${APP.ID} (vendor: ${APP.VENDOR})`);
88+
89+
const instrumentations = [
90+
...Instrumentation.CommonInstrumentations.minimal(),
91+
new KoaInstrumentation(),
92+
new HostMetricsInstrumentation({
93+
name: 'host-metrics-instrumentation',
94+
meterProvider: metricsClient.provider(),
95+
}),
96+
];
97+
98+
telemetryClient.registerInstrumentations(instrumentations);
99+
}
92100

93101
const clients: TelemetryClients = {
94102
logsClient,

‎yarn.lock‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,10 +1251,10 @@
12511251
dependencies:
12521252
"@types/yargs-parser" "*"
12531253

1254-
"@vtex/diagnostics-nodejs@0.1.0-io-beta.19":
1255-
version "0.1.0-io-beta.19"
1256-
resolved "https://registry.yarnpkg.com/@vtex/diagnostics-nodejs/-/diagnostics-nodejs-0.1.0-io-beta.19.tgz#e8ebc6a0b44014d0ffcda9df736291c52604ee9e"
1257-
integrity sha512-p3bcfpCI12k1DXqD2wJvO53dDdrULYsK7muJFHlwiZio4WvnSfPU1Ay5ni9Y8alD/MLX4fP4EEjRYXTGc7wFWQ==
1254+
"@vtex/diagnostics-nodejs@0.1.0-io-beta.20":
1255+
version "0.1.0-io-beta.20"
1256+
resolved "https://registry.yarnpkg.com/@vtex/diagnostics-nodejs/-/diagnostics-nodejs-0.1.0-io-beta.20.tgz#79ee25a88827a501206d1a8ef0715674fa1521b5"
1257+
integrity sha512-8DbibPcPEd2yJCRKZOM2JAf4FYr9W90lD+DRV/mOwgMd1ZGdZuXupfoqqOzOdrkOidXhCeLBPhRXne8O2N3QEQ==
12581258
dependencies:
12591259
"@grpc/grpc-js" "^1.13.4"
12601260
"@opentelemetry/api" "^1.9.0"

0 commit comments

Comments
 (0)