-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathcontracts_utils.go
70 lines (57 loc) · 2.88 KB
/
contracts_utils.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 azuremonitorexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/azuremonitorexporter"
import (
"github.com/microsoft/ApplicationInsights-Go/appinsights/contracts"
"go.opentelemetry.io/collector/pdata/pcommon" // Applies resource attributes values to data properties
conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
)
const (
instrumentationLibraryName string = "instrumentationlibrary.name"
instrumentationLibraryVersion string = "instrumentationlibrary.version"
)
// Applies resource attributes values to data properties
func applyResourcesToDataProperties(dataProperties map[string]string, resourceAttributes pcommon.Map) {
// Copy all the resource labels into the base data properties. Resource values are always strings
resourceAttributes.Range(func(k string, v pcommon.Value) bool {
dataProperties[k] = v.Str()
return true
})
}
// Sets important ai.cloud.* tags on the envelope
func applyCloudTagsToEnvelope(envelope *contracts.Envelope, resourceAttributes pcommon.Map) {
// Extract key service.* labels from the Resource labels and construct CloudRole and CloudRoleInstance envelope tags
// https://github.com/open-telemetry/opentelemetry-specification/tree/main/specification/resource/semantic_conventions
if serviceName, serviceNameExists := resourceAttributes.Get(conventions.AttributeServiceName); serviceNameExists {
cloudRole := serviceName.Str()
if serviceNamespace, serviceNamespaceExists := resourceAttributes.Get(conventions.AttributeServiceNamespace); serviceNamespaceExists {
cloudRole = serviceNamespace.Str() + "." + cloudRole
}
envelope.Tags[contracts.CloudRole] = cloudRole
}
if serviceInstance, exists := resourceAttributes.Get(conventions.AttributeServiceInstanceID); exists {
envelope.Tags[contracts.CloudRoleInstance] = serviceInstance.Str()
}
envelope.Tags[contracts.InternalSdkVersion] = getCollectorVersion()
}
// Applies internal sdk version tag on the envelope
func applyInternalSdkVersionTagToEnvelope(envelope *contracts.Envelope) {
envelope.Tags[contracts.InternalSdkVersion] = getCollectorVersion()
}
// Applies instrumentation values to data properties
func applyInstrumentationScopeValueToDataProperties(dataProperties map[string]string, instrumentationScope pcommon.InstrumentationScope) {
// Copy the instrumentation properties
if instrumentationScope.Name() != "" {
dataProperties[instrumentationLibraryName] = instrumentationScope.Name()
}
if instrumentationScope.Version() != "" {
dataProperties[instrumentationLibraryVersion] = instrumentationScope.Version()
}
}
// Applies attributes as Application Insights properties
func setAttributesAsProperties(attributeMap pcommon.Map, properties map[string]string) {
attributeMap.Range(func(k string, v pcommon.Value) bool {
properties[k] = v.AsString()
return true
})
}