Skip to content

Update logging instrumentation #577

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"dependencies": {
"@types/koa": "^2.11.0",
"@types/koa-compose": "^3.2.3",
"@vtex/diagnostics-nodejs": "0.1.0-beta.8",
"@vtex/node-error-report": "^0.0.3",
"@wry/equality": "^0.1.9",
"agentkeepalive": "^4.0.2",
Expand Down Expand Up @@ -121,7 +122,28 @@
"tslint-config-vtex": "^2.1.0",
"tslint-eslint-rules": "^5.4.0",
"typemoq": "^2.1.0",
"typescript": "^3.8.3",
"typescript": "^4.4.4",
"typescript-json-schema": "^0.40.0"
},
"resolutions": {
"@opentelemetry/core": "1.30.1",
"@opentelemetry/exporter-metrics-otlp-grpc": "0.57.2",
"@opentelemetry/exporter-metrics-otlp-http": "0.57.2",
"@opentelemetry/exporter-logs-otlp-grpc": "0.57.2",
"@opentelemetry/exporter-logs-otlp-http": "0.57.2",
"@opentelemetry/exporter-trace-otlp-grpc": "0.57.2",
"@opentelemetry/exporter-trace-otlp-http": "0.57.2",
"@opentelemetry/instrumentation": "0.57.2",
"@opentelemetry/instrumentation-http": "0.57.2",
"@opentelemetry/instrumentation-grpc": "0.57.2",
"@opentelemetry/instrumentation-net": "0.43.1",
"@opentelemetry/propagator-b3": "1.30.1",
"@opentelemetry/resource-detector-aws": "1.12.0",
"@opentelemetry/resources": "1.30.1",
"@opentelemetry/sdk-node": "0.57.2",
"@opentelemetry/sdk-logs": "0.57.2",
"@opentelemetry/sdk-metrics": "1.30.1",
"@opentelemetry/sdk-trace-base": "1.30.1",
"@opentelemetry/sdk-trace-node": "1.30.1"
}
}
6 changes: 3 additions & 3 deletions src/HttpClient/middlewares/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@ export const cacheMiddleware = ({ type, storage, asyncSet }: CacheOptions) => {


const cacheReadSpan = createCacheSpan(cacheType, 'read', tracer, span)
let cached: void | Cached
let cached: void | Cached = undefined
try {
const cacheHasWithSegment = await storage.has(keyWithSegment)
cached = cacheHasWithSegment ? await storage.get(keyWithSegment) : await storage.get(key)
} catch (error) {
Comment on lines +112 to -116
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These will probably conflict with the changes I made myself in #574. I believe I like your implementation better, so I might keep the changes you made here in the future.

} catch (error: any) {
ErrorReport.create({ originalError: error }).injectOnSpan(cacheReadSpan)
logger?.warn({ message: 'Error reading from the HttpClient cache', error })
} finally {
Expand Down Expand Up @@ -247,7 +247,7 @@ export const cacheMiddleware = ({ type, storage, asyncSet }: CacheOptions) => {
[HttpCacheLogFields.RESPONSE_TYPE]: responseType,
})
}
} catch (error) {
} catch (error: any) {
ErrorReport.create({ originalError: error }).injectOnSpan(cacheWriteSpan)
logger?.warn({ message: 'Error writing to the HttpClient cache', error })
} finally {
Expand Down
2 changes: 1 addition & 1 deletion src/HttpClient/middlewares/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const metricsMiddleware = ({metrics, serverTiming, name}: MetricsOpts) =>
if (ctx.config.metric && ctx.response && ctx.response.status) {
status = statusLabel(ctx.response.status)
}
} catch (err) {
} catch (err: any) {
const isCancelled = (err.message === cancelMessage)
if (ctx.config.metric) {
errorCode = err.code
Expand Down
2 changes: 1 addition & 1 deletion src/HttpClient/middlewares/recorder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const recorderMiddleware = (recorder: Recorder) =>
if (ctx.response) {
(recorder as Recorder).record(ctx.response.headers)
}
} catch (err) {
} catch (err: any) {
if (err.response && err.response.headers && err.response.status === 404) {
(recorder as Recorder).record(err.response.headers)
}
Expand Down
2 changes: 1 addition & 1 deletion src/HttpClient/middlewares/tracing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const createHttpClientTracingMiddleware = ({
try {
await next()
response = ctx.response
} catch (err) {
} catch (err: any) {
response = err.response
if(ctx.tracing?.isSampled) {
ErrorReport.create({ originalError: err }).injectOnSpan(span, logger)
Expand Down
2 changes: 1 addition & 1 deletion src/caches/DiskCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class DiskCache<V> implements CacheLayer<string, V>{
resolve(fileData)
} catch (e) {
release()
resolve(undefined)
resolve(null as unknown as V)
}
})
})
Expand Down
2 changes: 1 addition & 1 deletion src/caches/LRUDiskCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export class LRUDiskCache<V> implements CacheLayer<string, V>{
resolve(fileData)
} catch (e) {
release()
resolve(undefined)
resolve(null as unknown as V)
}
})
})
Expand Down
32 changes: 16 additions & 16 deletions src/clients/infra/Apps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@ export class Apps extends InfraClient {
return this.installRuntime(descriptor, tracingConfig)
}

