-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathk8snode.go
70 lines (59 loc) · 2.12 KB
/
k8snode.go
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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package k8snode // import "github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/k8snode"
import (
"context"
"fmt"
"os"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/processor"
conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
"go.uber.org/zap"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/metadataproviders/k8snode"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/k8snode/internal/metadata"
)
const (
TypeStr = "k8snode"
)
var _ internal.Detector = (*detector)(nil)
type detector struct {
provider k8snode.Provider
logger *zap.Logger
ra *metadata.ResourceAttributesConfig
rb *metadata.ResourceBuilder
}
func NewDetector(set processor.Settings, dcfg internal.DetectorConfig) (internal.Detector, error) {
cfg := dcfg.(Config)
if err := cfg.UpdateDefaults(); err != nil {
return nil, err
}
nodeName := os.Getenv(cfg.NodeFromEnvVar)
k8snodeProvider, err := k8snode.NewProvider(nodeName, cfg.APIConfig)
if err != nil {
return nil, fmt.Errorf("failed creating k8snode detector: %w", err)
}
return &detector{
provider: k8snodeProvider,
logger: set.Logger,
ra: &cfg.ResourceAttributes,
rb: metadata.NewResourceBuilder(cfg.ResourceAttributes),
}, nil
}
func (d *detector) Detect(ctx context.Context) (resource pcommon.Resource, schemaURL string, err error) {
if d.ra.K8sNodeUID.Enabled {
nodeUID, err := d.provider.NodeUID(ctx)
if err != nil {
return pcommon.NewResource(), "", fmt.Errorf("failed getting k8s node UID: %w", err)
}
d.rb.SetK8sNodeUID(nodeUID)
}
if d.ra.K8sNodeName.Enabled {
nodeName, err := d.provider.NodeName(ctx)
if err != nil {
return pcommon.NewResource(), "", fmt.Errorf("failed getting k8s node name: %w", err)
}
d.rb.SetK8sNodeName(nodeName)
}
return d.rb.Emit(), conventions.SchemaURL, nil
}