Docs
Install
Cloudflare Workers

Cloudflare Workers

HyperDX uses the OpenTelemetry standard for collecting telemetry data (logs and traces).

This Guide Integrates:

✅ Logs✖️ Metrics️✅ Traces

Getting Started

Install Cloudflare Workers OpenTelemetry SDK

Use the following command to install the SDK.

npm install opentelemetry-sdk-workers

Configure Environment Variables

Afterwards you'll need to add the following environment variables to your wrangler.toml to ship telemetry to HyperDX:

[vars]
OTEL_SERVICE_NAME = "<NAME_OF_YOUR_APP_OR_SERVICE>"
OTEL_EXPORTER_OTLP_ENDPOINT = "https://in-otel.hyperdx.io"
OTEL_EXPORTER_OTLP_HEADERS = "authorization=<YOUR_HYPERDX_API_KEY>"
OTEL_EXPORTER_LOGS_ENABLED = "true"

Send Telemetry Data With Cloudflare Workers OpenTelemetry SDK

To send telemetry data to HyperDX, you can use the APIs provided by the SDK, such as logger, sendResponse, fetch, and captureException, etc. Here's an example code snippet using these APIs:

/* Required to patch missing performance API in Cloudflare Workers. */
import 'opentelemetry-sdk-workers/performance';
import { WorkersSDK } from 'opentelemetry-sdk-workers';
 
export interface Env {
  OTEL_EXPORTER_LOGS_ENABLED: string;
  OTEL_EXPORTER_OTLP_ENDPOINT: string;
  OTEL_EXPORTER_OTLP_HEADERS: string;
  OTEL_SERVICE_NAME: string;
}
 
export default {
  async fetch(request: Request, env: any, ctx: ExecutionContext) {
    const sdk = WorkersSDK.fromEnv(request, env, ctx);
    const ts = Date.now();
 
    try {
      sdk.logger.info(
        JSON.stringify({
          message: 'Before fetch',
        }),
      );
      await sdk.fetch('https://httpbin.org/headers/');
      sdk.logger.info(
        JSON.stringify({
          message: 'After fetch',
          took: Date.now() - ts,
        }),
      );
      return sdk.sendResponse(
        new Response(
          JSON.stringify({
            message: 'Hello, world!',
          }),
          {
            headers: {
              'content-type': 'application/json;charset=UTF-8',
            },
          },
        ),
      );
    } catch (ex: any) {
      sdk.captureException(ex);
    }
  },
};

Hi, how can I help you?