Skip to content
workers-go
Esc
navigateopen⌘Jpreview
On this page

Analytics Engine

Write data points to a Workers Analytics Engine dataset from Go.

Workers Analytics Engine is an analytics and logging datastore: a Worker writes data points to a bound dataset, and you query them later with the SQL API. The exp/cloudflare/analytics package wraps the bound dataset.

Declare the binding

[[analytics_engine_datasets]]
binding = "DATASET"
dataset = "my_dataset"

The dataset is created automatically the first time you write to it.

Resolve the binding

analytics.NewAnalyticsEngineDataset resolves the binding by name and returns an error when no binding with that name exists.

import "github.com/syumai/workers-go/exp/cloudflare/analytics"

dataset, err := analytics.NewAnalyticsEngineDataset("DATASET")
if err != nil {
	// no binding named DATASET
}

Write a data point

WriteDataPoint writes a single AnalyticsEngineDataPoint:

  • Indexes — index strings used to sample the data point.
  • Doubles — numeric columns, written as double1, double2, …
  • Blobs — string columns, written as blob1, blob2, …
err := dataset.WriteDataPoint(analytics.AnalyticsEngineDataPoint{
	Indexes: []string{req.URL.Hostname()},
	Blobs:   []string{req.Method, req.URL.Path},
	Doubles: []float64{time.Since(start).Milliseconds()},
})
if err != nil {
	return err
}

Query the dataset later through the Analytics Engine SQL API.

Was this page helpful?