Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[chore] [exporter/azuremonitor] Incorporate SDK Version Tagging in Azure Monitor Exporter #28999

Merged
merged 5 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions exporter/azuremonitorexporter/contracts_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ func applyCloudTagsToEnvelope(envelope *contracts.Envelope, resourceAttributes p
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
Expand Down
1 change: 1 addition & 0 deletions exporter/azuremonitorexporter/log_to_envelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func (packer *logPacker) LogRecordToEnvelope(logRecord plog.LogRecord, resource
applyResourcesToDataProperties(messageData.Properties, resourceAttributes)
applyInstrumentationScopeValueToDataProperties(messageData.Properties, instrumentationScope)
applyCloudTagsToEnvelope(envelope, resourceAttributes)
applyInternalSdkVersionTagToEnvelope(envelope)

setAttributesAsProperties(logRecord.Attributes(), messageData.Properties)

Expand Down
1 change: 1 addition & 0 deletions exporter/azuremonitorexporter/logexporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ func TestLogRecordToEnvelope(t *testing.T) {
assert.Equal(t, contracts.Information, messageData.SeverityLevel)
assert.Equal(t, defaultTraceIDAsHex, envelope.Tags[contracts.OperationId])
assert.Equal(t, defaultSpanIDAsHex, envelope.Tags[contracts.OperationParentId])
assert.Contains(t, envelope.Tags[contracts.InternalSdkVersion], "otelc-")
})
}
}
Expand Down
1 change: 1 addition & 0 deletions exporter/azuremonitorexporter/metric_to_envelopes.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func (packer *metricPacker) MetricToEnvelopes(metric pmetric.Metric, resource pc
applyResourcesToDataProperties(metricData.Properties, resourceAttributes)
applyInstrumentationScopeValueToDataProperties(metricData.Properties, instrumentationScope)
applyCloudTagsToEnvelope(envelope, resourceAttributes)
applyInternalSdkVersionTagToEnvelope(envelope)

setAttributesAsProperties(timedDataPoint.attributes, metricData.Properties)

Expand Down
1 change: 1 addition & 0 deletions exporter/azuremonitorexporter/metricexporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ func getDataPoint(t testing.TB, metric pmetric.Metric) *contracts.DataPoint {
require.NotNil(t, envelope)

assert.NotNil(t, envelope.Tags)
assert.Contains(t, envelope.Tags[contracts.InternalSdkVersion], "otelc-")
assert.NotNil(t, envelope.Time)

require.NotNil(t, envelope.Data)
Expand Down
2 changes: 2 additions & 0 deletions exporter/azuremonitorexporter/trace_to_envelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ func spanToEnvelopes(
applyResourcesToDataProperties(dataProperties, resourceAttributes)
applyInstrumentationScopeValueToDataProperties(dataProperties, instrumentationScope)
applyCloudTagsToEnvelope(envelope, resourceAttributes)
applyInternalSdkVersionTagToEnvelope(envelope)

// Sanitize the base data, the envelope and envelope tags
sanitize(dataSanitizeFunc, logger)
Expand Down Expand Up @@ -158,6 +159,7 @@ func spanToEnvelopes(
applyResourcesToDataProperties(dataProperties, resourceAttributes)
applyInstrumentationScopeValueToDataProperties(dataProperties, instrumentationScope)
applyCloudTagsToEnvelope(spanEventEnvelope, resourceAttributes)
applyInternalSdkVersionTagToEnvelope(envelope)

// Sanitize the base data, the envelope and envelope tags
sanitize(dataSanitizeFunc, logger)
Expand Down
1 change: 1 addition & 0 deletions exporter/azuremonitorexporter/trace_to_envelope_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,7 @@ func commonEnvelopeValidations(
assert.Equal(t, defaultParentSpanIDAsHex, envelope.Tags[contracts.OperationParentId])
assert.Equal(t, defaultServiceNamespace+"."+defaultServiceName, envelope.Tags[contracts.CloudRole])
assert.Equal(t, defaultServiceInstance, envelope.Tags[contracts.CloudRoleInstance])
assert.Contains(t, envelope.Tags[contracts.InternalSdkVersion], "otelc-")
assert.NotNil(t, envelope.Data)

if expectedEnvelopeName == defaultRequestDataEnvelopeName {
Expand Down
42 changes: 42 additions & 0 deletions exporter/azuremonitorexporter/version_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package azuremonitorexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/azuremonitorexporter"

import (
"runtime"
"runtime/debug"
"strings"
"sync"
)

var (
once sync.Once
cachedVersion string
)

func getCollectorVersion() string {
once.Do(func() {
osInformation := runtime.GOOS[:3] + "-" + runtime.GOARCH
unknownVersion := "otelc-unknown-" + osInformation

info, ok := debug.ReadBuildInfo()
if !ok {
cachedVersion = unknownVersion
return
}

for _, mod := range info.Deps {
if mod.Path == "go.opentelemetry.io/collector" {
// Extract the semantic version without metadata.
semVer := strings.SplitN(mod.Version, "-", 2)[0]
cachedVersion = "otelc-" + semVer + "-" + osInformation
return
}
}

cachedVersion = unknownVersion
})

return cachedVersion
}