Skip to content

Commit 85e9c53

Browse files
committed
add tests for cloud extension
1 parent 522cf91 commit 85e9c53

File tree

1 file changed

+37
-5
lines changed

1 file changed

+37
-5
lines changed

test/OpenTelemetry.Exporter.Geneva.Tests/GenevaLogExporterTests.cs

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -664,14 +664,16 @@ public void SerializationTestWithILoggerLogWithTemplates(bool hasTableNameMappin
664664
{
665665
PrepopulatedFields = new Dictionary<string, object>
666666
{
667-
["cloud.role"] = "BusyWorker",
668-
["cloud.roleInstance"] = "CY1SCH030021417",
669667
["cloud.roleVer"] = "9.0.15289.2",
670668
["prepopulated"] = "prepopulated field",
671669
},
672670
};
673671

674-
var resource = ResourceBuilder.CreateEmpty().AddAttributes([new KeyValuePair<string, object>("resourceAttr", "from resource")]).Build();
672+
var resource = ResourceBuilder.CreateEmpty().AddAttributes([
673+
new KeyValuePair<string, object>("resourceAttr", "from resource"),
674+
new KeyValuePair<string, object>("service.name", "BusyWorker"),
675+
new KeyValuePair<string, object>("service.instanceId", "CY1SCH030021417")])
676+
.Build();
675677

676678
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
677679
{
@@ -858,7 +860,11 @@ public void SerializationTestWithDuplicateFields(bool conflictingPrepopulatedFie
858860
var TimeStampAndMappings = ((fluentdData as object[])[1] as object[])[0];
859861
var timeStamp = (DateTime)(TimeStampAndMappings as object[])[0];
860862
var mapping = (TimeStampAndMappings as object[])[1] as Dictionary<object, object>;
861-
var env_properties = mapping.GetValueOrDefault("env_properties") as Dictionary<object, object> ?? [];
863+
var env_properties = new Dictionary<object, object>();
864+
if (mapping.ContainsKey("env_properties"))
865+
{
866+
env_properties = mapping["env_properties"] as Dictionary<object, object> ?? [];
867+
}
862868

863869
void AssertField(bool isDedicated, string fieldValue)
864870
{
@@ -1712,7 +1718,12 @@ private void AssertFluentdForwardModeForLogRecord(GenevaExporterOptions exporter
17121718
foreach (var item in exporterOptions.PrepopulatedFields)
17131719
{
17141720
var partAValue = item.Value as string;
1715-
var partAKey = MsgPackExporter.V40_PART_A_MAPPING.GetValueOrDefault(item.Key, item.Key);
1721+
var partAKey = item.Key;
1722+
if (MsgPackExporter.V40_PART_A_MAPPING.ContainsKey(item.Key))
1723+
{
1724+
partAKey = MsgPackExporter.V40_PART_A_MAPPING[item.Key];
1725+
}
1726+
17161727
Assert.Equal(partAValue, mapping[partAKey]);
17171728
}
17181729

@@ -1737,6 +1748,21 @@ private void AssertFluentdForwardModeForLogRecord(GenevaExporterOptions exporter
17371748
Assert.Equal(logRecord.Exception.Message, mapping["env_ex_msg"]);
17381749
}
17391750

1751+
// Part A cloud extensions
1752+
var serviceNameField = resource.Attributes.FirstOrDefault(attr => attr.Key == "service.name");
1753+
if (serviceNameField.Key == "service.name" && !exporterOptions.PrepopulatedFields.ContainsKey("cloud.role"))
1754+
{
1755+
Assert.Contains("env_cloud_role", mapping);
1756+
Assert.Equal(serviceNameField.Value, mapping["env_cloud_role"]);
1757+
}
1758+
1759+
var serviceInstanceField = resource.Attributes.FirstOrDefault(attr => attr.Key == "service.instanceId");
1760+
if (serviceInstanceField.Key == "service.instanceId" && !exporterOptions.PrepopulatedFields.ContainsKey("cloud.roleInstance"))
1761+
{
1762+
Assert.Contains("env_cloud_roleInstance", mapping);
1763+
Assert.Equal(serviceInstanceField.Value, mapping["env_cloud_roleInstance"]);
1764+
}
1765+
17401766
// Part B fields
17411767

17421768
// `LogRecord.LogLevel` was marked Obsolete in https://github.com/open-telemetry/opentelemetry-dotnet/pull/4568
@@ -1810,6 +1836,12 @@ private void AssertFluentdForwardModeForLogRecord(GenevaExporterOptions exporter
18101836

18111837
foreach (var item in resource.Attributes)
18121838
{
1839+
if (item.Key == "service.name" || item.Key == "service.instanceId")
1840+
{
1841+
// these ones are already checked.
1842+
continue;
1843+
}
1844+
18131845
if (exporterOptions.CustomFields == null || exporterOptions.CustomFields.Contains(item.Key))
18141846
{
18151847
// It should be found as a custom field

0 commit comments

Comments
 (0)