-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathazure.go
118 lines (100 loc) · 3.44 KB
/
azure.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
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
111
112
113
114
115
116
117
118
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package azure // import "github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/azure"
import (
"context"
"regexp"
"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/azure"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/azure/internal/metadata"
)
const (
// TypeStr is type of detector.
TypeStr = "azure"
tagPrefix = "azure.tag."
)
var _ internal.Detector = (*Detector)(nil)
// Detector is an Azure metadata detector
type Detector struct {
provider azure.Provider
tagKeyRegexes []*regexp.Regexp
logger *zap.Logger
rb *metadata.ResourceBuilder
}
// NewDetector creates a new Azure metadata detector
func NewDetector(p processor.Settings, dcfg internal.DetectorConfig) (internal.Detector, error) {
cfg := dcfg.(Config)
tagKeyRegexes, err := compileRegexes(cfg)
if err != nil {
return nil, err
}
return &Detector{
provider: azure.NewProvider(),
tagKeyRegexes: tagKeyRegexes,
logger: p.Logger,
rb: metadata.NewResourceBuilder(cfg.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) {
compute, err := d.provider.Metadata(ctx)
if err != nil {
d.logger.Debug("Azure detector metadata retrieval failed", zap.Error(err))
// return an empty Resource and no error
return pcommon.NewResource(), "", nil
}
d.rb.SetCloudProvider(conventions.AttributeCloudProviderAzure)
d.rb.SetCloudPlatform(conventions.AttributeCloudPlatformAzureVM)
d.rb.SetHostName(compute.Name)
d.rb.SetCloudRegion(compute.Location)
d.rb.SetHostID(compute.VMID)
d.rb.SetCloudAccountID(compute.SubscriptionID)
// Also save compute.Name in "azure.vm.name" as host.id (AttributeHostName) is
// used by system detector.
d.rb.SetAzureVMName(compute.Name)
d.rb.SetAzureVMSize(compute.VMSize)
d.rb.SetAzureVMScalesetName(compute.VMScaleSetName)
d.rb.SetAzureResourcegroupName(compute.ResourceGroupName)
res := d.rb.Emit()
if len(d.tagKeyRegexes) != 0 {
tags := matchAzureTags(compute.TagsList, d.tagKeyRegexes)
for key, val := range tags {
res.Attributes().PutStr(tagPrefix+key, val)
}
}
return res, conventions.SchemaURL, nil
}
func matchAzureTags(azureTags []azure.ComputeTagsListMetadata, tagKeyRegexes []*regexp.Regexp) map[string]string {
tags := make(map[string]string)
for _, tag := range azureTags {
matched := regexArrayMatch(tagKeyRegexes, tag.Name)
if matched {
tags[tag.Name] = tag.Value
}
}
return tags
}
func compileRegexes(cfg Config) ([]*regexp.Regexp, error) {
tagRegexes := make([]*regexp.Regexp, len(cfg.Tags))
for i, elem := range cfg.Tags {
regex, err := regexp.Compile(elem)
if err != nil {
return nil, err
}
tagRegexes[i] = regex
}
return tagRegexes, nil
}
func regexArrayMatch(arr []*regexp.Regexp, val string) bool {
for _, elem := range arr {
matched := elem.MatchString(val)
if matched {
return true
}
}
return false
}