Data & Security

Azure Event Hubs Request Logger Plugin

This plugin pushes request/response logs to Azure Event Hubs. This can be used to stream the request data generated by your API Gateway to use for monitoring, analytics, auditing, or debugging purposes.

Enterprise Feature

Custom logging is available as an add-on as part of an enterprise plan. If you would like to purchase this feature, please contact us at sales@zuplo.com or reach out to your account manager.

Most enterprise features can be used in a trial mode for a limited time. Feel free to use enterprise features for development and testing purposes.

Setup

You can define the fields created in the JSON object by creating a custom type in TypeScript and a function to extract the field data from the Response, ZuploRequest, and ZuploContext.

The plugin is configured in the Runtime Extensions file zuplo.runtime.ts: git sta

modules/zuplo.runtime.ts
// The interface that describes the rows
// in the output
interface LogEntry {
  timestamp: string;
  method: string;
  url: string;
  status: number;
  statusText: string;
  sub: string | null;
  contentLength: string | null;
}

// Add the plugin
runtime.addPlugin(
  new AzureEventHubsRequestLoggerPlugin<LogEntry>({
    connectionString: environment.AZURE_EVENT_HUBS_CONNECTION_STRING,
    // e.g. "Endpoint=sb://your-namespace.servicebus.windows.net/;SharedAccessKeyName=key-name;SharedAccessKey=YOUR_SHARED_ACCESS_KEY"
    batchPeriodSeconds: 1,
    entityPath: "your-event-hub-name",
    generateLogEntry: (response: Response, request: ZuploRequest) => ({
      // You can customize the log entry here by adding new fields
      timestamp: new Date().toISOString(),
      url: request.url,
      method: request.method,
      status: response.status,
      statusText: response.statusText,
      sub: request.user?.sub ?? null,
      contentLength: request.headers.get("content-length"),
    }),
  }),
);
ts

The configuration requires a connectionString which you can get from the Azure portal "Shared access policies" section in Event Hubs. If the connection string contains an EntityPath property the separate entityPath option to define the event hub's name is not required.

Entries will be batched and sent as an array, they will be sent every batchPeriodSeconds. If not specified it will be sent very frequently (~every 10ms) to avoid data loss. Note that batchPeriodSeconds can be specified as a fraction, e.g. 0.1 for every 100ms.