Skip to content

Commit

Permalink
Warm up http/proto exporter by sending special spans (#56)
Browse files Browse the repository at this point in the history
* Use http/proto exporter instead of grpc to reduce the cost of loading libraries

* Disable tracing/metrics of http exporter

* Warm up http/proto exporter by sending special spans
  • Loading branch information
RafalSumislawski authored Dec 4, 2024
1 parent 6b730a6 commit dd9429a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
24 changes: 22 additions & 2 deletions nodejs/packages/cx-wrapper/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,45 @@ import { load } from './loader.js';
import { initializeInstrumentations } from './instrumentation-init.js';
import { initializeProvider } from './provider-init.js';
import { makeLambdaInstrumentation } from './lambda-instrumentation-init.js';
import { parseBooleanEnvvar } from './common.js';

const instrumentations = initializeInstrumentations();
initializeProvider(instrumentations);
const tracerProvider = initializeProvider(instrumentations);
const lambdaInstrumentation = makeLambdaInstrumentation();

if (process.env.CX_ORIGINAL_HANDLER === undefined)
throw Error('CX_ORIGINAL_HANDLER is missing');

// We want user code to get initialized during lambda init phase
diag.debug(`Initialization: Loading original handler ${process.env.CX_ORIGINAL_HANDLER}`);
try {
(async () => {
diag.debug(`Initialization: Loading original handler ${process.env.CX_ORIGINAL_HANDLER}`);
await load(
process.env.LAMBDA_TASK_ROOT,
process.env.CX_ORIGINAL_HANDLER
);
diag.debug(`Initialization: Original handler loaded`);
})();
} catch (e) {}

if (parseBooleanEnvvar("OTEL_WARM_UP_EXPORTER") ?? true) {
// We want exporter code to get initialized during lambda init phase
try {
(async () => {
try {
diag.debug(`Initialization: warming up exporter`);
const warmupSpan = tracerProvider.getTracer('cx-wrapper').startSpan('warmup');
warmupSpan.setAttribute('cx.internal.span.role', 'warmup');
warmupSpan.end();
await tracerProvider.forceFlush();
diag.debug(`Initialization: exporter warmed up`);
} catch (e) {
diag.error(`Initialization: exporter warmup failed: ${e}`);
}
})();
} catch (e) {}
}

export const handler = (event: any, context: Context, callback: Callback) => {
diag.debug(`Loading original handler ${process.env.CX_ORIGINAL_HANDLER}`);
load(
Expand Down
6 changes: 4 additions & 2 deletions nodejs/packages/cx-wrapper/provider-init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ declare global {

const DEFAULT_OTEL_EXPORT_TIMEOUT = 2000; // this is a localhost call, and we don't want to block the function for too long

export function initializeProvider(instrumentations: any[]): void {
export function initializeProvider(instrumentations: any[]): NodeTracerProvider {
diag.debug('Initializing OpenTelemetry providers');

const export_timeout = parseIntEnvvar("OTEL_EXPORT_TIMEOUT") ?? DEFAULT_OTEL_EXPORT_TIMEOUT;
Expand Down Expand Up @@ -106,4 +106,6 @@ export function initializeProvider(instrumentations: any[]): void {
tracerProvider,
meterProvider
});
}

return tracerProvider
}
2 changes: 1 addition & 1 deletion nodejs/packages/layer/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit dd9429a

Please sign in to comment.