Skip to content

Commit

Permalink
Update otel samples to use released package (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
ejizba authored Jul 31, 2024
1 parent 0032091 commit cce03ca
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 180 deletions.
75 changes: 75 additions & 0 deletions js/package-lock.json

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

1 change: 1 addition & 0 deletions js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
},
"dependencies": {
"@azure/functions": "^4.5.0",
"@azure/functions-opentelemetry-instrumentation": "^0.1.0",
"@azure/monitor-opentelemetry-exporter": "^1.0.0-beta.23",
"@opentelemetry/api": "^1.8.0",
"@opentelemetry/auto-instrumentations-node": "^0.46.1",
Expand Down
50 changes: 5 additions & 45 deletions js/src/otelAppInsights.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
const { app } = require('@azure/functions');
const { AzureFunctionsInstrumentation } = require('@azure/functions-opentelemetry-instrumentation');
const { AzureMonitorLogExporter, AzureMonitorTraceExporter } = require('@azure/monitor-opentelemetry-exporter');
const { context: otelContext, propagation } = require('@opentelemetry/api');
const { SeverityNumber } = require('@opentelemetry/api-logs');
const { getNodeAutoInstrumentations, getResourceDetectors } = require('@opentelemetry/auto-instrumentations-node');
const { registerInstrumentations } = require('@opentelemetry/instrumentation');
const { detectResourcesSync } = require('@opentelemetry/resources');
Expand All @@ -17,46 +15,8 @@ tracerProvider.register();
const loggerProvider = new LoggerProvider({ resource });
loggerProvider.addLogRecordProcessor(new SimpleLogRecordProcessor(new AzureMonitorLogExporter()));

registerInstrumentations({ tracerProvider, loggerProvider, instrumentations: [getNodeAutoInstrumentations()] });

// NOTE: The below code will soon be a part of a new package `@opentelemetry/instrumentation-azure-functions`
// See here for more info: https://github.com/Azure/azure-functions-nodejs-library/issues/245
app.setup({ capabilities: { WorkerOpenTelemetryEnabled: true } });

const logger = loggerProvider.getLogger('default');
app.hook.log((context) => {
logger.emit({
body: context.message,
severityNumber: toOtelSeverityNumber(context.level),
severityText: context.level,
});
registerInstrumentations({
tracerProvider,
loggerProvider,
instrumentations: [getNodeAutoInstrumentations(), new AzureFunctionsInstrumentation()],
});

app.hook.preInvocation((context) => {
context.functionHandler = otelContext.bind(
propagation.extract(otelContext.active(), {
traceparent: context.invocationContext.traceContext.traceParent,
tracestate: context.invocationContext.traceContext.traceState,
}),
context.functionHandler
);
});

function toOtelSeverityNumber(level) {
switch (level) {
case 'information':
return SeverityNumber.INFO;
case 'debug':
return SeverityNumber.DEBUG;
case 'error':
return SeverityNumber.ERROR;
case 'trace':
return SeverityNumber.TRACE;
case 'warning':
return SeverityNumber.WARN;
case 'critical':
return SeverityNumber.FATAL;
default:
return SeverityNumber.UNSPECIFIED;
}
}
50 changes: 5 additions & 45 deletions js/src/otelOtlp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
const { app } = require('@azure/functions');
const { context: otelContext, propagation } = require('@opentelemetry/api');
const { SeverityNumber } = require('@opentelemetry/api-logs');
const { AzureFunctionsInstrumentation } = require('@azure/functions-opentelemetry-instrumentation');
const { getNodeAutoInstrumentations, getResourceDetectors } = require('@opentelemetry/auto-instrumentations-node');
const { OTLPLogExporter } = require('@opentelemetry/exporter-logs-otlp-http');
const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-http');
Expand All @@ -18,46 +16,8 @@ tracerProvider.register();
const loggerProvider = new LoggerProvider({ resource });
loggerProvider.addLogRecordProcessor(new SimpleLogRecordProcessor(new OTLPLogExporter()));

registerInstrumentations({ tracerProvider, loggerProvider, instrumentations: [getNodeAutoInstrumentations()] });

// NOTE: The below code will soon be a part of a new package `@opentelemetry/instrumentation-azure-functions`
// See here for more info: https://github.com/Azure/azure-functions-nodejs-library/issues/245
app.setup({ capabilities: { WorkerOpenTelemetryEnabled: true } });

const logger = loggerProvider.getLogger('default');
app.hook.log((context) => {
logger.emit({
body: context.message,
severityNumber: toOtelSeverityNumber(context.level),
severityText: context.level,
});
registerInstrumentations({
tracerProvider,
loggerProvider,
instrumentations: [getNodeAutoInstrumentations(), new AzureFunctionsInstrumentation()],
});

app.hook.preInvocation((context) => {
context.functionHandler = otelContext.bind(
propagation.extract(otelContext.active(), {
traceparent: context.invocationContext.traceContext.traceParent,
tracestate: context.invocationContext.traceContext.traceState,
}),
context.functionHandler
);
});

function toOtelSeverityNumber(level) {
switch (level) {
case 'information':
return SeverityNumber.INFO;
case 'debug':
return SeverityNumber.DEBUG;
case 'error':
return SeverityNumber.ERROR;
case 'trace':
return SeverityNumber.TRACE;
case 'warning':
return SeverityNumber.WARN;
case 'critical':
return SeverityNumber.FATAL;
default:
return SeverityNumber.UNSPECIFIED;
}
}
75 changes: 75 additions & 0 deletions ts/package-lock.json

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

1 change: 1 addition & 0 deletions ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
},
"dependencies": {
"@azure/functions": "^4.5.0",
"@azure/functions-opentelemetry-instrumentation": "^0.1.0",
"@azure/monitor-opentelemetry-exporter": "^1.0.0-beta.23",
"@opentelemetry/api": "^1.8.0",
"@opentelemetry/auto-instrumentations-node": "^0.46.1",
Expand Down
50 changes: 5 additions & 45 deletions ts/src/otelAppInsights.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { app, LogLevel } from '@azure/functions';
import { AzureFunctionsInstrumentation } from '@azure/functions-opentelemetry-instrumentation';
import { AzureMonitorLogExporter, AzureMonitorTraceExporter } from '@azure/monitor-opentelemetry-exporter';
import { context as otelContext, propagation } from '@opentelemetry/api';
import { SeverityNumber } from '@opentelemetry/api-logs';
import { getNodeAutoInstrumentations, getResourceDetectors } from '@opentelemetry/auto-instrumentations-node';
import { registerInstrumentations } from '@opentelemetry/instrumentation';
import { detectResourcesSync } from '@opentelemetry/resources';
Expand All @@ -17,46 +15,8 @@ tracerProvider.register();
const loggerProvider = new LoggerProvider({ resource });
loggerProvider.addLogRecordProcessor(new SimpleLogRecordProcessor(new AzureMonitorLogExporter()));

registerInstrumentations({ tracerProvider, loggerProvider, instrumentations: [getNodeAutoInstrumentations()] });

// NOTE: The below code will soon be a part of a new package `@opentelemetry/instrumentation-azure-functions`
// See here for more info: https://github.com/Azure/azure-functions-nodejs-library/issues/245
app.setup({ capabilities: { WorkerOpenTelemetryEnabled: true } });

const logger = loggerProvider.getLogger('default');
app.hook.log((context) => {
logger.emit({
body: context.message,
severityNumber: toOtelSeverityNumber(context.level),
severityText: context.level,
});
registerInstrumentations({
tracerProvider,
loggerProvider,
instrumentations: [getNodeAutoInstrumentations(), new AzureFunctionsInstrumentation()],
});

app.hook.preInvocation((context) => {
context.functionHandler = otelContext.bind(
propagation.extract(otelContext.active(), {
traceparent: context.invocationContext.traceContext.traceParent,
tracestate: context.invocationContext.traceContext.traceState,
}),
context.functionHandler
);
});

function toOtelSeverityNumber(level: LogLevel): SeverityNumber {
switch (level) {
case 'information':
return SeverityNumber.INFO;
case 'debug':
return SeverityNumber.DEBUG;
case 'error':
return SeverityNumber.ERROR;
case 'trace':
return SeverityNumber.TRACE;
case 'warning':
return SeverityNumber.WARN;
case 'critical':
return SeverityNumber.FATAL;
default:
return SeverityNumber.UNSPECIFIED;
}
}
Loading

0 comments on commit cce03ca

Please sign in to comment.