cwpagedmetricput

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 28, 2019 License: Apache-2.0 Imports: 11 Imported by: 2

README

cwpagedmetricput

Build Status GoDoc Coverage Status

cwpagedmetricput allows you to send metrics to cloudwatch without worrying about how many metrics you can reference in a single request or how to compress or split the metrics when they are too big.

It exposes an interface that matches the cloudwatch PutMetricData interface from aws-sdk-go. The idea is that you can call PutMetricData, but can pass as large or as many datum as you want.

Rules checked

  • Splits MetricDatum into buckets if there are too many Datum
  • Splits large Values arrays from single MetricDatum into multiple Datum
  • Splits large HTTP request bodies
  • gzip encodes request bodies
  • Optional filtering of valid CloudWatch units

Example

func ExamplePager_PutMetricData() {
	a := cwpagedmetricput.Pager {
		Client: cloudwatch.New(session.Must(session.NewSession(&aws.Config{
			Region: aws.String("us-west-2"),
		}))),
	}
	_, err := a.PutMetricData(&cloudwatch.PutMetricDataInput{
		Namespace: aws.String("custom"),
		MetricData: []*cloudwatch.MetricDatum {
			{
				MetricName: aws.String("custom metric"),
			},
		},
	})
	if err != nil {
		// You'll need valid AWS credentials
		fmt.Println("error result")
	} else {
		fmt.Println("result")
	}
	// Output: error result
}

Contributing

Make sure your tests pass CI/CD pipeline which includes running make fix lint test locally. You'll need an AWS account to verify integration tests, which should also pass make integration_test. I recommend opening a github issue to discuss your ideas before contributing code.

Documentation

Overview

Package cwpagedmetricput allows mass PutMetricData calls into CloudWatch metrics with an API that matches CloudWatch's aws-sdk-go client. It does this by bucketing Datum into sizes allowed by AWS's API. It also takes cost saving measures by gzip sending large datum sends.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CloudWatchClient

type CloudWatchClient interface {
	// PutMetricDataWithContext should match the contract of cloudwatch.CloudWatch.PutMetricDataWithContext
	PutMetricDataWithContext(aws.Context, *cloudwatch.PutMetricDataInput, ...request.Option) (*cloudwatch.PutMetricDataOutput, error)
}

CloudWatchClient is anything that can receive CloudWatch metrics as documented by CloudWatch's public API constraints.

type Config

type Config struct {
	// True will empty out the "unit" field of datum that have a unit not explicitly documented at
	// https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_MetricDatum.html
	ClearInvalidUnits bool
	// True will *not* use goroutines to send all the batches at once and will send the batches serially after they are
	// created
	SerialSends bool
	// Callback executed when weird datum or RPC calls force us to drop some of the datum from a request we've had to
	// split.
	OnDroppedDatum func(datum *cloudwatch.MetricDatum)
}

Config controls optional parameters of Pager. The zero value is a reasonable default.

type Pager

type Pager struct {
	// Client is required and is usually an instance of *cloudwatch.CloudWatch
	Client CloudWatchClient
	// Config is optional and controls how data is filtered or aggregated
	Config Config
}

Pager behaves like CloudWatch's MetricData API, but takes care of all of the smaller parts for you around how to correctly bucket and split MetricDatum. Pager is as thread safe as the Client parameter. If you're using *cloudwatch.CloudWatch as your Client, then it will be thread safe.

func (*Pager) PutMetricData

PutMetricData should be a drop in replacement for *cloudwatch.CloudWatch.PutMetricData, but taking care of splitting datum that are too large. Note: More difficult to support PutMetricDataRequest since it is not one request.Request, but many.

Example
package main

import (
	"fmt"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/cloudwatch"
	"github.com/cep21/cwpagedmetricput"
)

func main() {
	a := cwpagedmetricput.Pager{
		Client: cloudwatch.New(session.Must(session.NewSession(&aws.Config{
			Region: aws.String("us-west-2"),
		}))),
	}
	_, err := a.PutMetricData(&cloudwatch.PutMetricDataInput{
		Namespace: aws.String("custom"),
		MetricData: []*cloudwatch.MetricDatum{
			{
				MetricName: aws.String("custom metric"),
			},
		},
	})
	if err != nil {
		// You'll need valid AWS credentials
		fmt.Println("error result")
	} else {
		fmt.Println("result")
	}
}
Output:

error result

func (*Pager) PutMetricDataWithContext

func (c *Pager) PutMetricDataWithContext(ctx aws.Context, input *cloudwatch.PutMetricDataInput, reqs ...request.Option) (*cloudwatch.PutMetricDataOutput, error)

PutMetricDataWithContext should be a drop in replacement for *cloudwatch.CloudWatch.PutMetricDataWithContext, but taking care of splitting datum that are too large.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL