Skip to content

Commit

Permalink
Add generic metric function
Browse files Browse the repository at this point in the history
  • Loading branch information
FollowTheProcess committed Nov 5, 2023
1 parent 2eab6ff commit f05293e
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 4 deletions.
26 changes: 22 additions & 4 deletions metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"io"
"os"
"strings"
"sync"
"time"

"github.com/FollowTheProcess/metrics/unit"
Expand Down Expand Up @@ -65,6 +66,8 @@ type MetricDefinition struct {
}

// Logger is the mechanism to write EMF metrics.
//
// A Logger is safe to use concurrently across goroutines.
type Logger struct {
// Where to write EMF metrics to.
stdout io.Writer
Expand All @@ -81,6 +84,9 @@ type Logger struct {

// The actual metrics.
metrics MetricDirective

// Synchronisation
mu sync.Mutex
}

// Option is a functional option to configure a Logger.
Expand Down Expand Up @@ -141,13 +147,25 @@ func New(opts ...Option) *Logger {
}

// Count records a count metric.
func (l *Logger) Count(name string, count int, resolution StorageResolution) *Logger {
l.store(name, count, unit.Count, resolution)
func (l *Logger) Count(name string, count int, res StorageResolution) *Logger {
l.mu.Lock()
defer l.mu.Unlock()
l.store(name, count, unit.Count, res)
return l
}

// Add records a generic user defined metric.
func (l *Logger) Add(name string, value any, unit unit.Unit, res StorageResolution) *Logger {
l.mu.Lock()
defer l.mu.Unlock()
l.store(name, value, unit, res)
return l
}

// Dimension adds a metrics dimension.
func (l *Logger) Dimension(key, value string) *Logger {
l.mu.Lock()
defer l.mu.Unlock()
l.metrics.Dimensions = append(l.metrics.Dimensions, Dimension{key})
l.values[key] = value
return l
Expand Down Expand Up @@ -181,12 +199,12 @@ func (l *Logger) Flush() error {
}

// store inserts a metric into the Logger, to be flushed later.
func (l *Logger) store(name string, value any, unit unit.Unit, resolution StorageResolution) {
func (l *Logger) store(name string, value any, unit unit.Unit, res StorageResolution) {
// Store the metric metadata
metric := MetricDefinition{
Name: name,
Unit: unit,
Resolution: resolution,
Resolution: res,
}
l.metrics.Metrics = append(l.metrics.Metrics, metric)

Expand Down
21 changes: 21 additions & 0 deletions metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"

"github.com/FollowTheProcess/metrics"
"github.com/FollowTheProcess/metrics/unit"
"github.com/FollowTheProcess/test"
"github.com/kinbiko/jsonassert"
)
Expand All @@ -20,13 +21,26 @@ func TestMetricsLog(t *testing.T) {
logFn func(*metrics.Logger) // Function to apply to the logger for the test
want string // Name of file containing expected JSON
}{
{
name: "no metrics means empty json",
logFn: func(logger *metrics.Logger) {},
want: "empty.json",
},
{
name: "count",
logFn: func(logger *metrics.Logger) {
logger.Count("something", 5, metrics.StandardResolution)
},
want: "count.json",
},
{
name: "count with trace id",
env: map[string]string{"_X_AMZN_TRACE_ID": "something_looks_like_id_with_Sampled=1"},
logFn: func(logger *metrics.Logger) {
logger.Count("something", 5, metrics.StandardResolution)
},
want: "traceid.json",
},
{
name: "count high resolution",
logFn: func(logger *metrics.Logger) {
Expand All @@ -41,6 +55,13 @@ func TestMetricsLog(t *testing.T) {
},
want: "dimensions.json",
},
{
name: "generic metric",
logFn: func(logger *metrics.Logger) {
logger.Add("Foo", 27, unit.Percent, metrics.StandardResolution)
},
want: "generic.json",
},
}

for _, tt := range tests {
Expand Down
Empty file added testdata/empty.json
Empty file.
31 changes: 31 additions & 0 deletions testdata/generic.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"_aws": {
"Timestamp": "<<PRESENCE>>",
"CloudWatchMetrics": [
{
"Namespace": "aws-embedded-metrics",
"Dimensions": [
[
"ServiceName",
"ServiceType"
]
],
"Metrics": [
{
"Name": "Foo",
"Unit": "Percent",
"StorageResolution": 60
}
]
}
]
},
"executionEnvironment": "",
"functionName": "",
"functionVersion": "",
"logStreamId": "",
"memorySize": "",
"ServiceName": "",
"ServiceType": "AWS::Lambda::Function",
"Foo": 27
}
32 changes: 32 additions & 0 deletions testdata/traceid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"_aws": {
"Timestamp": "<<PRESENCE>>",
"CloudWatchMetrics": [
{
"Namespace": "aws-embedded-metrics",
"Dimensions": [
[
"ServiceName",
"ServiceType"
]
],
"Metrics": [
{
"Name": "something",
"Unit": "Count",
"StorageResolution": 60
}
]
}
]
},
"executionEnvironment": "",
"functionName": "",
"functionVersion": "",
"logStreamId": "",
"memorySize": "",
"something": 5,
"ServiceName": "",
"ServiceType": "AWS::Lambda::Function",
"traceId": "something_looks_like_id_with_Sampled=1"
}

0 comments on commit f05293e

Please sign in to comment.