Skip to content
This repository was archived by the owner on Dec 1, 2018. It is now read-only.

Commit fc99b06

Browse files
authored
Merge pull request #1560 from mwringe/hawkular-label-tags
[Hawkular] Add labels as individual tags
2 parents 8c6c154 + a8c87c5 commit fc99b06

File tree

5 files changed

+126
-4
lines changed

5 files changed

+126
-4
lines changed

docs/sink-configuration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ The following options are available:
8686
* `name` - The syntax is `name(regexp)` where MetricName is matched (such as `cpu/usage`) with a `regexp` filter
8787
* `batchSize`- How many metrics are sent in each request to Hawkular-Metrics (default is 1000)
8888
* `concurrencyLimit`- How many concurrent requests are used to send data to the Hawkular-Metrics (default is 5)
89+
* `labelTagPrefix` - A prefix to be placed in front of each label when stored as a tag for the metric (default is `labels.`)
8990

9091
A combination of `insecure` / `caCert` / `auth` is not supported, only a single of these parameters is allowed at once. Also, combination of `useServiceAccount` and `user` + `pass` is not supported. To increase the performance of Hawkular sink in case of multiple instances of Hawkular-Metrics (such as scaled scenario in OpenShift) modify the parameters of batchSize and concurrencyLimit to balance the load on Hawkular-Metrics instances.
9192

