-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
azure.go
67 lines (55 loc) · 2.31 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
// 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"
"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"
)
var _ internal.Detector = (*Detector)(nil)
// Detector is an Azure metadata detector
type Detector struct {
provider azure.Provider
logger *zap.Logger
rb *metadata.ResourceBuilder
}
// NewDetector creates a new Azure metadata detector
func NewDetector(p processor.CreateSettings, dcfg internal.DetectorConfig) (internal.Detector, error) {
cfg := dcfg.(Config)
return &Detector{
provider: azure.NewProvider(),
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)
return d.rb.Emit(), conventions.SchemaURL, nil
}