Skip to content

Commit d13a4cc

Browse files
committed
fix: Drop BleemeoItem annotation
This fix some metrics that were not consistent between BleemeoItem annotation and label item. This was the case for io_utilization on Windows. Originally the BleemeoItem annotation versus item labels was intended to support storing the item into a labels named "device", "path", "volume"... depending on what was measured. It was originally needed because Bleemeo didn't supported arbitrary labels. Now that Bleemeo support it, this distinction only cause trouble and mistake. While fixing it, two other issue where seen: * PostgreSQL detailled database when running on a container didn't worked: it was expected to have the container name concatenated with database name, due to a bug only the container name was kept. * HAproxy had the same issue with backend name in place of database name
1 parent f5eb8f5 commit d13a4cc

File tree

29 files changed

+190
-514
lines changed

29 files changed

+190
-514
lines changed

agent/agent.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1874,12 +1874,12 @@ func (a *agent) sendDockerContainerHealth(ctx context.Context, container facts.C
18741874
Labels: map[string]string{
18751875
types.LabelName: "container_health_status",
18761876
types.LabelMetaContainerName: container.ContainerName(),
1877+
types.LabelItem: container.ContainerName(),
18771878
types.LabelMetaContainerID: container.ID(),
18781879
},
18791880
Annotations: types.MetricAnnotations{
18801881
Status: status,
18811882
ContainerID: container.ID(),
1882-
BleemeoItem: container.ContainerName(),
18831883
},
18841884
Point: types.Point{
18851885
Time: time.Now(),

agent/appender.go

-2
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ func (ma miscAppenderMinute) CollectWithState(ctx context.Context, state registr
118118
}
119119

120120
annotations := types.MetricAnnotations{
121-
BleemeoItem: srv.Instance,
122121
ContainerID: srv.ContainerID,
123122
ServiceName: srv.Name,
124123
ServiceInstance: srv.Instance,
@@ -146,7 +145,6 @@ func (ma miscAppenderMinute) CollectWithState(ctx context.Context, state registr
146145
}
147146

148147
annotations := types.MetricAnnotations{
149-
BleemeoItem: srv.Instance,
150148
ContainerID: srv.ContainerID,
151149
ServiceName: srv.Name,
152150
ServiceInstance: srv.Instance,

bleemeo/internal/filter/metric_filter.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func (f *Filter) IsAllowed(lbls map[string]string, annotations types.MetricAnnot
5353
}
5454

5555
// Deny metrics with an item too long for the API.
56-
if len(annotations.BleemeoItem) > common.APIMetricItemLength ||
56+
if len(lbls[types.LabelItem]) > common.APIMetricItemLength ||
5757
annotations.ServiceName != "" && len(annotations.ServiceInstance) > common.APIServiceInstanceLength {
5858
return false, bleemeoTypes.DenyItemTooLong, nil
5959
}

bleemeo/internal/synchronizer/metrics.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -741,9 +741,8 @@ func (s *Synchronizer) metricUpdateList(ctx context.Context, apiClient types.Met
741741
}
742742

743743
if metricutils.MetricOnlyHasItem(metric.Labels(), agentID) {
744-
annotations := metric.Annotations()
745744
params.Set("label", metric.Labels()[gloutonTypes.LabelName])
746-
params.Set("item", annotations.BleemeoItem)
745+
params.Set("item", metric.Labels()[gloutonTypes.LabelItem])
747746
params.Del("labels_text")
748747
}
749748

@@ -1171,7 +1170,7 @@ func (s *Synchronizer) prepareMetricPayload(
11711170
}
11721171

11731172
if metricutils.MetricOnlyHasItem(labels, agentID) {
1174-
payload.Item = annotations.BleemeoItem
1173+
payload.Item = labels[gloutonTypes.LabelItem]
11751174
payload.LabelsText = ""
11761175
}
11771176

bleemeo/internal/synchronizer/metrics_test.go

-3
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,6 @@ func TestMetricSimpleSync(t *testing.T) {
602602
labels.New(
603603
labels.Label{Name: gloutonTypes.LabelName, Value: "metric"},
604604
labels.Label{Name: gloutonTypes.LabelItem, Value: strconv.FormatInt(int64(n), 10)},
605-
labels.Label{Name: gloutonTypes.LabelMetaBleemeoItem, Value: strconv.FormatInt(int64(n), 10)},
606605
labels.Label{Name: gloutonTypes.LabelInstanceUUID, Value: idAgentMain},
607606
),
608607
})
@@ -656,7 +655,6 @@ func TestMetricSimpleSync(t *testing.T) {
656655
labels.New(
657656
labels.Label{Name: gloutonTypes.LabelName, Value: "disk_used"},
658657
labels.Label{Name: gloutonTypes.LabelItem, Value: "/home"},
659-
labels.Label{Name: gloutonTypes.LabelMetaBleemeoItem, Value: "/home"},
660658
labels.Label{Name: gloutonTypes.LabelInstanceUUID, Value: idAgentMain},
661659
),
662660
})
@@ -1822,7 +1820,6 @@ func TestServiceStatusRename(t *testing.T) { //nolint: maintidx
18221820
labels.New(
18231821
labels.Label{Name: gloutonTypes.LabelName, Value: "nginx_status"},
18241822
labels.Label{Name: gloutonTypes.LabelItem, Value: "container1"},
1825-
labels.Label{Name: gloutonTypes.LabelMetaBleemeoItem, Value: "container1"},
18261823
),
18271824
gloutonTypes.MetricAnnotations{
18281825
ServiceName: srvNginx.Name,

bleemeo/internal/synchronizer/sync_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -1098,7 +1098,6 @@ func TestBleemeoPlan(t *testing.T) { //nolint:maintidx
10981098
labels.Label{Name: gloutonTypes.LabelMetaServiceName, Value: "redis"},
10991099
labels.Label{Name: gloutonTypes.LabelMetaServiceInstance, Value: "short-redis-container-name"},
11001100
labels.Label{Name: gloutonTypes.LabelMetaContainerID, Value: "1234"},
1101-
labels.Label{Name: gloutonTypes.LabelMetaBleemeoItem, Value: "short-redis-container-name"},
11021101
labels.Label{Name: gloutonTypes.LabelItem, Value: "short-redis-container-name"},
11031102
),
11041103
})

discovery/metrics.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
"github.com/bleemeo/glouton/inputs/jenkins"
3939
"github.com/bleemeo/glouton/inputs/mem"
4040
"github.com/bleemeo/glouton/inputs/memcached"
41+
"github.com/bleemeo/glouton/inputs/modify"
4142
"github.com/bleemeo/glouton/inputs/mongodb"
4243
"github.com/bleemeo/glouton/inputs/mysql"
4344
"github.com/bleemeo/glouton/inputs/nats"
@@ -476,7 +477,7 @@ func (d *Discovery) registerInput(input telegraf.Input, opts registry.Registrati
476477
}
477478

478479
if service.Instance != "" {
479-
extraLabels[types.LabelItem] = service.Instance
480+
input = modify.AddInstance(input, service.Instance)
480481
}
481482

482483
if opts.Description == "" {

facts/container-runtime/containerd/containerd.go

-2
Original file line numberDiff line numberDiff line change
@@ -1502,7 +1502,6 @@ func rateFromMetricValue(gloutonIDToName map[string]string, pastValues []metricV
15021502
types.LabelMetaContainerID: id,
15031503
},
15041504
Annotations: types.MetricAnnotations{
1505-
BleemeoItem: name,
15061505
ContainerID: id,
15071506
},
15081507
})
@@ -1529,7 +1528,6 @@ func rateFromMetricValue(gloutonIDToName map[string]string, pastValues []metricV
15291528
types.LabelMetaContainerID: id,
15301529
},
15311530
Annotations: types.MetricAnnotations{
1532-
BleemeoItem: name,
15331531
ContainerID: id,
15341532
},
15351533
})

