Skip to content

Instantly share code, notes, and snippets.

@yoshixmk
Created August 18, 2023 06:30
Show Gist options
  • Save yoshixmk/d2b7b4237e9749bd2727593e81176f1a to your computer and use it in GitHub Desktop.
Save yoshixmk/d2b7b4237e9749bd2727593e81176f1a to your computer and use it in GitHub Desktop.
Export to datadog-agent as span which service name is luna using Deno
#!/bin/bash
OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318 deno run -A sample.ts
// Datadog
import * as opentelemetry from "npm:@opentelemetry/[email protected]"
import {
SpanKind,
} from "npm:@opentelemetry/[email protected]"
import type {
Context,
Span,
SpanOptions,
} from "npm:@opentelemetry/[email protected]"
import { Resource } from "npm:@opentelemetry/[email protected]"
import {
ConsoleSpanExporter,
SimpleSpanProcessor,
} from "npm:@opentelemetry/[email protected]"
import {
NodeTracerProvider,
NoopSpanProcessor,
} from "npm:@opentelemetry/[email protected]"
import { registerInstrumentations } from "npm:@opentelemetry/[email protected]"
import { HttpInstrumentation } from "npm:@opentelemetry/[email protected]"
import { FetchInstrumentation } from "npm:@opentelemetry/[email protected]"
import { ExpressInstrumentation } from "npm:@opentelemetry/[email protected]"
import { GraphQLInstrumentation } from "npm:@opentelemetry/[email protected]"
import { OTLPTraceExporter } from "npm:@opentelemetry/[email protected]"
import { SocketIoInstrumentation } from "npm:@opentelemetry/[email protected]"
import { NetInstrumentation } from "npm:@opentelemetry/[email protected]"
// Monkeypatching to get past FetchInstrumentation's dependence on sdk-trace-web, which depends on some browser-only constructs
Object.assign(globalThis, { location: {} }) // released but doesn't work https://github.com/open-telemetry/opentelemetry-js/blame/6d13eb437932e46e021c840ac5d327d556eb3c52/packages/opentelemetry-sdk-trace-web/src/utils.ts#L315-L319
const OTEL_SDK_DISABLED = false // omit impl: set each of enviromemt
const environment = "LOCAL" // omit impl: show env name from env var
// Initialize provider and identify this particular service
// (in this case, we're implementing a federated gateway)
const provider = new NodeTracerProvider({
resource: Resource.default().merge(
new Resource({
"service.name": "luna",
"process.runtime.name": "deno",
"service.instance.id": Deno.hostname(),
"process.pid": Deno.pid,
"deployment.environment": environment
}),
),
})
// Workaround: Set NoopSpanProcesor to get the correct behavior.
// https://opentelemetry.io/docs/specs/otel/configuration/sdk-environment-variables/#general-sdk-configuration
if (OTEL_SDK_DISABLED) {
provider.addSpanProcessor(new NoopSpanProcessor())
} else {
provider.addSpanProcessor(new SimpleSpanProcessor(new OTLPTraceExporter()))
if (environment === "LOCAL") {
provider.addSpanProcessor(
new SimpleSpanProcessor(new ConsoleSpanExporter()),
)
}
}
// Register the provider to begin tracing
provider.register()
// Register server-related instrumentation
registerInstrumentations({
tracerProvider: provider,
// Sorry, there are not active instrumentations...
instrumentations: [
new GraphQLInstrumentation(),
new HttpInstrumentation(),
new ExpressInstrumentation(),
new FetchInstrumentation(),
new SocketIoInstrumentation(),
new NetInstrumentation(),
],
})
const tracer = opentelemetry.trace.getTracer("luna-tracer-main")
const context = opentelemetry.context.active()
export const startSpan = (
spanName = "doWork",
options: SpanOptions = {},
ctx = context,
): Span =>
tracer.startSpan(spanName, { kind: SpanKind.SERVER, ...options }, ctx)
const setSpan = (parent: Span): Context =>
opentelemetry.trace.setSpan(
opentelemetry.context.active(),
parent,
)
export const startSpanWith = (
parent: Span,
spanName = "doWork",
options: SpanOptions = {},
): Span => startSpan(spanName, options, setSpan(parent))
// ----
const parentSpan: Span = startSpan("graphql request")
const child = startSpanWith(parentSpan)
// You do something here
await new Promise(resolve => setTimeout(resolve, 5000));
child.end()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment