Skip to content

add host_source label to track host identifier attribute #5152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ configurable via the throughput_bytes_slo field, and it will populate op="traces
* [ENHANCEMENT] Support TraceQL Metrics checks in Vulture [#4886](https://github.com/grafana/tempo/pull/4886) (@ruslan-mikhailov)
* [ENHANCEMENT] Add memcached to the resources dashboard [#5049](https://github.com/grafana/tempo/pull/5049) (@javiermolinar)
* [ENHANCEMENT] Query-frontend: logs add msg to the log line [#4975](https://github.com/grafana/tempo/pull/4975) (@jmichalek132)
* [ENHANCEMENT] Host Info Processor: track host identifying resource attribute in metric [#5152](https://github.com/grafana/tempo/pull/5152) (@rlankfo)
* [BUGFIX] Choose a default step for a gRPC streaming query range request if none is provided. [#4546](https://github.com/grafana/tempo/pull/4576) (@joe-elliott)
Correctly copy exemplars for metrics like `| rate()` when gRPC streaming.
* [BUGFIX] Make comparison to nil symmetric [#4869](https://github.com/grafana/tempo/pull/4869) (@stoewer)
Expand Down
18 changes: 11 additions & 7 deletions modules/generator/processor/hostinfo/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const (

hostInfoMetric = "traces_host_info"
hostIdentifierAttr = "grafana_host_id"
hostSourceAttr = "host_source"
)

type Processor struct {
Expand All @@ -30,28 +31,30 @@ func (p *Processor) Name() string {
return Name
}

func (p *Processor) findHostIdentifier(resourceSpans *v1.ResourceSpans) string {
func (p *Processor) findHostIdentifier(resourceSpans *v1.ResourceSpans) (string, string) {
attrs := resourceSpans.GetResource().GetAttributes()
for _, idAttr := range p.Cfg.HostIdentifiers {
for _, attr := range attrs {
if attr.GetKey() == idAttr {
hostSource := attr.GetKey()
if hostSource == idAttr {
if val := attr.GetValue(); val != nil {
if strVal := val.GetStringValue(); strVal != "" {
return strVal
return strVal, hostSource
}
}
}
}
}
return ""
return "", ""
}

func (p *Processor) PushSpans(_ context.Context, req *tempopb.PushSpansRequest) {
values := make([]string, 1)
values := make([]string, 2)
for i := range req.Batches {
resourceSpans := req.Batches[i]
if hostID := p.findHostIdentifier(resourceSpans); hostID != "" {
if hostID, hostSource := p.findHostIdentifier(resourceSpans); hostID != "" && hostSource != "" {
values[0] = hostID
values[1] = hostSource
labelValues := p.registry.NewLabelValueCombo(
p.labels,
values,
Expand All @@ -64,8 +67,9 @@ func (p *Processor) PushSpans(_ context.Context, req *tempopb.PushSpansRequest)
func (p *Processor) Shutdown(_ context.Context) {}

func New(cfg Config, reg registry.Registry, logger log.Logger) (*Processor, error) {
labels := make([]string, 1)
labels := make([]string, 2)
labels[0] = hostIdentifierAttr
labels[1] = hostSourceAttr
p := &Processor{
Cfg: cfg,
logger: logger,
Expand Down
55 changes: 54 additions & 1 deletion modules/generator/processor/hostinfo/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import (

func TestHostInfo(t *testing.T) {
testRegistry := registry.NewTestRegistry()

cfg := Config{}

cfg.RegisterFlagsAndApplyDefaults("", nil)
p, err := New(cfg, testRegistry, nil)
require.NoError(t, err)
Expand All @@ -42,11 +42,64 @@ func TestHostInfo(t *testing.T) {

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)
require.NoError(t, err)
require.Equal(t, p.Name(), Name)
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))
}

}
}
Loading