-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
consul.go
83 lines (67 loc) · 2.43 KB
/
consul.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
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package consul // import "github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/consul"
import (
"context"
"fmt"
"github.com/hashicorp/consul/api"
"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/consul"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/consul/internal/metadata"
)
const (
// TypeStr is type of detector.
TypeStr = "consul"
)
var _ internal.Detector = (*Detector)(nil)
// Detector is a system metadata detector
type Detector struct {
provider consul.Provider
logger *zap.Logger
rb *metadata.ResourceBuilder
}
// NewDetector creates a new system metadata detector
func NewDetector(p processor.CreateSettings, dcfg internal.DetectorConfig) (internal.Detector, error) {
userCfg := dcfg.(Config)
cfg := api.DefaultConfig()
if userCfg.Address != "" {
cfg.Address = userCfg.Address
}
if userCfg.Datacenter != "" {
cfg.Datacenter = userCfg.Datacenter
}
if userCfg.Namespace != "" {
cfg.Namespace = userCfg.Namespace
}
if userCfg.Token != "" {
cfg.Token = string(userCfg.Token)
}
if userCfg.TokenFile != "" {
cfg.Token = userCfg.TokenFile
}
client, err := api.NewClient(cfg)
if err != nil {
return nil, fmt.Errorf("failed creating consul client: %w", err)
}
provider := consul.NewProvider(client, userCfg.MetaLabels)
return &Detector{provider: provider, logger: p.Logger, rb: metadata.NewResourceBuilder(userCfg.ResourceAttributes)}, nil
}
// Detect detects system metadata and returns a resource with the available ones
func (d *Detector) Detect(ctx context.Context) (resource pcommon.Resource, schemaURL string, err error) {
md, err := d.provider.Metadata(ctx)
if err != nil {
return pcommon.NewResource(), "", fmt.Errorf("failed to get consul metadata: %w", err)
}
d.rb.SetHostName(md.Hostname)
d.rb.SetCloudRegion(md.Datacenter)
d.rb.SetHostID(md.NodeID)
res := d.rb.Emit()
for key, element := range md.HostMetadata {
res.Attributes().PutStr(key, element)
}
return res, conventions.SchemaURL, nil
}