Skip to content

Commit a5867ad

Browse files
Timothy Mothrarajkumar-rangaraj
andauthored
Azure Monitor Exporter: Upgrade OpenTelemetry to v1.0.1 (Azure#18673)
* update versions * changing activity.GetResource to exporter.ParentProvider.GetResource() * BatchExportProcessor<T> is now abstract. * change to packages * fix for BatchExportProcessor * fixing Resource tests * more fixes for Resource * Remove allocationless foreach, as OTel removed public reference for it. * update version * update comment Co-authored-by: Rajkumar Rangaraj <rajrang@microsoft.com>
1 parent 00c628c commit a5867ad

File tree

16 files changed

+148
-148
lines changed

16 files changed

+148
-148
lines changed

eng/Packages.Data.props

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,17 @@
8787
<PackageReference Update="Microsoft.Identity.Client.Extensions.Msal" Version="2.16.6" />
8888
<!-- TODO: Make sure this package is arch-board approved -->
8989
<PackageReference Update="System.IdentityModel.Tokens.Jwt" Version="5.4.0" />
90+
91+
<!-- OpenTelemetry dependency approved for Azure.Monitor.OpenTelemetry.Exporter package only -->
92+
<PackageReference Update="OpenTelemetry" Version="1.0.1" Condition="'$(MSBuildProjectName)' == 'Azure.Monitor.OpenTelemetry.Exporter'" />
9093
</ItemGroup>
9194

9295
<!--
9396
Dependency versions for Track 2, Microsoft.* libraries.
9497
These are dependencies for Track 2 integration packages
9598
-->
96-
9799
<ItemGroup Condition="'$(IsClientLibrary)' == 'true' and $(MSBuildProjectName.StartsWith('Microsoft.'))">
98100
<PackageReference Update="CloudNative.CloudEvents" Version="2.0.0-beta.1" />
99-
<PackageReference Update="OpenTelemetry" Version="0.8.0-beta.1" />
100101
<PackageReference Update="Microsoft.Azure.WebJobs" Version="3.0.25" />
101102
<PackageReference Update="Microsoft.Spatial" Version="7.5.3" />
102103
<PackageReference Update="Newtonsoft.Json" Version="10.0.3" />
@@ -194,9 +195,9 @@
194195
<PackageReference Update="NSubstitute" Version="3.1.0" />
195196
<PackageReference Update="NUnit" Version="3.12.0" />
196197
<PackageReference Update="NUnit3TestAdapter" Version="3.13.0" />
197-
<PackageReference Update="OpenTelemetry.Extensions.Hosting" Version="0.8.0-beta.1" />
198-
<PackageReference Update="OpenTelemetry.Instrumentation.AspNetCore" Version="0.8.0-beta.1" />
199-
<PackageReference Update="OpenTelemetry.Instrumentation.Http" Version="0.8.0-beta.1" />
198+
<PackageReference Update="OpenTelemetry.Extensions.Hosting" Version="1.0.0-rc2" />
199+
<PackageReference Update="OpenTelemetry.Instrumentation.AspNetCore" Version="1.0.0-rc2" />
200+
<PackageReference Update="OpenTelemetry.Instrumentation.Http" Version="1.0.0-rc2" />
200201
<PackageReference Update="Polly" Version="7.1.0" />
201202
<PackageReference Update="Portable.BouncyCastle" Version="1.8.5" />
202203
<PackageReference Update="PublicApiGenerator" Version="10.0.1" />

sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Azure.Monitor.OpenTelemetry.Exporter.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<ItemGroup>
1111
<PackageReference Include="System.Text.Json" />
12-
<PackageReference Include="OpenTelemetry" VersionOverride="0.8.0-beta.1"/>
12+
<PackageReference Include="OpenTelemetry" />
1313
</ItemGroup>
1414

1515
<!-- Shared source from Azure.Core -->

sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/AzureMonitorConverter.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
using OpenTelemetry;
1010
using OpenTelemetry.Logs;
11+
using OpenTelemetry.Resources;
1112

1213
namespace Azure.Monitor.OpenTelemetry.Exporter
1314
{
@@ -25,15 +26,15 @@ internal static class AzureMonitorConverter
2526
[TelemetryType.Event] = "EventData",
2627
};
2728

28-
internal static List<TelemetryItem> Convert(Batch<Activity> batchActivity, string instrumentationKey)
29+
internal static List<TelemetryItem> Convert(Batch<Activity> batchActivity, Resource resource, string instrumentationKey)
2930
{
3031
List<TelemetryItem> telemetryItems = new List<TelemetryItem>();
3132
TelemetryItem telemetryItem;
3233

3334
foreach (var activity in batchActivity)
3435
{
3536
MonitorBase telemetryData = new MonitorBase();
36-
telemetryItem = TelemetryPartA.GetTelemetryItem(activity, instrumentationKey);
37+
telemetryItem = TelemetryPartA.GetTelemetryItem(activity, resource, instrumentationKey);
3738

3839
switch (activity.GetTelemetryType())
3940
{

sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/AzureMonitorExporterHelperExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static TracerProviderBuilder AddAzureMonitorTraceExporter(this TracerProv
3030
configure?.Invoke(options);
3131

3232
// TODO: Pick Simple vs Batching based on AzureMonitorExporterOptions
33-
return builder.AddProcessor(new BatchExportProcessor<Activity>(new AzureMonitorTraceExporter(options)));
33+
return builder.AddProcessor(new BatchActivityExportProcessor(new AzureMonitorTraceExporter(options)));
3434
}
3535
}
3636
}

sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/AzureMonitorExporterLoggingExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static OpenTelemetryLoggerOptions AddAzureMonitorLogExporter(this OpenTel
2222
var options = new AzureMonitorExporterOptions();
2323
configure?.Invoke(options);
2424

25-
return loggerOptions.AddProcessor(new BatchExportProcessor<LogRecord>(new AzureMonitorLogExporter(options)));
25+
return loggerOptions.AddProcessor(new BatchLogRecordExportProcessor(new AzureMonitorLogExporter(options)));
2626
}
2727
}
2828
}

sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/AzureMonitorTraceExporter.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ public override ExportResult Export(in Batch<Activity> batch)
3838

3939
try
4040
{
41-
var telemetryItems = AzureMonitorConverter.Convert(batch, this.instrumentationKey);
41+
var resource = this.ParentProvider.GetResource();
42+
43+
var telemetryItems = AzureMonitorConverter.Convert(batch, resource, this.instrumentationKey);
4244

4345
// TODO: Handle return value, it can be converted as metrics.
4446
// TODO: Validate CancellationToken and async pattern here.

sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/TagEnumerationState.cs

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace Azure.Monitor.OpenTelemetry.Exporter
1010
{
11-
internal struct TagEnumerationState : IActivityEnumerator<KeyValuePair<string, object>>
11+
internal struct TagEnumerationState
1212
{
1313
private static readonly IReadOnlyDictionary<string, PartBType> Part_B_Mapping = new Dictionary<string, PartBType>()
1414
{
@@ -63,56 +63,57 @@ internal struct TagEnumerationState : IActivityEnumerator<KeyValuePair<string, o
6363
private PartBType tempActivityType;
6464
public PartBType activityType;
6565

66-
public bool ForEach(KeyValuePair<string, object> activityTag)
66+
public void ForEach(IEnumerable<KeyValuePair<string, object>> activityTags)
6767
{
68-
if (activityTag.Value == null)
68+
foreach (KeyValuePair<string, object> activityTag in activityTags)
6969
{
70-
return true;
71-
}
70+
if (activityTag.Value == null)
71+
{
72+
continue;
73+
}
7274

73-
if (activityTag.Value is Array array)
74-
{
75-
StringBuilder sw = new StringBuilder();
76-
foreach (var item in array)
75+
if (activityTag.Value is Array array)
7776
{
78-
// TODO: Consider changing it to JSon array.
79-
if (item != null)
77+
StringBuilder sw = new StringBuilder();
78+
foreach (var item in array)
79+
{
80+
// TODO: Consider changing it to JSon array.
81+
if (item != null)
82+
{
83+
sw.Append(item);
84+
sw.Append(',');
85+
}
86+
}
87+
88+
if (sw.Length > 0)
8089
{
81-
sw.Append(item);
82-
sw.Append(',');
90+
sw.Length--;
8391
}
92+
93+
AzMonList.Add(ref PartCTags, new KeyValuePair<string, object>(activityTag.Key, sw.ToString()));
94+
continue;
8495
}
8596

86-
if (sw.Length > 0)
97+
if (!Part_B_Mapping.TryGetValue(activityTag.Key, out tempActivityType))
8798
{
88-
sw.Length--;
99+
AzMonList.Add(ref PartCTags, activityTag);
100+
continue;
89101
}
90102

91-
AzMonList.Add(ref PartCTags, new KeyValuePair<string, object>(activityTag.Key, sw.ToString()));
92-
return true;
93-
}
94-
95-
if (!Part_B_Mapping.TryGetValue(activityTag.Key, out tempActivityType))
96-
{
97-
AzMonList.Add(ref PartCTags, activityTag);
98-
return true;
99-
}
100-
101-
if (activityType == PartBType.Unknown || activityType == PartBType.Common)
102-
{
103-
activityType = tempActivityType;
104-
}
103+
if (activityType == PartBType.Unknown || activityType == PartBType.Common)
104+
{
105+
activityType = tempActivityType;
106+
}
105107

106-
if (tempActivityType == activityType || tempActivityType == PartBType.Common)
107-
{
108-
AzMonList.Add(ref PartBTags, activityTag);
109-
}
110-
else
111-
{
112-
AzMonList.Add(ref PartCTags, activityTag);
108+
if (tempActivityType == activityType || tempActivityType == PartBType.Common)
109+
{
110+
AzMonList.Add(ref PartBTags, activityTag);
111+
}
112+
else
113+
{
114+
AzMonList.Add(ref PartCTags, activityTag);
115+
}
113116
}
114-
115-
return true;
116117
}
117118
}
118119
}

sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/TelemetryPartA.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ internal class TelemetryPartA
3030

3131
internal static string RoleInstance { get; set; }
3232

33-
internal static TelemetryItem GetTelemetryItem(Activity activity, string instrumentationKey)
33+
internal static TelemetryItem GetTelemetryItem(Activity activity, Resource resource, string instrumentationKey)
3434
{
3535
TelemetryItem telemetryItem = new TelemetryItem(PartA_Name_Mapping[activity.GetTelemetryType()], activity.StartTimeUtc.ToString(CultureInfo.InvariantCulture))
3636
{
3737
InstrumentationKey = instrumentationKey
3838
};
3939

40-
InitRoleInfo(activity);
40+
InitRoleInfo(resource);
4141
telemetryItem.Tags[ContextTagKeys.AiCloudRole.ToString()] = RoleName;
4242
telemetryItem.Tags[ContextTagKeys.AiCloudRoleInstance.ToString()] = RoleInstance;
4343
telemetryItem.Tags[ContextTagKeys.AiOperationId.ToString()] = activity.TraceId.ToHexString();
@@ -82,15 +82,13 @@ internal static TelemetryItem GetTelemetryItem(LogRecord logRecord, string instr
8282
return telemetryItem;
8383
}
8484

85-
internal static void InitRoleInfo(Activity activity)
85+
internal static void InitRoleInfo(Resource resource)
8686
{
8787
if (RoleName != null || RoleInstance != null)
8888
{
8989
return;
9090
}
9191

92-
var resource = activity.GetResource();
93-
9492
if (resource == null)
9593
{
9694
return;
@@ -101,15 +99,15 @@ internal static void InitRoleInfo(Activity activity)
10199

102100
foreach (var attribute in resource.Attributes)
103101
{
104-
if (attribute.Key == Resource.ServiceNameKey && attribute.Value is string)
102+
if (attribute.Key == SemanticConventions.AttributeServiceName && attribute.Value is string)
105103
{
106104
serviceName = attribute.Value.ToString();
107105
}
108-
else if (attribute.Key == Resource.ServiceNamespaceKey && attribute.Value is string)
106+
else if (attribute.Key == SemanticConventions.AttributeServiceNamespace && attribute.Value is string)
109107
{
110108
serviceNamespace = attribute.Value.ToString();
111109
}
112-
else if (attribute.Key == Resource.ServiceInstanceIdKey && attribute.Value is string)
110+
else if (attribute.Key == SemanticConventions.AttributeServiceInstance && attribute.Value is string)
113111
{
114112
RoleInstance = attribute.Value.ToString();
115113
}

sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/TelemetryPartB.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ private static TagEnumerationState EnumerateActivityTags(Activity activity)
109109
PartCTags = AzMonList.Initialize()
110110
};
111111

112-
activity.EnumerateTags(ref monitorTags);
112+
monitorTags.ForEach(activity.TagObjects);
113113
return monitorTags;
114114
}
115115

sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Benchmarks/TagObjectsBenchmarks.cs

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -85,30 +85,6 @@ public void Cleanup()
8585
{
8686
}
8787

88-
[Benchmark]
89-
public void Enumerate_AzMonList_NoItem()
90-
{
91-
NoItemActivity.EnumerateTags(ref monitorTags);
92-
}
93-
94-
[Benchmark]
95-
public void Enumerate_AzMonList_Part_B()
96-
{
97-
PartBActivity.EnumerateTags(ref monitorTags);
98-
}
99-
100-
[Benchmark]
101-
public void Enumerate_AzMonList_Part_C()
102-
{
103-
PartCActivity.EnumerateTags(ref monitorTags);
104-
}
105-
106-
[Benchmark]
107-
public void Enumerate_AzMonList_PartB_And_C()
108-
{
109-
PartBAndCActivity.EnumerateTags(ref monitorTags);
110-
}
111-
11288
[Benchmark]
11389
public void Enumerate_TagObjects_NoItem()
11490
{

0 commit comments

Comments
 (0)