Observability

Traces

Send OpenTelemetry traces to OpsCompanion over OTLP/HTTP JSON.

Get Your API Key

In the OpsCompanion app, click your profile picture in the top right of the navbar, click Manage Account, then open the API Keys tab at the top.

Choose A Method

If you need help creating instrumentation.ts in Next.js, follow the official Next.js instrumentation guide.

Send a trace from server-only code in a route handler or server action.

.env.local

OPSCOMPANION_API_KEY=your_api_key_here

app/api/checkout/route.ts

import { randomBytes } from "node:crypto"

function hex(bytes: number) {
  return randomBytes(bytes).toString("hex")
}

export async function POST() {
  const startTimeUnixNano = (BigInt(Date.now()) * 1000000n).toString()
  const endTimeUnixNano = (BigInt(Date.now() + 42) * 1000000n).toString()

  await fetch("https://otel.opscompanion.ai/v1/traces", {
    method: "POST",
    headers: {
      Authorization: `Bearer ${process.env.OPSCOMPANION_API_KEY}`,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      resourceSpans: [
        {
          resource: {
            attributes: [
              {
                key: "service.name",
                value: { stringValue: "my-nextjs-app" },
              },
              {
                key: "deployment.environment",
                value: {
                  stringValue: process.env.VERCEL_ENV ?? "local",
                },
              },
            ],
          },
          scopeSpans: [
            {
              scope: { name: "checkout" },
              spans: [
                {
                  traceId: hex(16),
                  spanId: hex(8),
                  name: "POST /api/checkout",
                  kind: 2,
                  startTimeUnixNano,
                  endTimeUnixNano,
                  attributes: [
                    {
                      key: "http.method",
                      value: { stringValue: "POST" },
                    },
                    {
                      key: "http.route",
                      value: { stringValue: "/api/checkout" },
                    },
                  ],
                  status: { code: 1 },
                },
              ],
            },
          ],
        },
      ],
    }),
  })

  return Response.json({ ok: true })
}

Success Response

Successful requests return an OTLP-style partialSuccess object.

{
  "partialSuccess": {
    "rejectedSpans": 0,
    "errorMessage": ""
  }
}

On this page