The aws-sdk-go-metrics module injects an instrumented http client into your AWS SDK Go client to track AWS API calls with status codes and latency. The module supports both the aws-sdk-go and aws-sdk-go-v2.
registry := prometheus.NewRegistry()
// Create an instrumented AWS SDK session with a shared config
sess, err := awsmetrics.Instrument(session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
})), registry)
if err != nil {
log.Fatalf("Unable to create instrumented aws sdk session for the aws-sdk-go v1: %v", err)
}
http.Handle("/metrics", promhttp.HandlerFor(
registry,
promhttp.HandlerOpts{EnableOpenMetrics: false},
))
http.ListenAndServe(":2112", nil)
registry := prometheus.NewRegistry()
// Create an instrumented AWS SDK v2 config
cfg, err := config.LoadDefaultConfig(context.TODO(), awsmetricsv2.WithInstrumentedClients(registry))
if err != nil {
log.Fatal(err)
}
http.Handle("/metrics", promhttp.HandlerFor(
registry,
promhttp.HandlerOpts{EnableOpenMetrics: false},
))
http.ListenAndServe(":2112", nil)
Both examples above produce the same prometheus metrics format as shown below. These metrics were generated by running the demo application in cmd/v1/main.go
and cmd/v2/main.go
which calls S3 ListBuckets, EC2 DescribeInstances, and EKS DescribeAddonVersions. Some of the values duplicated in latency buckets are ommited to be more readable.
> curl 127.0.0.1:2112/metrics
# HELP aws_sdk_go_request_latency Latency of AWS SDK Go requests
# TYPE aws_sdk_go_request_latency histogram
... truncated ...
aws_sdk_go_request_latency_bucket{action="/",service="s3",status_code="200",le="70"} 3
aws_sdk_go_request_latency_bucket{action="/",service="s3",status_code="200",le="80"} 3
aws_sdk_go_request_latency_bucket{action="/",service="s3",status_code="200",le="90"} 3
aws_sdk_go_request_latency_bucket{action="/",service="s3",status_code="200",le="100"} 3
aws_sdk_go_request_latency_bucket{action="/",service="s3",status_code="200",le="125"} 3
aws_sdk_go_request_latency_bucket{action="/",service="s3",status_code="200",le="150"} 3
aws_sdk_go_request_latency_bucket{action="/",service="s3",status_code="200",le="175"} 3
aws_sdk_go_request_latency_bucket{action="/",service="s3",status_code="200",le="200"} 3
aws_sdk_go_request_latency_bucket{action="/",service="s3",status_code="200",le="225"} 3
aws_sdk_go_request_latency_bucket{action="/",service="s3",status_code="200",le="250"} 3
aws_sdk_go_request_latency_bucket{action="/",service="s3",status_code="200",le="275"} 3
aws_sdk_go_request_latency_bucket{action="/",service="s3",status_code="200",le="300"} 3
aws_sdk_go_request_latency_bucket{action="/",service="s3",status_code="200",le="400"} 4
... truncated ...
aws_sdk_go_request_latency_bucket{action="/",service="s3",status_code="200",le="+Inf"} 4
aws_sdk_go_request_latency_count{action="/",service="s3",status_code="200"} 4
... truncated ...
aws_sdk_go_request_latency_bucket{action="/addons/supported-versions",service="eks",status_code="200",le="500"} 1
aws_sdk_go_request_latency_bucket{action="/addons/supported-versions",service="eks",status_code="200",le="600"} 2
aws_sdk_go_request_latency_bucket{action="/addons/supported-versions",service="eks",status_code="200",le="700"} 3
... truncated ...
aws_sdk_go_request_latency_bucket{action="/addons/supported-versions",service="eks",status_code="200",le="+Inf"} 3
aws_sdk_go_request_latency_count{action="/addons/supported-versions",service="eks",status_code="200"} 3
... truncated ...
aws_sdk_go_request_latency_bucket{action="DescribeInstances",service="ec2",status_code="200",le="125"} 2
aws_sdk_go_request_latency_bucket{action="DescribeInstances",service="ec2",status_code="200",le="150"} 2
aws_sdk_go_request_latency_bucket{action="DescribeInstances",service="ec2",status_code="200",le="175"} 2
aws_sdk_go_request_latency_bucket{action="DescribeInstances",service="ec2",status_code="200",le="200"} 2
aws_sdk_go_request_latency_bucket{action="DescribeInstances",service="ec2",status_code="200",le="225"} 2
aws_sdk_go_request_latency_bucket{action="DescribeInstances",service="ec2",status_code="200",le="250"} 2
aws_sdk_go_request_latency_bucket{action="DescribeInstances",service="ec2",status_code="200",le="275"} 2
aws_sdk_go_request_latency_bucket{action="DescribeInstances",service="ec2",status_code="200",le="300"} 2
aws_sdk_go_request_latency_bucket{action="DescribeInstances",service="ec2",status_code="200",le="400"} 2
aws_sdk_go_request_latency_bucket{action="DescribeInstances",service="ec2",status_code="200",le="500"} 3
... truncated ...
aws_sdk_go_request_latency_bucket{action="DescribeInstances",service="ec2",status_code="200",le="+Inf"} 3
aws_sdk_go_request_latency_count{action="DescribeInstances",service="ec2",status_code="200"} 3
# HELP aws_sdk_go_requests The total number of AWS SDK Go requests
# TYPE aws_sdk_go_requests counter
aws_sdk_go_requests{action="/",service="s3",status_code="200"} 4
aws_sdk_go_requests{action="/addons/supported-versions",service="eks",status_code="200"} 3
aws_sdk_go_requests{action="DescribeInstances",service="ec2",status_code="200"} 3