Remote logging

SDKs send logs to Hypertune Edge in the background. This allows you to:

Manually flush logs

You can manually trigger and wait for logs to be flushed with the flushLogs method:

import { NextFetchEvent, NextRequest } from "next/server";
import getHypertune from "./lib/getHypertune";

export async function middleware(
  req: NextRequest,
  context: NextFetchEvent
): Promise<void> {
  const hypertune = await getHypertune();

  const exampleFlag = hypertune.exampleFlag({ fallback: false });
  console.log("Middleware Example Flag:", exampleFlag);

  context.waitUntil(hypertune.flushLogs());
}

This is a requirement in serverless and edge environments like Vercel deployments, Cloudflare Workers, AWS Lambdas, etc, where background SDK tasks like flushing logs aren't guaranteed to execute.

Configuring log flushing

Flush interval

By default, SDKs flush logs to Hypertune Edge every 2 seconds. You can adjust this interval by setting the remoteLogging.flushIntervalMs option. For example, to flush logs every 10 seconds, set this option to 10_000. Setting the value to null disables automatic log flushing.

Logging mode

The logging mode determines which SDK log messages, expression evaluations, A/B test exposures, and analytics events are sent to the remote server. You can control this behaviour using the remoteLogging.mode initialization option:

ModeBehaviour

Normal

All data is sent to the remote server.

Session

SDK log messages, expression evaluations, and A/B test exposures are deduplicated per session, based on the provided context. However, all analytics events are still sent to the remote server.

Off

No data is sent, making this mode ideal for local development or testing environments.

Custom logging endpoint

By default, SDKs send all log data to Hypertune servers via the https://gcp.fasthorse.workers.dev/logs endpoint. If you're experiencing issues with ad blockers and want to ensure reliable data transmission, you can proxy these requests through your own server. To do this, set the remoteLogging.endpointUrl option to an endpoint in your backend and then forward the requests to the default Hypertune endpoint from there using tools like Next.js redirects or a tool like http-proxy-middleware.

If you'd rather store and process Hypertune data within your own system, you can implement this behaviour in your custom remote logging endpoint. To help you do this we've published the OpenAPI contract for the logging endpoint here. You can use the contract to better understand the payload format the endpoint uses and to generate types in your language of choice.

Last updated