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.
npminstallopentelemetry-sdk-workersConfigure 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';
exportinterfaceEnv {
OTEL_EXPORTER_LOGS_ENABLED:string;
OTEL_EXPORTER_OTLP_ENDPOINT:string;
OTEL_EXPORTER_OTLP_HEADERS:string;
OTEL_SERVICE_NAME:string;
}
exportdefault {
asyncfetch(request:Request, env:any, ctx:ExecutionContext) {
constsdk=WorkersSDK.fromEnv(request, env, ctx);
constts=Date.now();
try {
sdk.logger.info(
JSON.stringify({
message:'Before fetch',
}),
);
awaitsdk.fetch('https://httpbin.org/headers/');
sdk.logger.info(
JSON.stringify({
message:'After fetch',
took:Date.now() - ts,
}),
);
returnsdk.sendResponse(
newResponse(
JSON.stringify({
message:'Hello, world!',
}),
{
headers: {
'content-type':'application/json;charset=UTF-8',
},
},
),
);
} catch (ex:any) {
sdk.captureException(ex);
}
},
};