From 5a5d368acba5a975947c1bb4820db5eabae7636f Mon Sep 17 00:00:00 2001 From: Magnus Gideryd Date: Sat, 17 Apr 2021 01:12:30 +0200 Subject: [PATCH 1/4] TelemetryItem.Time format to keep time precision --- .../src/TelemetryPartA.cs | 10 ++++++++-- .../TelemetryPartATests.cs | 4 ++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/TelemetryPartA.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/TelemetryPartA.cs index 755ea04dd5915..b156160587f46 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/TelemetryPartA.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/TelemetryPartA.cs @@ -18,6 +18,7 @@ namespace Azure.Monitor.OpenTelemetry.Exporter /// internal class TelemetryPartA { + private const string DateTimeFormat = "yyyy-MM-dd HH:mm:ss.ffffZ"; private static readonly IReadOnlyDictionary PartA_Name_Mapping = new Dictionary { [TelemetryType.Request] = "Request", @@ -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 }; @@ -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) { @@ -122,5 +123,10 @@ internal static void InitRoleInfo(Resource resource) RoleName = serviceName; } } + + internal static string FormatTimestamp(System.DateTime timeStamp) + { + return timeStamp.ToString(DateTimeFormat, CultureInfo.InvariantCulture); + } } } diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/TelemetryPartATests.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/TelemetryPartATests.cs index c35cbd499fc83..a7c862d61eb26 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/TelemetryPartATests.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/TelemetryPartATests.cs @@ -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()]); @@ -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()]); From 2c2adf41ae8607a049697cda5e1ae3645a277888 Mon Sep 17 00:00:00 2001 From: Magnus Gideryd Date: Fri, 18 Jun 2021 22:10:32 +0200 Subject: [PATCH 2/4] Fix DateTimeFormat to follow UTC ISO 8601 --- .../Azure.Monitor.OpenTelemetry.Exporter/src/TelemetryPartA.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/TelemetryPartA.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/TelemetryPartA.cs index b156160587f46..ab46a9b6dfd4c 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/TelemetryPartA.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/TelemetryPartA.cs @@ -18,7 +18,7 @@ namespace Azure.Monitor.OpenTelemetry.Exporter /// internal class TelemetryPartA { - private const string DateTimeFormat = "yyyy-MM-dd HH:mm:ss.ffffZ"; + private const string DateTimeFormat = "yyyy-MM-ddTHH:mm:ss.fffffffZ"; private static readonly IReadOnlyDictionary PartA_Name_Mapping = new Dictionary { [TelemetryType.Request] = "Request", From 4ce98d02a3717fec6b626360c89e3a1a0f637e91 Mon Sep 17 00:00:00 2001 From: Magnus Gideryd Date: Fri, 18 Jun 2021 22:26:06 +0200 Subject: [PATCH 3/4] Rename DateTime format function Co-authored-by: Reiley Yang --- .../Azure.Monitor.OpenTelemetry.Exporter/src/TelemetryPartA.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/TelemetryPartA.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/TelemetryPartA.cs index ab46a9b6dfd4c..c24ba7a90dc0d 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/TelemetryPartA.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/TelemetryPartA.cs @@ -124,7 +124,7 @@ internal static void InitRoleInfo(Resource resource) } } - internal static string FormatTimestamp(System.DateTime timeStamp) + internal static string FormatUtcTimestamp(System.DateTime utcTimeStamp) { return timeStamp.ToString(DateTimeFormat, CultureInfo.InvariantCulture); } From defab787ebeb0f31f917690b7ddf8a9ce6613dc8 Mon Sep 17 00:00:00 2001 From: Magnus Gideryd Date: Fri, 18 Jun 2021 22:30:46 +0200 Subject: [PATCH 4/4] Rename parameter to FormatUtcTimestamp Co-authored-by: Reiley Yang --- .../src/TelemetryPartA.cs | 8 ++++---- .../TelemetryPartATests.cs | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/TelemetryPartA.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/TelemetryPartA.cs index c24ba7a90dc0d..39b2e405a0460 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/TelemetryPartA.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/TelemetryPartA.cs @@ -33,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()], FormatTimestamp(activity.StartTimeUtc)) + TelemetryItem telemetryItem = new TelemetryItem(PartA_Name_Mapping[activity.GetTelemetryType()], FormatUtcTimestamp(activity.StartTimeUtc)) { InstrumentationKey = instrumentationKey }; @@ -56,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 = FormatTimestamp(logRecord.Timestamp); + var time = FormatUtcTimestamp(logRecord.Timestamp); TelemetryItem telemetryItem = new TelemetryItem(name, time) { @@ -124,9 +124,9 @@ internal static void InitRoleInfo(Resource resource) } } - internal static string FormatUtcTimestamp(System.DateTime utcTimeStamp) + internal static string FormatUtcTimestamp(System.DateTime utcTimestamp) { - return timeStamp.ToString(DateTimeFormat, CultureInfo.InvariantCulture); + return utcTimestamp.ToString(DateTimeFormat, CultureInfo.InvariantCulture); } } } diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/TelemetryPartATests.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/TelemetryPartATests.cs index a7c862d61eb26..45f7102f20388 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/TelemetryPartATests.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/TelemetryPartATests.cs @@ -116,7 +116,7 @@ public void GeneratePartAEnvelope_DefaultActivity_DefaultResource() var telemetryItem = TelemetryPartA.GetTelemetryItem(activity, resource, null); Assert.Equal("RemoteDependency", telemetryItem.Name); - Assert.Equal(TelemetryPartA.FormatTimestamp(activity.StartTimeUtc), telemetryItem.Time); + Assert.Equal(TelemetryPartA.FormatUtcTimestamp(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()]); @@ -133,7 +133,7 @@ public void GeneratePartAEnvelope_Activity_WithResource() var telemetryItem = TelemetryPartA.GetTelemetryItem(activity, resource, null); Assert.Equal("RemoteDependency", telemetryItem.Name); - Assert.Equal(TelemetryPartA.FormatTimestamp(activity.StartTimeUtc), telemetryItem.Time); + Assert.Equal(TelemetryPartA.FormatUtcTimestamp(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()]);