-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathidentifier.go
More file actions
60 lines (48 loc) · 1.26 KB
/
identifier.go
File metadata and controls
60 lines (48 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package helper
import (
"github.com/observiq/stanza/entry"
)
// NewIdentifierConfig creates a new identifier config with default values
func NewIdentifierConfig() IdentifierConfig {
return IdentifierConfig{
Resource: make(map[string]ExprStringConfig),
}
}
// IdentifierConfig is the configuration of a resource identifier
type IdentifierConfig struct {
Resource map[string]ExprStringConfig `json:"resource" yaml:"resource"`
}
// Build will build an identifier from the supplied configuration
func (c IdentifierConfig) Build() (Identifier, error) {
identifier := Identifier{
resource: make(map[string]*ExprString),
}
for k, v := range c.Resource {
exprString, err := v.Build()
if err != nil {
return identifier, err
}
identifier.resource[k] = exprString
}
return identifier, nil
}
// Identifier is a helper that adds values to the resource of an entry
type Identifier struct {
resource map[string]*ExprString
}
// Identify will add values to the resource of an entry
func (i *Identifier) Identify(e *entry.Entry) error {
if len(i.resource) == 0 {
return nil
}
env := GetExprEnv(e)
defer PutExprEnv(env)
for k, v := range i.resource {
rendered, err := v.Render(env)
if err != nil {
return err
}
e.AddResourceKey(k, rendered)
}
return nil
}