-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessor.go
More file actions
110 lines (94 loc) · 2.62 KB
/
Copy pathprocessor.go
File metadata and controls
110 lines (94 loc) · 2.62 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package incusattrprocessor
import (
"context"
"errors"
"github.com/bmarinov/incusattrprocessor/internal/cgroup"
"github.com/bmarinov/incusattrprocessor/internal/incus"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pprofile"
"go.opentelemetry.io/collector/processor"
"go.uber.org/zap"
)
const (
attrInstanceName = "incus.instance.name"
attrInstanceProject = "incus.instance.project"
attrInstanceLocation = "incus.instance.location"
)
const attrPID = "process.pid"
type MetadataSource interface {
GetInstanceMetadata(ctx context.Context, id string) (incus.InstanceInfo, error)
}
func newIncusAttrProcessor(
params processor.Settings,
cfg *processorConfig,
meta MetadataSource,
startFn func(context.Context) error,
) *incusAttrProcessor {
return &incusAttrProcessor{
config: *cfg,
lookup: meta,
logger: params.Logger,
start: startFn,
}
}
type incusAttrProcessor struct {
cancel context.CancelFunc
config processorConfig
lookup MetadataSource
logger *zap.Logger
start func(ctx context.Context) error
}
func (p *incusAttrProcessor) processProfiles(ctx context.Context, pd pprofile.Profiles) (pprofile.Profiles, error) {
total, matched := 0, 0
for _, rp := range pd.ResourceProfiles().All() {
attrs := rp.Resource().Attributes()
pidVal, ok := attrs.Get(attrPID)
if !ok {
continue
}
total++
pid := pidVal.AsString()
meta, err := p.lookup.GetInstanceMetadata(ctx, pid)
if err != nil {
if !errors.Is(err, cgroup.ErrNotContainer) {
p.logger.Debug("metadata lookup failed", zap.String("pid", pid), zap.Error(err))
}
continue
}
matched++
p.logger.Debug("matched container", zap.String("pid", pid), zap.String("container", meta.Name))
setResourceAttr(attrs, attrInstanceName, meta.Name)
setResourceAttr(attrs, attrInstanceProject, meta.Project)
setResourceAttr(attrs, attrInstanceLocation, meta.Location)
}
if total > 0 {
p.logger.Debug("batch", zap.Int("matched", matched), zap.Int("total", total))
}
return pd, nil
}
func setResourceAttr(attrs pcommon.Map, key, val string) {
if val == "" {
return
}
if _, ok := attrs.Get(key); ok {
return
}
attrs.PutStr(key, val)
}
func (p *incusAttrProcessor) startup(ctx context.Context, _ component.Host) error {
background, cancel := context.WithCancel(context.WithoutCancel(ctx))
p.cancel = cancel
go func() {
if err := p.start(background); err != nil {
p.logger.Warn("incus processor startup", zap.Error(err))
}
}()
return nil
}
func (p *incusAttrProcessor) shutdown(_ context.Context) error {
if p.cancel != nil {
p.cancel()
}
return nil
}