aboutsummaryrefslogtreecommitdiffstats
path: root/library/go/core/metrics/solomon/timer_test.go
blob: 4904815701b69736c078803cc442d2b193234ffa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package solomon

import (
	"encoding/json"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
	"go.uber.org/atomic"
)

func TestTimer_RecordDuration(t *testing.T) {
	c := &Timer{
		name:       "mytimer",
		metricType: typeGauge,
		tags:       map[string]string{"ololo": "trololo"},
	}

	c.RecordDuration(1 * time.Second)
	assert.Equal(t, 1*time.Second, c.value.Load())

	c.RecordDuration(42 * time.Millisecond)
	assert.Equal(t, 42*time.Millisecond, c.value.Load())
}

func TestTimerRated_MarshalJSON(t *testing.T) {
	c := &Timer{
		name:       "mytimer",
		metricType: typeRated,
		tags:       map[string]string{"ololo": "trololo"},
		value:      *atomic.NewDuration(42 * time.Millisecond),
	}

	b, err := json.Marshal(c)
	assert.NoError(t, err)

	expected := []byte(`{"type":"RATE","labels":{"ololo":"trololo","sensor":"mytimer"},"value":0.042}`)
	assert.Equal(t, expected, b)
}

func TestNameTagTimer_MarshalJSON(t *testing.T) {
	c := &Timer{
		name:       "mytimer",
		metricType: typeRated,
		tags:       map[string]string{"ololo": "trololo"},
		value:      *atomic.NewDuration(42 * time.Millisecond),

		useNameTag: true,
	}

	b, err := json.Marshal(c)
	assert.NoError(t, err)

	expected := []byte(`{"type":"RATE","labels":{"name":"mytimer","ololo":"trololo"},"value":0.042}`)
	assert.Equal(t, expected, b)
}