Skip to content

Commit

Permalink
[chore] [exporter/azuremonitor] Incorporate SDK Version Tagging in Az…
Browse files Browse the repository at this point in the history
…ure Monitor Exporter (#28999)

**Description:** <Describe what has changed.>
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->

This pull request enhances the Azure Monitor Exporter by tagging
telemetry data with the OpenTelemetry Collector's internal SDK version.
This additional metadata will facilitate improved monitoring,
troubleshooting, and analysis of data sourced from different versions of
the collector.

**Changes:**
- Added functionality in `azuremonitorexporter` to append the SDK
version to each telemetry envelope.
- Implemented `getCollectorVersion` to dynamically fetch the current
version of the OpenTelemetry Collector.

**Sample SDK Version String:**
- The SDK version string is formatted as follows:
`otelc-<version>-<os>-<arch>`
- Example: `otelc-v0.88.1-lin-amd64`

**Note:**
- Since this change pertains to internal diagnostic data enrichment and
does not modify any external behavior or configuration interfaces, it
does not necessitate a changelog entry.

**Link to tracking Issue:** <Issue number if applicable>

**Testing:** <Describe what testing was performed and which tests were
added.>
- Adjusted existing unit tests to ensure the SDK version string is
correctly included in the telemetry envelopes.

```json
{
	"ver": 1,
	"name": "Microsoft.ApplicationInsights.RemoteDependency",
	"time": "2023-11-06T15:21:12.9807976-08:00",
	"sampleRate": 100,
	"seq": "",
	"iKey": "Ikey",
	"tags": {
		"ai.cloud.role": "otlp-test",
		"ai.cloud.roleInstance": "2c4a6d1b-f7cb-4579-8d05-65b83b4acf7e",
		*"ai.internal.sdkVersion": "otelc-v0.88.1-lin-amd64",*
		"ai.operation.id": "0e3a010cb64d919aa8f214bf0a166334",
		"ai.operation.parentId": ""
	},
	"data": {
	}
}
```

---------

Co-authored-by: Yang Song <[email protected]>
  • Loading branch information
rajkumar-rangaraj and songy23 authored Nov 14, 2023
1 parent 30a5f14 commit 482cc7d
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 0 deletions.
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
}

0 comments on commit 482cc7d

Please sign in to comment.