Skip to content

Commit

Permalink
chore: Add SpanEnrichingProcessor for Azure Monitor integration
Browse files Browse the repository at this point in the history
  • Loading branch information
saoc90 committed Jun 20, 2024
1 parent b8d7aca commit 768a999
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/instrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export function register() {
if (process.env.NEXT_RUNTIME === 'nodejs') {
const { useAzureMonitor } = require("@azure/monitor-opentelemetry");
const { metrics } = require('@opentelemetry/api');
const { SpanEnrichingProcessor } = require('./span-enriching-processor');

const cosmosdb = new URL(process.env.AZURE_COSMOSDB_URI);
const cosmosdbHost = cosmosdb.hostname;
Expand All @@ -30,6 +31,7 @@ export function register() {
};

useAzureMonitor({
spanProcessors: [new SpanEnrichingProcessor()] ,
azureMonitorExporterOptions: {
connectionString: process.env.APPLICATIONINSIGHTS_CONNECTION_STRING || "",

Expand Down
2 changes: 1 addition & 1 deletion src/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const nextConfig = {
output: "standalone",
experimental: {
serverComponentsExternalPackages: ["@azure/storage-blob", "@azure/monitor-opentelemetry", "@opentelemetry/api", "@opentelemetry/instrumentation"],
serverComponentsExternalPackages: ["@azure/storage-blob", "@azure/monitor-opentelemetry", "@opentelemetry/api", "@opentelemetry/instrumentation", "@opentelemetry/sdk-trace-base"],
instrumentationHook: true,
}
};
Expand Down
23 changes: 23 additions & 0 deletions src/span-enriching-processor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Import the necessary packages.
import { SpanKind, TraceFlags } from "@opentelemetry/api";
import { Span, SpanProcessor } from "@opentelemetry/sdk-trace-base";

// Create a new SpanEnrichingProcessor class.
export class SpanEnrichingProcessor implements SpanProcessor {
forceFlush(): Promise<void> {
return Promise.resolve();
}

shutdown(): Promise<void> {
return Promise.resolve();
}

onStart(_span: Span): void {}

onEnd(span: Span) {
// If the span is an internal span, set the trace flags to NONE to prevent it from being collected.
if(span.kind == SpanKind.INTERNAL){
span.spanContext().traceFlags = TraceFlags.NONE;
}
}
}

0 comments on commit 768a999

Please sign in to comment.