aboutsummaryrefslogtreecommitdiffstats
path: root/library/go/core/metrics/solomon/gauge_test.go
blob: 82659a49c4596052665e07530f3868b5e01ac3c6 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package solomon

import (
	"encoding/json"
	"testing"

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

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

	c.Add(1)
	assert.Equal(t, float64(1), c.value.Load())

	c.Add(42)
	assert.Equal(t, float64(43), c.value.Load())

	c.Add(14.89)
	assert.Equal(t, float64(57.89), c.value.Load())
}

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

	c.Set(1)
	assert.Equal(t, float64(1), c.value.Load())

	c.Set(42)
	assert.Equal(t, float64(42), c.value.Load())

	c.Set(14.89)
	assert.Equal(t, float64(14.89), c.value.Load())
}

func TestGauge_MarshalJSON(t *testing.T) {
	c := &Gauge{
		name:       "mygauge",
		metricType: typeGauge,
		tags:       map[string]string{"ololo": "trololo"},
		value:      *atomic.NewFloat64(42.18),
	}

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

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

func TestNameTagGauge_MarshalJSON(t *testing.T) {
	c := &Gauge{
		name:       "mygauge",
		metricType: typeGauge,
		tags:       map[string]string{"ololo": "trololo"},
		value:      *atomic.NewFloat64(42.18),

		useNameTag: true,
	}

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

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