Skip to content

go-aws-emf

Go library for generating AWS CloudWatch Embedded Metric Format (EMF) JSON logs. Publish custom CloudWatch metrics directly from structured logs — no CloudWatch SDK or agent required.

Why use it

CloudWatch EMF lets you embed custom metrics inside log events. Instead of making separate PutMetricData API calls, you write a specially formatted JSON log line and CloudWatch extracts the metrics automatically. This library handles the EMF JSON structure so you don’t have to.

  • Zero external dependencies (stdlib only)
  • Full validation against the AWS EMF schema
  • Multiple dimensions and metrics per log entry
  • High-resolution metrics (1-second resolution) support

Quick start

go get github.com/zlatkoc/go-aws-emf/pkg/emf

Basic usage

metricLog := emf.NewMetricLog("MyApplicationMetrics")

metricLog.PutDimension("ServiceName", "UserService")
metricLog.PutDimension("Environment", "Production")

metricLog.WithDimensionSet([]string{"ServiceName"})
metricLog.WithDimensionSet([]string{"ServiceName", "Environment"})

metricLog.PutMetric("Latency", 42.0, emf.UnitMilliseconds)
metricLog.PutMetric("RequestCount", 1, emf.UnitCount)

fmt.Println(metricLog.String())

Builder pattern

metricLog := emf.NewMetricLog("MyApplicationMetrics")

metricLog.Builder().
    Dimension("ServiceName", "UserService").
    Dimension("Environment", "Production").
    DimensionSet([]string{"ServiceName"}).
    DimensionSet([]string{"ServiceName", "Environment"}).
    Metric("Latency", 42.0, emf.UnitMilliseconds).
    Metric("RequestCount", 1, emf.UnitCount).
    Property("RequestId", "12345").
    Build()

fmt.Println(metricLog.String())