Documentation ¶
Overview ¶
Package cwatsch enables sending cloudwatch metrics in batches.
Name of the package is designed to sound similar to "Quatsch" - a german word for "talk nonsense". Constant chatter a child makes at the dinner table is a good example of Quatsch.
The goal of the package is to reduce aws costs spent on sending custom metrics.
Index ¶
- func NewTicker(ctx context.Context, interval time.Duration, fn func())
- type Batch
- func (b *Batch) Add(namespace string, data ...*cw.MetricDatum) *Batch
- func (b *Batch) AddInputs(inputs ...*cw.PutMetricDataInput) *Batch
- func (b *Batch) Flush() error
- func (b *Batch) FlushCompleteBatches() error
- func (b *Batch) FlushCompleteBatchesCtx(ctx context.Context) error
- func (b *Batch) FlushCtx(ctx context.Context) error
- func (b *Batch) LaunchAutoFlush(ctx context.Context, interval time.Duration, onError func(error))
- func (b *Batch) PutMetricData(input *cw.PutMetricDataInput) (*cw.PutMetricDataOutput, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Batch ¶
func New ¶
func New(cwAPI cloudwatchiface.CloudWatchAPI) *Batch
func (*Batch) FlushCompleteBatches ¶
FlushCompleteBatches flushes completed batches. The batch is completed if it has exactly 20 MetricDatum items. 20 is a max number of items aws allows to send in one request.
func (*Batch) FlushCompleteBatchesCtx ¶
func (*Batch) LaunchAutoFlush ¶
LaunchAutoFlush creates a background job that auto-flushes metrics periodically. onError is an optional parameter (nil can be provided).
Example ¶
batch := cwatsch.New(&cwAPI) defer batch.Flush() ctx, cancel := context.WithCancel(context.Background()) defer cancel() // the metrics will be auto-flushed every 30s batch.LaunchAutoFlush(ctx, 30*time.Second, func(err error) { log.Println(err) }) batch.Add("myApp", &cloudwatch.MetricDatum{ MetricName: aws.String("number_of_calls"), Value: aws.Float64(1), Timestamp: aws.Time(time.Now()), })
Output:
func (*Batch) PutMetricData ¶
func (b *Batch) PutMetricData(input *cw.PutMetricDataInput) (*cw.PutMetricDataOutput, error)
Click to show internal directories.
Click to hide internal directories.