Skip to content

Commit

Permalink
feat(metrics): add basic metrics type support (#789)
Browse files Browse the repository at this point in the history
Co-authored-by: Michi Hoffmann <cleptric@users.noreply.github.com>
  • Loading branch information
viglia and cleptric committed Apr 30, 2024
1 parent 84427d8 commit 9a9f285
Show file tree
Hide file tree
Showing 6 changed files with 726 additions and 0 deletions.
4 changes: 4 additions & 0 deletions interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ const profileType = "profile"
// checkInType is the type of a check in event.
const checkInType = "check_in"

// metricType is the type of a metric event.
const metricType = "statsd"

// Level marks the severity of the event.
type Level string

Expand Down Expand Up @@ -320,6 +323,7 @@ type Event struct {
Exception []Exception `json:"exception,omitempty"`
DebugMeta *DebugMeta `json:"debug_meta,omitempty"`
Attachments []*Attachment `json:"-"`
Metrics []Metric `json:"-"`

// The fields below are only relevant for transactions.

Expand Down
36 changes: 36 additions & 0 deletions interfaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,3 +513,39 @@ func TestStructSnapshots(t *testing.T) {
})
}
}

func TestMarshalMetrics(t *testing.T) {
tests := []struct {
name string
metrics []Metric
want string
}{
{
name: "allowed characters",
metrics: []Metric{
NewCounterMetric("counter", Second(), map[string]string{"foo": "bar", "route": "GET /foo"}, 1597790835, 1.0),
NewDistributionMetric("distribution", Second(), map[string]string{"$foo$": "%bar%"}, 1597790835, 1.0),
NewGaugeMetric("gauge", Second(), map[string]string{"föö": "bär"}, 1597790835, 1.0),
NewSetMetric[int]("set", Second(), map[string]string{"%{key}": "$value$"}, 1597790835, 1),
NewCounterMetric("no_tags", Second(), nil, 1597790835, 1.0),
},

want: strings.Join([]string{
"counter@second:1|c|#foo:bar,route:GET /foo|T1597790835",
"distribution@second:1|d|#_foo_:bar|T1597790835",
"gauge@second:1:1:1:1:1|g|#f_:br|T1597790835",
"set@second:1|s|#_key_:$value$|T1597790835",
"no_tags@second:1|c|T1597790835",
}, "\n"),
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
serializedMetric := marshalMetrics(test.metrics)
if diff := cmp.Diff(string(serializedMetric), test.want); diff != "" {
t.Errorf("Context mismatch (-want +got):\n%s", diff)
}
})
}
}
Loading

0 comments on commit 9a9f285

Please sign in to comment.