Capture beacon data from the browser, divert beacon request payloads to a log endpoint, and avoid putting load on your own infrastructure.
Use this starter
Using the Fastly CLI, create a new project using this starter somewhere on your computer:
$ fastly compute init --from=https://github.com/fastly/compute-starter-kit-rust-beacon-termination
Or click the button below to create a GitHub repository, provision a Fastly service, and set up continuous deployment:
What are beacons?
Beacons are HTTP requests, usually
POSTs, sent from a web browser to record some analytics data. Browsers offer native support for beacons via the
navigator.sendBeacon method, and via the Reporting API for out-of-band reports on browser-generated warnings like CSP and feature policy violations, deprecations, browser interventions, and network errors. Native apps will also often send beacon data back to base.
For an in-depth guide to beacon termination using VCL, see the Beacon Termination tutorial.
- Exposes a
POST /reportsendpoint to receive beacon reports (in batches)
- Deserializes individual reports from JSON to Rust data structures, with optional type-checking (see Payload examples)
- Enriches the data with information available at the edge, e.g. by adding geo data
- Sends reports to a logging endpoint as individual JSON lines N.B.: Depending on which logging endpoint type is chosen, these lines may be batched.
- Responds with a synthetic 204
This starter kit allows an individual report to be any valid JSON value.
For optional type-checking, it also includes the data structures for some common report payloads. These structures can be imported from modules following the
example_... naming convention:
- CSP Violationsmod example_csp_violation;use crate::example_csp_violation::ReportBody;
- Network Errorsmod example_network_error_log;use crate::example_network_error_log::ReportBody;
The following resources need to exist on your active Fastly service version for this starter kit to work:
- A logging endpoint called
Starters are a good way to bootstrap a project. For more specific use cases, and answers to common problems, try our library of code examples.