Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/flow/internal/stdlib/stdlib.go
4096 views
1
// Package stdlib contains Flow-specific standard library functions exposed to
2
// River configs.
3
package stdlib
4
5
import (
6
"encoding/json"
7
8
"github.com/grafana/agent/component/discovery"
9
"github.com/prometheus/prometheus/discovery/targetgroup"
10
)
11
12
// Identifiers holds a list of stdlib identifiers by name. All interface{}
13
// values are River-compatible values.
14
//
15
// Function identifiers are Go functions with exactly one non-error return
16
// value, with an optionally supported error return value as the second return
17
// value.
18
var Identifiers = map[string]interface{}{
19
"discovery_target_decode": func(in string) (interface{}, error) {
20
var targetGroups []*targetgroup.Group
21
if err := json.Unmarshal([]byte(in), &targetGroups); err != nil {
22
return nil, err
23
}
24
25
var res []discovery.Target
26
27
for _, group := range targetGroups {
28
for _, target := range group.Targets {
29
30
// Create the output target from group and target labels. Target labels
31
// should override group labels.
32
outputTarget := make(discovery.Target, len(group.Labels)+len(target))
33
for k, v := range group.Labels {
34
outputTarget[string(k)] = string(v)
35
}
36
for k, v := range target {
37
outputTarget[string(k)] = string(v)
38
}
39
40
res = append(res, outputTarget)
41
}
42
}
43
44
return res, nil
45
},
46
}
47
48