Skip to content

Commit

Permalink
[AzureMonitorExporter] fix SDK version (#37807)
Browse files Browse the repository at this point in the history
* fix SDK version

* guard against excessively length

* changelog

* add truncate

* changelog
  • Loading branch information
TimothyMothra authored Jul 25, 2023
1 parent 902abfb commit c12f465
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 9 deletions.
3 changes: 3 additions & 0 deletions sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

### Bugs Fixed

* Fixed an issue causing no telemetry if SDK Version string exceeds max length.
([#37807](https://github.com/Azure/azure-sdk-for-net/pull/37807))

### Other Changes

## 1.0.0-beta.13 (2023-07-13)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public TelemetryItem(string name, TelemetryItem telemetryItem, ActivitySpanId ac

Tags[ContextTagKeys.AiCloudRole.ToString()] = telemetryItem.Tags[ContextTagKeys.AiCloudRole.ToString()];
Tags[ContextTagKeys.AiCloudRoleInstance.ToString()] = telemetryItem.Tags[ContextTagKeys.AiCloudRoleInstance.ToString()];
Tags[ContextTagKeys.AiInternalSdkVersion.ToString()] = SdkVersionUtils.s_sdkVersion;
Tags[ContextTagKeys.AiInternalSdkVersion.ToString()] = SdkVersionUtils.s_sdkVersion.Truncate(SchemaConstants.Tags_AiInternalSdkVersion_MaxLength);
InstrumentationKey = telemetryItem.InstrumentationKey;

if (telemetryItem.SampleRate != 100f)
Expand Down Expand Up @@ -118,7 +118,7 @@ private void SetResourceSdkVersionAndIkey(AzureMonitorResource? resource, string
InstrumentationKey = instrumentationKey;
Tags[ContextTagKeys.AiCloudRole.ToString()] = resource?.RoleName;
Tags[ContextTagKeys.AiCloudRoleInstance.ToString()] = resource?.RoleInstance;
Tags[ContextTagKeys.AiInternalSdkVersion.ToString()] = SdkVersionUtils.s_sdkVersion;
Tags[ContextTagKeys.AiInternalSdkVersion.ToString()] = SdkVersionUtils.s_sdkVersion.Truncate(SchemaConstants.Tags_AiInternalSdkVersion_MaxLength);
}

internal static DateTimeOffset FormatUtcTimestamp(System.DateTime utcTimestamp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,5 +352,8 @@ public void FailedToDeserializeIngestionResponse(Exception ex)

[Event(35, Message = "Failed to deserialize response from ingestion due to an exception. Not user actionable. {0}", Level = EventLevel.Warning)]
public void FailedToDeserializeIngestionResponse(string exceptionMessage) => WriteEvent(35, exceptionMessage);

[Event(36, Message = "Version string exceeds expected length. This is only for internal telemetry and can safely be ignored. Type Name: {0}. Version: {1}", Level = EventLevel.Verbose)]
public void VersionStringUnexpectedLength(string typeName, string value) => WriteEvent(36, typeName, value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,7 @@ internal static class SchemaConstants
public const int TelemetryEnvelope_Name_MaxLength = 1024;
public const int TelemetryEnvelope_Time_MaxLength = 64;
public const int TelemetryEnvelope_InstrumentationKey_MaxLength = 40;

public const int Tags_AiInternalSdkVersion_MaxLength = 64;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,21 @@ internal static string? SdkVersionPrefix
try
{
string versionString = type
.Assembly
.GetCustomAttributes<AssemblyInformationalVersionAttribute>()
.First()
.InformationalVersion;
.Assembly
.GetCustomAttributes<AssemblyInformationalVersionAttribute>()
.First()
.InformationalVersion;

// Informational version will be something like 1.1.0-beta2+a25741030f05c60c85be102ce7c33f3899290d49.
// Ignoring part after '+' if it is present.
string? shortVersion = versionString?.Split('+')[0];
// Informational version may contain extra information.
// 1) "1.1.0-beta2+a25741030f05c60c85be102ce7c33f3899290d49". Ignoring part after '+' if it is present.
// 2) "4.6.30411.01 @BuiltBy: XXXXXX @Branch: XXXXXX @srccode: XXXXXX XXXXXX" Ignoring part after '@' if it is present.
string shortVersion = versionString.Split('+', '@', ' ')[0];

if (shortVersion.Length > 20)
{
AzureMonitorExporterEventSource.Log.VersionStringUnexpectedLength(type.Name, versionString);
return shortVersion.Substring(0, 20);
}

return shortVersion;
}
Expand Down

0 comments on commit c12f465

Please sign in to comment.