Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(metrics): add basic metrics type support #789

Merged
merged 28 commits into from Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
d879af1
add metrics definition
viglia Feb 26, 2024
e31599e
add SerializeValue implementation for the 4 metrics
viglia Feb 26, 2024
e76a856
remove unnecessary write
viglia Feb 26, 2024
a1d18dd
fix gauge metric serialization
viglia Feb 26, 2024
a5181e9
Fix linting
viglia Feb 26, 2024
9076a03
add key and value sanitization
viglia Feb 27, 2024
3ab5c89
add tags serialization
viglia Feb 27, 2024
34349ae
add serialize value tests
viglia Feb 27, 2024
cdb1743
fix set values order
viglia Feb 27, 2024
c75f03a
add golang.org/x/exp package
viglia Feb 27, 2024
caeb6d4
add custom definition for sortSlice
viglia Feb 27, 2024
cdcf2e1
add sorting to tags keys before serialization
viglia Feb 27, 2024
b345af6
fix metric sanitization according to updated docs regex
viglia Feb 28, 2024
d71b890
add metrics "json" marshalling
viglia Feb 28, 2024
d6b698f
fix serializeValue test for counter metric
viglia Feb 28, 2024
85e818e
fix lint
viglia Feb 28, 2024
4e5e80a
marshal metric when event type is metric
viglia Feb 28, 2024
d796223
add metrics encoding in envelopeFromBody
viglia Feb 29, 2024
44a8494
remove print statement
viglia Feb 29, 2024
31f97bf
refactor metricUnit
viglia Feb 29, 2024
753e446
use floats in the tests
viglia Feb 29, 2024
f61187c
MetricUnit exportable
viglia Feb 29, 2024
1cb5e19
Move regex in global var
viglia Mar 1, 2024
c682a20
Add timestamp doc
viglia Mar 1, 2024
36bcb9a
improve (t *HTTPSyncTransport) SendEvent(event *Event) logging
viglia Mar 1, 2024
d1ad4e0
improve logging for (t *HTTPTransport) worker()
viglia Mar 1, 2024
d1aa56b
add nanosecond unit
viglia Mar 6, 2024
1405211
Merge branch 'master' into viglia/feat/add-metric-types-basic-support
cleptric Apr 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions interfaces.go
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
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)
}
})
}
}