Files
Joe ElliottandGitHub 8a871e207e [Metrics Generator] Handle invalid utf8 (#5980)
* drop invalid utf8

Signed-off-by: Joe Elliott <[email protected]>

* validate in CloseAndBuildLabels()

Signed-off-by: Joe Elliott <[email protected]>

* changelog

Signed-off-by: Joe Elliott <[email protected]>

* removed dumbest comment ever

Signed-off-by: Joe Elliott <[email protected]>

---------

Signed-off-by: Joe Elliott <[email protected]>
2025-11-19 16:21:03 -05:00

109 lines
3.3 KiB
Go

package hostinfo
import (
"context"
"strconv"
"testing"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/prometheus/model/labels"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/grafana/tempo/modules/generator/processor"
"github.com/grafana/tempo/modules/generator/registry"
"github.com/grafana/tempo/pkg/tempopb"
common_v1 "github.com/grafana/tempo/pkg/tempopb/common/v1"
trace_v1 "github.com/grafana/tempo/pkg/tempopb/trace/v1"
"github.com/grafana/tempo/pkg/util/test"
)
func TestHostInfo(t *testing.T) {
testRegistry := registry.NewTestRegistry()
cfg := Config{}
cfg.RegisterFlagsAndApplyDefaults("", nil)
p, err := New(cfg, testRegistry, nil, prometheus.NewCounter(prometheus.CounterOpts{}))
require.NoError(t, err)
require.Equal(t, p.Name(), processor.HostInfoName)
defer p.Shutdown(context.TODO())
req := &tempopb.PushSpansRequest{
Batches: []*trace_v1.ResourceSpans{
test.MakeBatch(10, nil),
test.MakeBatch(10, nil),
},
}
for i, b := range req.Batches {
b.Resource.Attributes = append(b.Resource.Attributes, []*common_v1.KeyValue{
{Key: "host.id", Value: &common_v1.AnyValue{Value: &common_v1.AnyValue_StringValue{StringValue: "test" + strconv.Itoa(i)}}},
}...)
}
p.PushSpans(context.Background(), req)
lbls0 := labels.FromMap(map[string]string{
hostIdentifierAttr: "test0",
hostSourceAttr: "host.id",
})
assert.Equal(t, 1.0, testRegistry.Query(hostInfoMetric, lbls0))
lbls1 := labels.FromMap(map[string]string{
hostIdentifierAttr: "test1",
hostSourceAttr: "host.id",
})
assert.Equal(t, 1.0, testRegistry.Query(hostInfoMetric, lbls1))
}
func TestHostInfoHostSource(t *testing.T) {
testRegistry := registry.NewTestRegistry()
cfg := Config{}
cfg.RegisterFlagsAndApplyDefaults("", nil)
p, err := New(cfg, testRegistry, nil, prometheus.NewCounter(prometheus.CounterOpts{}))
require.NoError(t, err)
require.Equal(t, p.Name(), processor.HostInfoName)
defer p.Shutdown(context.TODO())
req := &tempopb.PushSpansRequest{
Batches: []*trace_v1.ResourceSpans{
test.MakeBatch(10, nil),
test.MakeBatch(10, nil),
},
}
for i, b := range req.Batches {
if i%2 == 0 {
b.Resource.Attributes = append(b.Resource.Attributes, []*common_v1.KeyValue{
{Key: "k8s.node.name", Value: &common_v1.AnyValue{Value: &common_v1.AnyValue_StringValue{StringValue: "test" + strconv.Itoa(i)}}},
}...)
}
b.Resource.Attributes = append(b.Resource.Attributes, []*common_v1.KeyValue{
{Key: "host.id", Value: &common_v1.AnyValue{Value: &common_v1.AnyValue_StringValue{StringValue: "test" + strconv.Itoa(i)}}},
}...)
}
p.PushSpans(context.Background(), req)
for i := range len(req.Batches) {
hostIDLbls := labels.FromMap(map[string]string{
hostIdentifierAttr: "test" + strconv.Itoa(i),
hostSourceAttr: "host.id",
})
k8sNodeNameLbls := labels.FromMap(map[string]string{
hostIdentifierAttr: "test" + strconv.Itoa(i),
hostSourceAttr: "k8s.node.name",
})
if i%2 == 0 {
assert.Equal(t, 0.0, testRegistry.Query(hostInfoMetric, hostIDLbls))
assert.Equal(t, 1.0, testRegistry.Query(hostInfoMetric, k8sNodeNameLbls))
} else {
assert.Equal(t, 1.0, testRegistry.Query(hostInfoMetric, hostIDLbls))
assert.Equal(t, 0.0, testRegistry.Query(hostInfoMetric, k8sNodeNameLbls))
}
}
}