Skip to content

Commit

Permalink
feat(nodejs): add logger sdk, fix metrics and update some packages (o…
Browse files Browse the repository at this point in the history
…pen-telemetry#1464)

* feat(nodejs): add logger sdk, fix metrics and update some packages

* chore: add missing logs package

* feat: log exporter needed

* fix: set global logger
  • Loading branch information
pragmaticivan authored Aug 14, 2024
1 parent 0725e69 commit c9c78cb
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 15 deletions.
25 changes: 14 additions & 11 deletions nodejs/packages/layer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,17 @@
"node": ">=16.0.0"
},
"dependencies": {
"@opentelemetry/api": "^1.7.0",
"@opentelemetry/api": "^1.9.0",
"@opentelemetry/api-logs": "^0.52.1",
"@opentelemetry/exporter-logs-otlp-proto": "^0.52.1",
"@opentelemetry/auto-configuration-propagators": "^0.2.0",
"@opentelemetry/exporter-metrics-otlp-proto": "^0.52.1",
"@opentelemetry/exporter-trace-otlp-proto": "^0.52.1",
"@opentelemetry/instrumentation": "^0.52.1",
"@opentelemetry/instrumentation-aws-lambda": "^0.43.0",
"@opentelemetry/instrumentation-aws-sdk": "^0.43.0",
"@opentelemetry/instrumentation-aws-sdk": "^0.43.1",
"@opentelemetry/instrumentation-dns": "^0.38.0",
"@opentelemetry/instrumentation-express": "^0.41.0",
"@opentelemetry/instrumentation-express": "^0.41.1",
"@opentelemetry/instrumentation-graphql": "^0.42.0",
"@opentelemetry/instrumentation-grpc": "^0.52.1",
"@opentelemetry/instrumentation-hapi": "^0.40.0",
Expand All @@ -47,17 +49,18 @@
"@opentelemetry/instrumentation-net": "^0.38.0",
"@opentelemetry/instrumentation-pg": "^0.43.0",
"@opentelemetry/instrumentation-redis": "^0.41.0",
"@opentelemetry/propagator-aws-xray": "^1.3.1",
"@opentelemetry/resource-detector-aws": "^1.3.4",
"@opentelemetry/resources": "^1.18.1",
"@opentelemetry/sdk-metrics": "^1.18.1",
"@opentelemetry/sdk-trace-base": "^1.18.1",
"@opentelemetry/sdk-trace-node": "^1.18.1"
"@opentelemetry/propagator-aws-xray": "^1.25.1",
"@opentelemetry/resource-detector-aws": "^1.5.2",
"@opentelemetry/resources": "^1.25.1",
"@opentelemetry/sdk-logs": "^0.52.1",
"@opentelemetry/sdk-metrics": "^1.25.1",
"@opentelemetry/sdk-trace-base": "^1.25.1",
"@opentelemetry/sdk-trace-node": "^1.25.1"
},
"devDependencies": {
"@types/mocha": "^10.0.6",
"@types/mocha": "^10.0.7",
"@types/sinon": "^17.0.3",
"mocha": "^10.4.0",
"mocha": "^10.7.0",
"sinon": "^18.0.0",
"ts-node": "^10.9.2"
}
Expand Down
43 changes: 39 additions & 4 deletions nodejs/packages/layer/src/wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,25 @@ import {
processDetector,
} from '@opentelemetry/resources';
import { AwsInstrumentation, AwsSdkInstrumentationConfig } from '@opentelemetry/instrumentation-aws-sdk';
import { AwsLambdaInstrumentation } from '@opentelemetry/instrumentation-aws-lambda';
import { AwsLambdaInstrumentation, AwsLambdaInstrumentationConfig} from '@opentelemetry/instrumentation-aws-lambda';
import {
diag,
DiagConsoleLogger,
DiagLogLevel,
} from "@opentelemetry/api";
import { getEnv } from '@opentelemetry/core';
import { AwsLambdaInstrumentationConfig } from '@opentelemetry/instrumentation-aws-lambda';
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-proto';
import { MeterProvider, MeterProviderOptions } from '@opentelemetry/sdk-metrics';
import { MeterProvider, MeterProviderOptions, PeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics';
import { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-proto';
import { OTLPLogExporter } from '@opentelemetry/exporter-logs-otlp-proto';
import { getPropagator } from '@opentelemetry/auto-configuration-propagators';
import {
LoggerProvider,
SimpleLogRecordProcessor,
ConsoleLogRecordExporter,
LoggerProviderConfig,
} from '@opentelemetry/sdk-logs';
import { logs } from '@opentelemetry/api-logs';

function defaultConfigureInstrumentations() {
// Use require statements for instrumentation to avoid having to have transitive dependencies on all the typescript
Expand Down Expand Up @@ -69,6 +77,7 @@ declare global {
function configureMeterProvider(meterProvider: MeterProvider): void
function configureLambdaInstrumentation(config: AwsLambdaInstrumentationConfig): AwsLambdaInstrumentationConfig
function configureInstrumentations(): Instrumentation[]
function configureLoggerProvider(loggerProvider: LoggerProvider): void
}

console.log('Registering OpenTelemetry');
Expand Down Expand Up @@ -129,8 +138,14 @@ async function initializeProvider() {
tracerProvider.register(sdkRegistrationConfig);

// Configure default meter provider (doesn't export metrics)
const metricExporter = new OTLPMetricExporter();
let meterConfig: MeterProviderOptions = {
resource,
readers: [
new PeriodicExportingMetricReader({
exporter: metricExporter,
})
],
}
if (typeof configureMeter === 'function') {
meterConfig = configureMeter(meterConfig);
Expand All @@ -141,11 +156,31 @@ async function initializeProvider() {
configureMeterProvider(meterProvider)
}

const logExporter = new OTLPLogExporter();
let loggerConfig: LoggerProviderConfig = {
resource,
}
const loggerProvider = new LoggerProvider(loggerConfig);
if (typeof configureLoggerProvider === 'function') {
configureLoggerProvider(loggerProvider)
} else {
loggerProvider.addLogRecordProcessor(new SimpleLogRecordProcessor(logExporter));
logs.setGlobalLoggerProvider(loggerProvider);
}

// logging for debug
if (logLevel === DiagLogLevel.DEBUG) {
loggerProvider.addLogRecordProcessor(
new SimpleLogRecordProcessor(new ConsoleLogRecordExporter())
);
}

// Re-register instrumentation with initialized provider. Patched code will see the update.
registerInstrumentations({
instrumentations,
tracerProvider,
meterProvider
meterProvider,
loggerProvider,
});
}
initializeProvider();

0 comments on commit c9c78cb

Please sign in to comment.