metrics/sinks/hawkular/client.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,17 @@ func (h *hawkularSink) registerLabeledIfNecessary(ms *core.MetricSet, metric cor
174174
// Set tag values
175175
for k, v := range ms.Labels {
176176
mdd.Tags[k] = v
177+
if k == core.LabelLabels.Key {
178+
labels := strings.Split(v, ",")
179+
for _, label := range labels {
180+
labelKeyValue := strings.Split(label, ":")
181+
if len(labelKeyValue) != 2 {
182+
glog.V(4).Infof("Could not split the label %v into its key and value pair. This label will not be added as a tag in Hawkular Metrics.", label)
183+
} else {
184+
mdd.Tags[h.labelTagPrefix+labelKeyValue[0]] = labelKeyValue[1]
185+
}
186+
}
187+
}
177188
}
178189

179190
// Set the labeled values

metrics/sinks/hawkular/driver.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ const (
4343

4444
nodeId string = "labelNodeId"
4545

46+
labelTagPrefixOpts = "labelTagPrefix"
47+
labelTagPrefixDefault = "labels."
48+
4649
defaultServiceAccountFile = "/var/run/secrets/kubernetes.io/serviceaccount/token"
4750
)
4851

@@ -212,6 +215,12 @@ func (h *hawkularSink) init() error {
212215
h.labelTenant = v[0]
213216
}
214217

218+
if v, found := opts[labelTagPrefixOpts]; found {
219+
h.labelTagPrefix = v[0]
220+
} else {
221+
h.labelTagPrefix = labelTagPrefixDefault
222+
}
223+
215224
if v, found := opts[nodeId]; found {
216225
h.labelNodeId = v[0]
217226
}

metrics/sinks/hawkular/driver_test.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,106 @@ func TestStoreTimeseries(t *testing.T) {
457457
assert.NotEqual(t, ids[0], ids[1])
458458
}
459459

460+
func TestTags(t *testing.T) {
461+
m := &sync.Mutex{}
462+
calls := make([]string, 0, 2)
463+
// how many times tags have been updated
464+
tagsUpdated := 0
465+
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
466+
m.Lock()
467+
defer m.Unlock()
468+
calls = append(calls, r.RequestURI)
469+
w.Header().Set("Content-Type", "application/json")
470+
471+
defer r.Body.Close()
472+
b, err := ioutil.ReadAll(r.Body)
473+
assert.NoError(t, err)
474+
475+
if strings.HasSuffix(r.RequestURI, "/tags") {
476+
tags := make(map[string]string)
477+
err := json.Unmarshal(b, &tags)
478+
assert.NoError(t, err)
479+
480+
assert.Equal(t, 10, len(tags))
481+
assert.Equal(t, "test-label", tags["projectId"])
482+
assert.Equal(t, "test-container", tags[core.LabelContainerName.Key])
483+
assert.Equal(t, "test-podid", tags[core.LabelPodId.Key])
484+
assert.Equal(t, "test-container/test/metric/A", tags["group_id"])
485+
assert.Equal(t, "test/metric/A", tags["descriptor_name"])
486+
assert.Equal(t, "XYZ", tags[core.LabelResourceID.Key])
487+
assert.Equal(t, "bytes", tags["units"])
488+
489+
assert.Equal(t, "testLabelA:testValueA,testLabelB:testValueB", tags[core.LabelLabels.Key])
490+
assert.Equal(t, "testValueA", tags["labels.testLabelA"])
491+
assert.Equal(t, "testValueB", tags["labels.testLabelB"])
492+
493+
tagsUpdated++
494+
}
495+
}))
496+
defer s.Close()
497+
498+
hSink, err := integSink(s.URL + "?tenant=test-heapster&labelToTenant=projectId")
499+
assert.NoError(t, err)
500+
501+
l := make(map[string]string)
502+
l["projectId"] = "test-label"
503+
l[core.LabelContainerName.Key] = "test-container"
504+
l[core.LabelPodId.Key] = "test-podid"
505+
l[core.LabelLabels.Key] = "testLabelA:testValueA,testLabelB:testValueB"
506+
507+
labeledMetric := core.LabeledMetric{
508+
Name: "test/metric/A",
509+
Labels: map[string]string{
510+
core.LabelResourceID.Key: "XYZ",
511+
},
512+
MetricValue: core.MetricValue{
513+
MetricType: core.MetricGauge,
514+
FloatValue: 124.456,
515+
},
516+
}
517+
518+
metricSet := core.MetricSet{
519+
Labels: l,
520+
LabeledMetrics: []core.LabeledMetric{labeledMetric},
521+
MetricValues: map[string]core.MetricValue{
522+
"test/metric/A": {
523+
ValueType: core.ValueInt64,
524+
MetricType: core.MetricCumulative,
525+
IntValue: 123456,
526+
},
527+
},
528+
}
529+
530+
smd := core.MetricDescriptor{
531+
Name: "test/metric/A",
532+
Units: core.UnitsBytes,
533+
ValueType: core.ValueInt64,
534+
Type: core.MetricGauge,
535+
Labels: []core.LabelDescriptor{},
536+
}
537+
538+
//register the metric definitions
539+
hSink.Register([]core.MetricDescriptor{smd})
540+
//register the metrics themselves
541+
hSink.registerLabeledIfNecessary(&metricSet, labeledMetric)
542+
543+
assert.Equal(t, 1, tagsUpdated)
544+
545+
tags := hSink.reg["test-container/test-podid/test/metric/A/XYZ"].Tags
546+
assert.Equal(t, 10, len(tags))
547+
assert.Equal(t, "test-label", tags["projectId"])
548+
assert.Equal(t, "test-container", tags[core.LabelContainerName.Key])
549+
assert.Equal(t, "test-podid", tags[core.LabelPodId.Key])
550+
assert.Equal(t, "test-container/test/metric/A", tags["group_id"])
551+
assert.Equal(t, "test/metric/A", tags["descriptor_name"])
552+
assert.Equal(t, "XYZ", tags[core.LabelResourceID.Key])
553+
assert.Equal(t, "bytes", tags["units"])
554+
555+
assert.Equal(t, "testLabelA:testValueA,testLabelB:testValueB", tags[core.LabelLabels.Key])
556+
assert.Equal(t, "testValueA", tags["labels.testLabelA"])
557+
assert.Equal(t, "testValueB", tags["labels.testLabelB"])
558+
}
559+
460560
func TestUserPass(t *testing.T) {
461561
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
462562
w.Header().Set("X-Authorization", r.Header.Get("Authorization"))

metrics/sinks/hawkular/types.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,11 @@ type hawkularSink struct {
5353

5454
uri *url.URL
5555

56-
labelTenant string
57-
labelNodeId string
58-
modifiers []metrics.Modifier
59-
filters []Filter
56+
labelTenant string
57+
labelNodeId string
58+
labelTagPrefix string
59+
modifiers []metrics.Modifier
60+
filters []Filter
6061

6162
batchSize int
6263
}

0 commit comments

Comments
 (0)