123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- // Copyright The OpenTelemetry Authors
- // SPDX-License-Identifier: Apache-2.0
- package splunk
- import (
- "encoding/json"
- "strings"
- "testing"
- "github.com/stretchr/testify/assert"
- )
- func TestGetValues(t *testing.T) {
- metric := Event{
- Fields: map[string]any{},
- }
- assert.Equal(t, map[string]any{}, metric.GetMetricValues())
- metric.Fields["metric_name:foo"] = "bar"
- assert.Equal(t, map[string]any{"foo": "bar"}, metric.GetMetricValues())
- metric.Fields["metric_name:foo2"] = "foobar"
- assert.Equal(t, map[string]any{"foo": "bar", "foo2": "foobar"}, metric.GetMetricValues())
- }
- func TestIsMetric(t *testing.T) {
- ev := Event{
- Event: map[string]any{},
- }
- assert.False(t, ev.IsMetric())
- metric := Event{
- Event: "metric",
- }
- assert.True(t, metric.IsMetric())
- arr := Event{
- Event: []any{"foo", "bar"},
- }
- assert.False(t, arr.IsMetric())
- yo := Event{
- Event: "yo",
- }
- assert.False(t, yo.IsMetric())
- }
- func TestIsMetric_WithoutEventField(t *testing.T) {
- fieldsOnly := Event{
- Fields: map[string]any{
- "foo": "bar",
- },
- }
- assert.False(t, fieldsOnly.IsMetric())
- fieldsWithMetrics := Event{
- Fields: map[string]any{
- "foo": "bar",
- "metric_name:foo": 123,
- "foobar": "foobar",
- },
- }
- assert.True(t, fieldsWithMetrics.IsMetric())
- }
- func TestDecodeJsonWithNoTime(t *testing.T) {
- dec := json.NewDecoder(strings.NewReader("{\"event\":\"hello\"}"))
- dec.More()
- var msg Event
- err := dec.Decode(&msg)
- assert.NoError(t, err)
- assert.Zero(t, msg.Time)
- }
- func TestDecodeJsonWithNumberTime(t *testing.T) {
- dec := json.NewDecoder(strings.NewReader("{\"time\":1610760752.606,\"event\":\"hello\"}"))
- dec.More()
- var msg Event
- err := dec.Decode(&msg)
- assert.NoError(t, err)
- assert.Equal(t, 1610760752.606, msg.Time)
- }
- func TestDecodeJsonWithStringTime(t *testing.T) {
- dec := json.NewDecoder(strings.NewReader("{\"time\":\"1610760752.606\",\"event\":\"hello\"}"))
- dec.More()
- var msg Event
- err := dec.Decode(&msg)
- assert.NoError(t, err)
- assert.Equal(t, 1610760752.606, msg.Time)
- }
- func TestDecodeJsonWithInvalidStringTime(t *testing.T) {
- dec := json.NewDecoder(strings.NewReader("{\"time\":\"1610760752.606\\\"\",\"event\":\"hello\"}"))
- dec.More()
- var msg Event
- err := dec.Decode(&msg)
- assert.Error(t, err)
- }
- func TestDecodeJsonWithInvalidNumberStringTime(t *testing.T) {
- dec := json.NewDecoder(strings.NewReader("{\"time\":\"0xdeadbeef\",\"event\":\"hello\"}"))
- dec.More()
- var msg Event
- err := dec.Decode(&msg)
- assert.Error(t, err)
- }
- func TestDecodeJsonWithInvalidNumberTime(t *testing.T) {
- dec := json.NewDecoder(strings.NewReader("{\"time\":1e1024,\"event\":\"hello\"}"))
- dec.More()
- var msg Event
- err := dec.Decode(&msg)
- assert.Error(t, err)
- }
|