inputs/disk/disk.go

-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ func (dt diskTransformer) renameGlobal(gatherContext internal.GatherContext) (in
9292
return gatherContext, true
9393
}
9494

95-
gatherContext.Annotations.BleemeoItem = item
9695
gatherContext.Tags[types.LabelItem] = item
9796

9897
return gatherContext, false

inputs/diskio/diskio.go

-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ func (dt diskIOTransformer) renameGlobal(gatherContext internal.GatherContext) (
7070
return gatherContext, true
7171
}
7272

73-
gatherContext.Annotations.BleemeoItem = item
7473
gatherContext.Tags[types.LabelItem] = item
7574

7675
return gatherContext, false

inputs/docker/docker.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ func (r renamer) renameGlobal(gatherContext internal.GatherContext) (internal.Ga
7575
gatherContext.Tags = make(map[string]string)
7676

7777
if name, ok := gatherContext.OriginalTags["container_name"]; ok {
78-
gatherContext.Annotations.BleemeoItem = name
7978
gatherContext.Tags[types.LabelMetaContainerName] = name
79+
gatherContext.Tags[types.LabelItem] = name
8080
}
8181

8282
if id, ok := gatherContext.OriginalFields["container_id"]; ok {

inputs/haproxy/haproxy.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
"github.com/bleemeo/glouton/inputs"
2525
"github.com/bleemeo/glouton/inputs/internal"
26+
"github.com/bleemeo/glouton/types"
2627

2728
"github.com/influxdata/telegraf"
2829
telegraf_inputs "github.com/influxdata/telegraf/plugins/inputs"
@@ -79,7 +80,9 @@ func renameGlobal(gatherContext internal.GatherContext) (internal.GatherContext,
7980
return gatherContext, true
8081
}
8182

82-
gatherContext.Annotations.BleemeoItem = gatherContext.Tags["proxy"]
83+
gatherContext.Tags = map[string]string{
84+
types.LabelItem: gatherContext.Tags["proxy"],
85+
}
8386

8487
return gatherContext, false
8588
}

inputs/internal/accumulator_test.go

+4-10
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,6 @@ func TestRenameCallback(t *testing.T) {
519519
func(labels map[string]string, annotations types.MetricAnnotations) (map[string]string, types.MetricAnnotations) {
520520
labels["service_name"] = "mysql_1"
521521
labels["service"] = "mysql"
522-
annotations.BleemeoItem = "somevalue"
523522

524523
return labels, annotations
525524
},
@@ -558,16 +557,16 @@ func TestRenameCallback2(t *testing.T) {
558557
"db": "dbname",
559558
}
560559
wantAnnotation := types.MetricAnnotations{
561-
BleemeoItem: "postgres_1_dbname",
562-
ContainerID: "1234",
560+
ContainerID: "1234",
561+
ServiceInstance: "changed",
563562
}
564563
finalFunc := func(_ string, _ map[string]interface{}, tags map[string]string, annotations types.MetricAnnotations, _ ...time.Time) {
565564
if !reflect.DeepEqual(tags, want) {
566565
t.Errorf("tags == %v, want %v", tags, want)
567566
}
568567

569568
if !reflect.DeepEqual(annotations, wantAnnotation) {
570-
t.Errorf("annotaions == %v, want %v", annotations, wantAnnotation)
569+
t.Errorf("annotations == %v, want %v", annotations, wantAnnotation)
571570
}
572571

573572
called++
@@ -577,7 +576,7 @@ func TestRenameCallback2(t *testing.T) {
577576
acc := Accumulator{
578577
RenameGlobal: func(gatherContext GatherContext) (GatherContext, bool) {
579578
gatherContext.Annotations = types.MetricAnnotations{
580-
BleemeoItem: "dbname",
579+
ServiceInstance: "changed",
581580
}
582581

583582
return gatherContext, false
@@ -586,11 +585,6 @@ func TestRenameCallback2(t *testing.T) {
586585
func(labels map[string]string, annotations types.MetricAnnotations) (map[string]string, types.MetricAnnotations) {
587586
labels[types.LabelContainerName] = "postgres_1"
588587
annotations.ContainerID = "1234"
589-
if annotations.BleemeoItem != "" {
590-
annotations.BleemeoItem = labels[types.LabelContainerName] + "_" + annotations.BleemeoItem
591-
} else {
592-
annotations.BleemeoItem = labels[types.LabelContainerName]
593-
}
594588

595589
return labels, annotations
596590
},

inputs/modify/tags.go

+15
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,18 @@ func AddRenameCallback(input telegraf.Input, f RenameCallback) telegraf.Input {
5151

5252
return internalInput
5353
}
54+
55+
// AddInstance will add the instance into item label. If item already exists, it will prefix is with instance.
56+
func AddInstance(input telegraf.Input, instance string) telegraf.Input {
57+
input = AddRenameCallback(input, func(labels map[string]string, annotations types.MetricAnnotations) (newLabels map[string]string, newAnnotations types.MetricAnnotations) {
58+
if labels[types.LabelItem] == "" {
59+
labels[types.LabelItem] = instance
60+
} else {
61+
labels[types.LabelItem] = instance + "_" + labels[types.LabelItem]
62+
}
63+
64+
return labels, annotations
65+
})
66+
67+
return input
68+
}

inputs/modify/tags_test.go

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
// Copyright 2015-2025 Bleemeo
2+
//
3+
// bleemeo.com an infrastructure monitoring solution in the Cloud
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
package modify
18+
19+
import (
20+
"maps"
21+
"testing"
22+
"time"
23+
24+
"github.com/bleemeo/glouton/inputs/internal"
25+
"github.com/bleemeo/glouton/types"
26+
"github.com/google/go-cmp/cmp"
27+
"github.com/influxdata/telegraf"
28+
)
29+
30+
type fixedInput struct {
31+
measurementName string
32+
tags map[string]string
33+
fields map[string]interface{}
34+
now time.Time
35+
}
36+
37+
func (i *fixedInput) Gather(acc telegraf.Accumulator) error {
38+
tagsCopy := maps.Clone(i.tags)
39+
fieldsCopy := maps.Clone(i.fields)
40+
acc.AddFields(i.measurementName, fieldsCopy, tagsCopy, i.now)
41+
42+
return nil
43+
}
44+
45+
func (*fixedInput) SampleConfig() string {
46+
return ""
47+
}
48+
49+
func TestAddInstance(t *testing.T) {
50+
const (
51+
containerName = "container_name"
52+
measurementName = "fixed_measure"
53+
)
54+
55+
fields := map[string]interface{}{"cpu": 4.2}
56+
now := time.Now()
57+
58+
tests := []struct {
59+
name string
60+
instance string
61+
tags map[string]string
62+
want []internal.Measurement
63+
}{
64+
{
65+
name: "instance-without-tags",
66+
instance: containerName,
67+
tags: map[string]string{},
68+
want: []internal.Measurement{
69+
{
70+
Name: measurementName,
71+
Fields: fields,
72+
Tags: map[string]string{types.LabelItem: containerName},
73+
T: []time.Time{now},
74+
},
75+
},
76+
},
77+
{
78+
name: "instance-with-tags",
79+
instance: containerName,
80+
tags: map[string]string{
81+
"key": "value",
82+
},
83+
want: []internal.Measurement{
84+
{
85+
Name: measurementName,
86+
Fields: fields,
87+
Tags: map[string]string{types.LabelItem: containerName, "key": "value"},
88+
T: []time.Time{now},
89+
},
90+
},
91+
},
92+
{
93+
name: "instance-with-item",
94+
instance: containerName,
95+
tags: map[string]string{
96+
types.LabelItem: "value",
97+
},
98+
want: []internal.Measurement{
99+
{
100+
Name: measurementName,
101+
Fields: fields,
102+
Tags: map[string]string{types.LabelItem: containerName + "_value"},
103+
T: []time.Time{now},
104+
},
105+
},
106+
},
107+
}
108+
for _, tt := range tests {
109+
t.Run(tt.name, func(t *testing.T) {
110+
acc := &internal.StoreAccumulator{}
111+
112+
input := AddInstance(&fixedInput{tags: tt.tags, measurementName: measurementName, fields: fields, now: now}, tt.instance)
113+
114+
if err := input.Gather(acc); err != nil {
115+
t.Error(err)
116+
}
117+
118+
if diff := cmp.Diff(tt.want, acc.Measurement); diff != "" {
119+
t.Errorf("measurement mismatch (-want +got)\n%s", diff)
120+
}
121+
})
122+
}
123+
}

inputs/net/net.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ func (nt netTransformer) renameGlobal(gatherContext internal.GatherContext) (int
7575
return gatherContext, true
7676
}
7777

78-
gatherContext.Annotations.BleemeoItem = item
7978
gatherContext.Tags[types.LabelItem] = item
8079

8180
return gatherContext, false
@@ -102,7 +101,7 @@ func (nt netTransformer) renameCallback(
102101
labels map[string]string,
103102
annotations types.MetricAnnotations,
104103
) (map[string]string, types.MetricAnnotations) {
105-
containerID, err := nt.vethProvider.ContainerID(annotations.BleemeoItem)
104+
containerID, err := nt.vethProvider.ContainerID(labels[types.LabelItem])
106105
if err != nil {
107106
logger.V(1).Printf("Failed to get container interfaces: %s", err)
108107

inputs/postgresql/postgresql.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package postgresql
1919
import (
2020
"github.com/bleemeo/glouton/inputs"
2121
"github.com/bleemeo/glouton/inputs/internal"
22+
"github.com/bleemeo/glouton/types"
2223

2324
"github.com/influxdata/telegraf"
2425
telegraf_config "github.com/influxdata/telegraf/config"
@@ -70,7 +71,7 @@ func renameGlobal(detailedDatabases []string) func(internal.GatherContext) (inte
7071

7172
for _, db := range detailedDatabases {
7273
if db == gatherContext.Tags["db"] {
73-
gatherContext.Annotations.BleemeoItem = gatherContext.Tags["db"]
74+
gatherContext.Tags[types.LabelItem] = gatherContext.Tags["db"]
7475

7576
return gatherContext, false
7677
}

0 commit comments

Comments
 (0)