bosun.org@v0.0.0-20210513094433-e25bc3e69a1f/cmd/tsdbrelay/denormalize/denormalization_test.go (about)

     1  package denormalize
     2  
     3  import (
     4  	"testing"
     5  
     6  	"bosun.org/opentsdb"
     7  )
     8  
     9  func TestSimpleRewrite(t *testing.T) {
    10  	rule := &DenormalizationRule{
    11  		Metric:   "a.b.c",
    12  		TagNames: []string{"host"},
    13  	}
    14  	tags := opentsdb.TagSet{"host": "foo-bar", "baz": "qwerty"}
    15  	dp := &opentsdb.DataPoint{
    16  		Metric:    "a.b.c",
    17  		Timestamp: 42,
    18  		Value:     3,
    19  		Tags:      tags.Copy(),
    20  	}
    21  	err := rule.Translate(dp)
    22  	if err != nil {
    23  		t.Fatal(err)
    24  	}
    25  	expectedName := "__foo-bar.a.b.c"
    26  	if dp.Metric != expectedName {
    27  		t.Errorf("metric name %s is not `%s`", dp.Metric, expectedName)
    28  	}
    29  	if dp.Timestamp != 42 {
    30  		t.Errorf("new metric timestamp does not match. %d != 42", dp.Timestamp)
    31  	}
    32  	if dp.Value != 3 {
    33  		t.Errorf("new metric value does not match. %d != 3", dp.Value)
    34  	}
    35  	if !dp.Tags.Equal(tags) {
    36  		t.Errorf("new metric tags do not match. %v != %v", dp.Tags, tags)
    37  	}
    38  }
    39  
    40  func TestMultipleTags(t *testing.T) {
    41  	rule := &DenormalizationRule{
    42  		Metric:   "a.b.c",
    43  		TagNames: []string{"host", "interface"},
    44  	}
    45  	dp := &opentsdb.DataPoint{
    46  		Metric: "a.b.c",
    47  		Tags:   opentsdb.TagSet{"host": "foo-bar", "interface": "eth0"},
    48  	}
    49  	err := rule.Translate(dp)
    50  	if err != nil {
    51  		t.Fatal(err)
    52  	}
    53  	expectedName := "__foo-bar.eth0.a.b.c"
    54  	if dp.Metric != expectedName {
    55  		t.Errorf("metric name %s is not `%s`", dp.Metric, expectedName)
    56  	}
    57  }
    58  
    59  func TestRewrite_TagNotPresent(t *testing.T) {
    60  	rule := &DenormalizationRule{
    61  		Metric:   "a.b.c",
    62  		TagNames: []string{"host"},
    63  	}
    64  	// Denormalization rule specified host, but data point has no host.
    65  	// Return error on translate and don't send anything downstream.
    66  	dp := &opentsdb.DataPoint{
    67  		Metric:    "a.b.c",
    68  		Timestamp: 42,
    69  		Value:     3,
    70  		Tags:      opentsdb.TagSet{"baz": "qwerty"},
    71  	}
    72  	err := rule.Translate(dp)
    73  	if err == nil {
    74  		t.Fatal("Expected error but got none.")
    75  	}
    76  }