VictoriaMetrics/lib/promscrape/discovery/consul/service_node_test.go
Aliaksandr Valialkin f325410c26
lib/promscrape: optimize service discovery speed
- Return meta-labels for the discovered targets via promutils.Labels
  instead of map[string]string. This improves the speed of generating
  meta-labels for discovered targets by up to 5x.

- Remove memory allocations in hot paths during ScrapeWork generation.
  The ScrapeWork contains scrape settings for a single discovered target.
  This improves the service discovery speed by up to 2x.
2022-11-29 21:26:00 -08:00

132 lines
3.5 KiB
Go

package consul
import (
"testing"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promscrape/discoveryutils"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promutils"
)
func TestParseServiceNodesFailure(t *testing.T) {
f := func(s string) {
t.Helper()
sns, err := parseServiceNodes([]byte(s))
if err == nil {
t.Fatalf("expecting non-nil error")
}
if sns != nil {
t.Fatalf("unexpected non-nil ServiceNodes: %v", sns)
}
}
f(``)
f(`[1,23]`)
f(`{"items":[{"metadata":1}]}`)
}
func TestParseServiceNodesSuccess(t *testing.T) {
data := `
[
{
"Node": {
"ID": "40e4a748-2192-161a-0510-9bf59fe950b5",
"Node": "foobar",
"Address": "10.1.10.12",
"Datacenter": "dc1",
"TaggedAddresses": {
"lan": "10.1.10.12",
"wan": "10.1.10.12"
},
"Meta": {
"instance_type": "t2.medium"
}
},
"Service": {
"ID": "redis",
"Service": "redis",
"Tags": ["primary"],
"Address": "10.1.10.12",
"TaggedAddresses": {
"lan": {
"address": "10.1.10.12",
"port": 8000
},
"wan": {
"address": "198.18.1.2",
"port": 80
}
},
"Meta": {
"redis_version": "4.0"
},
"Port": 8000,
"Weights": {
"Passing": 10,
"Warning": 1
},
"Namespace": "ns-dev",
"Partition": "part-foobar"
},
"Checks": [
{
"Node": "foobar",
"CheckID": "service:redis",
"Name": "Service 'redis' check",
"Status": "passing",
"Notes": "",
"Output": "",
"ServiceID": "redis",
"ServiceName": "redis",
"ServiceTags": ["primary"],
"Namespace": "default"
},
{
"Node": "foobar",
"CheckID": "serfHealth",
"Name": "Serf Health Status",
"Status": "passing",
"Notes": "",
"Output": "",
"ServiceID": "",
"ServiceName": "",
"ServiceTags": [],
"Namespace": "default"
}
]
}
]
`
sns, err := parseServiceNodes([]byte(data))
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if len(sns) != 1 {
t.Fatalf("unexpected length of ServiceNodes; got %d; want %d", len(sns), 1)
}
sn := sns[0]
// Check sn.appendTargetLabels()
tagSeparator := ","
labelss := sn.appendTargetLabels(nil, "redis", tagSeparator)
expectedLabelss := []*promutils.Labels{
promutils.NewLabelsFromMap(map[string]string{
"__address__": "10.1.10.12:8000",
"__meta_consul_address": "10.1.10.12",
"__meta_consul_dc": "dc1",
"__meta_consul_health": "passing",
"__meta_consul_metadata_instance_type": "t2.medium",
"__meta_consul_namespace": "ns-dev",
"__meta_consul_node": "foobar",
"__meta_consul_partition": "part-foobar",
"__meta_consul_service": "redis",
"__meta_consul_service_address": "10.1.10.12",
"__meta_consul_service_id": "redis",
"__meta_consul_service_metadata_redis_version": "4.0",
"__meta_consul_service_port": "8000",
"__meta_consul_tagged_address_lan": "10.1.10.12",
"__meta_consul_tagged_address_wan": "10.1.10.12",
"__meta_consul_tags": ",primary,",
}),
}
discoveryutils.TestEqualLabelss(t, labelss, expectedLabelss)
}