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

TelemetryItem.Time format to keep time precision #20476

Merged
merged 4 commits into from
Jun 18, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace Azure.Monitor.OpenTelemetry.Exporter
/// </summary>
internal class TelemetryPartA
{
private const string DateTimeFormat = "yyyy-MM-ddTHH:mm:ss.fffffffZ";
private static readonly IReadOnlyDictionary<TelemetryType, string> PartA_Name_Mapping = new Dictionary<TelemetryType, string>
{
[TelemetryType.Request] = "Request",
Expand All @@ -32,7 +33,7 @@ internal class TelemetryPartA

internal static TelemetryItem GetTelemetryItem(Activity activity, Resource resource, string instrumentationKey)
{
TelemetryItem telemetryItem = new TelemetryItem(PartA_Name_Mapping[activity.GetTelemetryType()], activity.StartTimeUtc.ToString(CultureInfo.InvariantCulture))
TelemetryItem telemetryItem = new TelemetryItem(PartA_Name_Mapping[activity.GetTelemetryType()], FormatTimestamp(activity.StartTimeUtc))
{
InstrumentationKey = instrumentationKey
};
Expand All @@ -55,7 +56,7 @@ internal static TelemetryItem GetTelemetryItem(Activity activity, Resource resou
internal static TelemetryItem GetTelemetryItem(LogRecord logRecord, string instrumentationKey)
{
var name = PartA_Name_Mapping[TelemetryType.Message];
var time = logRecord.Timestamp.ToString(CultureInfo.InvariantCulture);
var time = FormatTimestamp(logRecord.Timestamp);

TelemetryItem telemetryItem = new TelemetryItem(name, time)
{
Expand Down Expand Up @@ -122,5 +123,10 @@ internal static void InitRoleInfo(Resource resource)
RoleName = serviceName;
}
}

internal static string FormatTimestamp(System.DateTime timeStamp)
mangeg marked this conversation as resolved.
Show resolved Hide resolved
{
return timeStamp.ToString(DateTimeFormat, CultureInfo.InvariantCulture);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public void GeneratePartAEnvelope_DefaultActivity_DefaultResource()
var telemetryItem = TelemetryPartA.GetTelemetryItem(activity, resource, null);

Assert.Equal("RemoteDependency", telemetryItem.Name);
Assert.Equal(activity.StartTimeUtc.ToString(CultureInfo.InvariantCulture), telemetryItem.Time);
Assert.Equal(TelemetryPartA.FormatTimestamp(activity.StartTimeUtc), telemetryItem.Time);
Assert.StartsWith("unknown_service", telemetryItem.Tags[ContextTagKeys.AiCloudRole.ToString()]);
Assert.Null(telemetryItem.Tags[ContextTagKeys.AiCloudRoleInstance.ToString()]);
Assert.NotNull(telemetryItem.Tags[ContextTagKeys.AiOperationId.ToString()]);
Expand All @@ -133,7 +133,7 @@ public void GeneratePartAEnvelope_Activity_WithResource()
var telemetryItem = TelemetryPartA.GetTelemetryItem(activity, resource, null);

Assert.Equal("RemoteDependency", telemetryItem.Name);
Assert.Equal(activity.StartTimeUtc.ToString(CultureInfo.InvariantCulture), telemetryItem.Time);
Assert.Equal(TelemetryPartA.FormatTimestamp(activity.StartTimeUtc), telemetryItem.Time);
Assert.Equal("my-service", telemetryItem.Tags[ContextTagKeys.AiCloudRole.ToString()]);
Assert.Equal("my-instance", telemetryItem.Tags[ContextTagKeys.AiCloudRoleInstance.ToString()]);
Assert.Equal(activity.TraceId.ToHexString(), telemetryItem.Tags[ContextTagKeys.AiOperationId.ToString()]);
Expand Down