const metric = 'apps-install'
const metric = 'apps-install'
return this.http.post<AppInstallResponse>(
this.routes.Apps(),
{ id: descriptor },
{
{
metric,
tracing: {
requestSpanNameSuffix: metric,
Expand All @@ -131,9 +131,9 @@ export class Apps extends InfraClient {
}

public uninstallApp = (app: string, tracingConfig?: RequestTracingConfig) => {
const metric = 'apps-uninstall'
return this.http.delete(this.routes.App(app), {
metric,
const metric = 'apps-uninstall'
return this.http.delete(this.routes.App(app), {
metric,
tracing: {
requestSpanNameSuffix: metric,
...tracingConfig?.tracing,
Expand All @@ -142,9 +142,9 @@ export class Apps extends InfraClient {
}

public acknowledgeApp = (app: string, service: string, tracingConfig?: RequestTracingConfig) => {
const metric = 'apps-ack'
return this.http.put(this.routes.Acknowledge(app, service), null, {
metric,
const metric = 'apps-ack'
return this.http.put(this.routes.Acknowledge(app, service), null, {
metric,
tracing: {
requestSpanNameSuffix: metric,
...tracingConfig?.tracing,
Expand Down Expand Up @@ -189,7 +189,7 @@ export class Apps extends InfraClient {
const [response] = await Promise.all([request, finalize])
response.bundleSize = zip.pointer()
return response
} catch (e) {
} catch (e: any) {
e.bundleSize = zip.pointer()
throw e
}
Expand All @@ -212,7 +212,7 @@ export class Apps extends InfraClient {
throw e
})

const metric = 'apps-patch'
const metric = 'apps-patch'
const request = this.http.patch(this.routes.Link(app), zip, {
headers: { 'Content-Type': 'application/zip' },
metric,
Expand Down Expand Up @@ -252,8 +252,8 @@ export class Apps extends InfraClient {
const headers = {'Content-Type': 'application/json'}
const metric = 'apps-save'
return this.http.put(this.routes.Settings(app), settings, {
headers,
metric,
headers,
metric,
tracing: {
requestSpanNameSuffix: metric,
...tracingConfig?.tracing,
Expand Down Expand Up @@ -302,7 +302,7 @@ export class Apps extends InfraClient {

public listLinks = (tracingConfig?: RequestTracingConfig) => {
const inflightKey = inflightURL
const metric = 'apps-list-links'
const metric = 'apps-list-links'
return this.http.get<string[]>(this.routes.Links(), {
inflightKey,
metric,
Expand Down Expand Up @@ -363,7 +363,7 @@ export class Apps extends InfraClient {

public getFileFromApps = <T extends object | null>(app: string, path: string, nullIfNotFound?: boolean, tracingConfig?: RequestTracingConfig) => {
const inflightKey = inflightURL
const metric = 'get-file-from-apps'
const metric = 'get-file-from-apps'
return this.http.get<T>(this.routes.FileFromApps(app, path), {
cacheable: CacheType.Memory,
inflightKey,
Expand Down Expand Up @@ -519,7 +519,7 @@ export class Apps extends InfraClient {
}

public updateDependencies = (tracingConfig?: RequestTracingConfig) => {
const metric = 'apps-update-deps'
const metric = 'apps-update-deps'
return this.http.put<Record<string, string[]>>(this.routes.Dependencies(), null, {
metric,
tracing: {
Expand All @@ -530,7 +530,7 @@ export class Apps extends InfraClient {
}

public updateDependency = (name: string, version: string, registry: string, tracingConfig?: RequestTracingConfig) => {
const metric = 'apps-update-dep'
const metric = 'apps-update-dep'
return this.http.patch(this.routes.Apps(), [{name, version, registry}], {
metric,
tracing: {
Expand Down
2 changes: 1 addition & 1 deletion src/clients/infra/Registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ export class Registry extends InfraClient {
const [response] = await Promise.all([request, finalize])
response.bundleSize = zip.pointer()
return response
} catch (e) {
} catch (e: any) {
e.bundleSize = zip.pointer()
throw e
}
Expand Down
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,5 @@ export const PRODUCTION = process.env.VTEX_PRODUCTION === 'true'
export const INSPECT_DEBUGGER_PORT = 5858

export const cancellableMethods = new Set(['GET', 'OPTIONS', 'HEAD'])

export const LOG_CLIENT_INIT_TIMEOUT_MS = 5000
2 changes: 1 addition & 1 deletion src/service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const startApp = () => {
// to setup server configurations and share port address for incoming requests
startWorker(serviceJSON).listen(HTTP_SERVER_PORT)
}
} catch (err) {
} catch (err: any) {
logOnceToDevConsole(err.stack || err.message, LogLevel.Error)
process.exit(2)
}
Expand Down
54 changes: 54 additions & 0 deletions src/service/logger/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Exporters } from '@vtex/diagnostics-nodejs';
import { LogClient } from '@vtex/diagnostics-nodejs/dist/types';
import { getTelemetryClient } from '../telemetry';

let logClient: LogClient | undefined;
let isInitializing = false;
let initPromise: Promise<LogClient> | undefined = undefined;

export async function getLogClient(account: string, workspace: string, appName: string): Promise<LogClient> {

if (logClient) {
return logClient;
}

if (initPromise) {
return initPromise;
}

isInitializing = true;
initPromise = initializeClient(account, workspace, appName);

return initPromise;
}

async function initializeClient(account: string, workspace: string, appName: string): Promise<LogClient> {
try {
const telemetryClient = await getTelemetryClient();

const logsConfig = Exporters.CreateLogsExporterConfig({
endpoint: process.env.OTEL_EXPORTER_OTLP_ENDPOINT,
path: process.env.OTEL_EXPORTER_OTLP_PATH || '/v1/logs',
protocol: 'http',
interval: 5,
timeoutSeconds: 5,
headers: { 'Content-Type': 'application/json' },
});

const logsExporter = Exporters.CreateExporter(logsConfig, 'otlp');
await logsExporter.initialize();

const clientKey = `${account}-${workspace}-${appName}`;
logClient = await telemetryClient.newLogsClient({
exporter: logsExporter,
loggerName: `node-vtex-api-${clientKey}`,
});

return logClient;
} catch (error) {
console.error('Failed to initialize logs client:', error);
throw error;
} finally {
isInitializing = false;
}
}
2 changes: 1 addition & 1 deletion src/service/logger/console.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { isMaster, isWorker } from 'cluster'

import { LRUCache } from '../../caches'
import { LogLevel } from './logger'
import { LogLevel } from './loggerTypes'

export interface LogMessage {
cmd: typeof LOG_ONCE
Expand Down
2 changes: 2 additions & 0 deletions src/service/logger/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export * from './console'
export * from './logger'
export * from './loggerTypes'
export * from './client'
Loading
Loading