Skip to content

Commit 595fbc7

Browse files
rlankfoknylander-grafana
authored andcommitted
add host_source label to track host identifier attribute (grafana#5152)
* add host_source label to track host identifier attribute * update changelog * fix lint
1 parent 2755fe6 commit 595fbc7

File tree

3 files changed

+66
-8
lines changed

3 files changed

+66
-8
lines changed

‎CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ configurable via the throughput_bytes_slo field, and it will populate op="traces
7575
* [ENHANCEMENT] Support TraceQL Metrics checks in Vulture [#4886](https://github.com/grafana/tempo/pull/4886) (@ruslan-mikhailov)
7676
* [ENHANCEMENT] Add memcached to the resources dashboard [#5049](https://github.com/grafana/tempo/pull/5049) (@javiermolinar)
7777
* [ENHANCEMENT] Query-frontend: logs add msg to the log line [#4975](https://github.com/grafana/tempo/pull/4975) (@jmichalek132)
78+
* [ENHANCEMENT] Host Info Processor: track host identifying resource attribute in metric [#5152](https://github.com/grafana/tempo/pull/5152) (@rlankfo)
7879
* [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)
7980
Correctly copy exemplars for metrics like `| rate()` when gRPC streaming.
8081
* [BUGFIX] Make comparison to nil symmetric [#4869](https://github.com/grafana/tempo/pull/4869) (@stoewer)

‎modules/generator/processor/hostinfo/processor.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const (
1414

1515
hostInfoMetric = "traces_host_info"
1616
hostIdentifierAttr = "grafana_host_id"
17+
hostSourceAttr = "host_source"
1718
)
1819

1920
type Processor struct {
@@ -30,28 +31,30 @@ func (p *Processor) Name() string {
3031
return Name
3132
}
3233

33-
func (p *Processor) findHostIdentifier(resourceSpans *v1.ResourceSpans) string {
34+
func (p *Processor) findHostIdentifier(resourceSpans *v1.ResourceSpans) (string, string) {
3435
attrs := resourceSpans.GetResource().GetAttributes()
3536
for _, idAttr := range p.Cfg.HostIdentifiers {
3637
for _, attr := range attrs {
37-
if attr.GetKey() == idAttr {
38+
hostSource := attr.GetKey()
39+
if hostSource == idAttr {
3840
if val := attr.GetValue(); val != nil {
3941
if strVal := val.GetStringValue(); strVal != "" {
40-
return strVal
42+
return strVal, hostSource
4143
}
4244
}
4345
}
4446
}
4547
}
46-
return ""
48+
return "", ""
4749
}
4850

4951
func (p *Processor) PushSpans(_ context.Context, req *tempopb.PushSpansRequest) {
50-
values := make([]string, 1)
52+
values := make([]string, 2)
5153
for i := range req.Batches {
5254
resourceSpans := req.Batches[i]
53-
if hostID := p.findHostIdentifier(resourceSpans); hostID != "" {
55+
if hostID, hostSource := p.findHostIdentifier(resourceSpans); hostID != "" && hostSource != "" {
5456
values[0] = hostID
57+
values[1] = hostSource
5558
labelValues := p.registry.NewLabelValueCombo(
5659
p.labels,
5760
values,
@@ -64,8 +67,9 @@ func (p *Processor) PushSpans(_ context.Context, req *tempopb.PushSpansRequest)
6467
func (p *Processor) Shutdown(_ context.Context) {}
6568

6669
func New(cfg Config, reg registry.Registry, logger log.Logger) (*Processor, error) {
67-
labels := make([]string, 1)
70+
labels := make([]string, 2)
6871
labels[0] = hostIdentifierAttr
72+
labels[1] = hostSourceAttr
6973
p := &Processor{
7074
Cfg: cfg,
7175
logger: logger,

‎modules/generator/processor/hostinfo/processor_test.go

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import (
1717

1818
func TestHostInfo(t *testing.T) {
1919
testRegistry := registry.NewTestRegistry()
20-
2120
cfg := Config{}
21+
2222
cfg.RegisterFlagsAndApplyDefaults("", nil)
2323
p, err := New(cfg, testRegistry, nil)
2424
require.NoError(t, err)
@@ -42,11 +42,64 @@ func TestHostInfo(t *testing.T) {
4242

4343
lbls0 := labels.FromMap(map[string]string{
4444
hostIdentifierAttr: "test0",
45+
hostSourceAttr: "host.id",
4546
})
4647
assert.Equal(t, 1.0, testRegistry.Query(hostInfoMetric, lbls0))
4748

4849
lbls1 := labels.FromMap(map[string]string{
4950
hostIdentifierAttr: "test1",
51+
hostSourceAttr: "host.id",
5052
})
5153
assert.Equal(t, 1.0, testRegistry.Query(hostInfoMetric, lbls1))
5254
}
55+
56+
func TestHostInfoHostSource(t *testing.T) {
57+
testRegistry := registry.NewTestRegistry()
58+
59+
cfg := Config{}
60+
cfg.RegisterFlagsAndApplyDefaults("", nil)
61+
p, err := New(cfg, testRegistry, nil)
62+
require.NoError(t, err)
63+
require.Equal(t, p.Name(), Name)
64+
defer p.Shutdown(context.TODO())
65+
66+
req := &tempopb.PushSpansRequest{
67+
Batches: []*trace_v1.ResourceSpans{
68+
test.MakeBatch(10, nil),
69+
test.MakeBatch(10, nil),
70+
},
71+
}
72+
73+
for i, b := range req.Batches {
74+
if i%2 == 0 {
75+
b.Resource.Attributes = append(b.Resource.Attributes, []*common_v1.KeyValue{
76+
{Key: "k8s.node.name", Value: &common_v1.AnyValue{Value: &common_v1.AnyValue_StringValue{StringValue: "test" + strconv.Itoa(i)}}},
77+
}...)
78+
}
79+
b.Resource.Attributes = append(b.Resource.Attributes, []*common_v1.KeyValue{
80+
{Key: "host.id", Value: &common_v1.AnyValue{Value: &common_v1.AnyValue_StringValue{StringValue: "test" + strconv.Itoa(i)}}},
81+
}...)
82+
}
83+
84+
p.PushSpans(context.Background(), req)
85+
86+
for i := range len(req.Batches) {
87+
hostIDLbls := labels.FromMap(map[string]string{
88+
hostIdentifierAttr: "test" + strconv.Itoa(i),
89+
hostSourceAttr: "host.id",
90+
})
91+
k8sNodeNameLbls := labels.FromMap(map[string]string{
92+
hostIdentifierAttr: "test" + strconv.Itoa(i),
93+
hostSourceAttr: "k8s.node.name",
94+
})
95+
96+
if i%2 == 0 {
97+
assert.Equal(t, 0.0, testRegistry.Query(hostInfoMetric, hostIDLbls))
98+
assert.Equal(t, 1.0, testRegistry.Query(hostInfoMetric, k8sNodeNameLbls))
99+
} else {
100+
assert.Equal(t, 1.0, testRegistry.Query(hostInfoMetric, hostIDLbls))
101+
assert.Equal(t, 0.0, testRegistry.Query(hostInfoMetric, k8sNodeNameLbls))
102+
}
103+
104+
}
105+
}

0 commit comments

Comments
 (0)