Skip to content

Commit

Permalink
Log metric unit too
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Dec 13, 2024
1 parent 108bb60 commit bbb3bdc
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
2 changes: 1 addition & 1 deletion aws/cwatch/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (c *DevClient) PutMetricData(ctx context.Context, params *cloudwatch.PutMet
for _, dim := range md.Dimensions {
log = log.With(strings.ToLower(aws.ToString(dim.Name)), aws.ToString(dim.Value))
}
log.With("metric", aws.ToString(md.MetricName), "value", aws.ToFloat64(md.Value)).Info("put metric data")
log.With("metric", aws.ToString(md.MetricName), "value", aws.ToFloat64(md.Value), "unit", md.Unit).Info("put metric data")
}

c.callCount.Add(1)
Expand Down
5 changes: 3 additions & 2 deletions aws/cwatch/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ type Service struct {
batcher *syncx.Batcher[types.MetricDatum]
}

// NewService creates a new Cloudwatch service with the given credentials and configuration
// NewService creates a new Cloudwatch service with the given credentials and configuration. If deployment is "dev" then
// then instead of a real Cloudwatch client, the service will get a mocked version that just logs metrics.
func NewService(accessKey, secretKey, region, namespace, deployment string) (*Service, error) {
var client Client

Expand Down Expand Up @@ -75,6 +76,6 @@ func (s *Service) Prepare(data []types.MetricDatum) *cloudwatch.PutMetricDataInp
func (s *Service) processBatch(batch []types.MetricDatum) {
_, err := s.Client.PutMetricData(context.TODO(), s.Prepare(batch))
if err != nil {
slog.Error("error sending metrics to cloudwatch", "error", err, "count", len(batch))
slog.Error("error sending metric data batch", "error", err, "count", len(batch))

Check warning on line 79 in aws/cwatch/service.go

View check run for this annotation

Codecov / codecov/patch

aws/cwatch/service.go#L79

Added line #L79 was not covered by tests
}
}
6 changes: 3 additions & 3 deletions aws/cwatch/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ func TestService(t *testing.T) {

// test writing metrics directly via the client
_, err = svc.Client.PutMetricData(context.Background(), svc.Prepare([]types.MetricDatum{
{MetricName: aws.String("NumGoats"), Value: aws.Float64(10)},
{MetricName: aws.String("NumSheep"), Dimensions: []types.Dimension{{Name: aws.String("Host"), Value: aws.String("foo1")}}, Value: aws.Float64(20)},
{MetricName: aws.String("NumGoats"), Value: aws.Float64(10), Unit: types.StandardUnitCount},
{MetricName: aws.String("NumSheep"), Dimensions: []types.Dimension{{Name: aws.String("Host"), Value: aws.String("foo1")}}, Value: aws.Float64(20), Unit: types.StandardUnitCount},
}))
assert.NoError(t, err)

// test queuing metrics to be sent by batching process
svc.Queue(types.MetricDatum{MetricName: aws.String("NumFish"), Value: aws.Float64(30)})
svc.Queue(types.MetricDatum{MetricName: aws.String("SleepTime"), Value: aws.Float64(30), Unit: types.StandardUnitSeconds})

svc.StopQueue()
wg.Wait()
Expand Down

0 comments on commit bbb3bdc

Please sign in to comment.