Skip to content

Commit e85bd93

Browse files
authored
Add ToOtlpLog unit tests for scopes (#4523)
1 parent 64df5e3 commit e85bd93

File tree

1 file changed

+236
-0
lines changed

1 file changed

+236
-0
lines changed

test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpLogExporterTests.cs

Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
using OpenTelemetry.Tests;
2727
using OpenTelemetry.Trace;
2828
using Xunit;
29+
using static OpenTelemetry.Proto.Common.V1.AnyValue;
2930
using OtlpCollector = OpenTelemetry.Proto.Collector.Logs.V1;
3031
using OtlpCommon = OpenTelemetry.Proto.Common.V1;
3132
using OtlpLogs = OpenTelemetry.Proto.Logs.V1;
@@ -607,6 +608,241 @@ public void Export_WhenExportIsSuccessful_ReturnsExportResultSuccess()
607608
Assert.Equal(ExportResult.Success, result);
608609
}
609610

611+
[Fact]
612+
public void ToOtlpLog_WhenOptionsIncludeScopesIsFalse_DoesNotContainScopeAttribute()
613+
{
614+
// Arrange.
615+
var logRecords = new List<LogRecord>(1);
616+
using var loggerFactory = LoggerFactory.Create(builder =>
617+
{
618+
builder.AddOpenTelemetry(options =>
619+
{
620+
options.IncludeScopes = false;
621+
options.AddInMemoryExporter(logRecords);
622+
});
623+
});
624+
var logger = loggerFactory.CreateLogger("Some category");
625+
626+
const string expectedScopeKey = "Some scope key";
627+
const string expectedScopeValue = "Some scope value";
628+
629+
// Act.
630+
using (logger.BeginScope(new List<KeyValuePair<string, object>>
631+
{
632+
new KeyValuePair<string, object>(expectedScopeKey, expectedScopeValue),
633+
}))
634+
{
635+
logger.LogInformation("Some log information message.");
636+
}
637+
638+
// Assert.
639+
var logRecord = logRecords.Single();
640+
var otlpLogRecord = logRecord.ToOtlpLog(DefaultSdkLimitOptions);
641+
var actualScope = TryGetAttribute(otlpLogRecord, expectedScopeKey);
642+
Assert.Null(actualScope);
643+
}
644+
645+
[Theory]
646+
[InlineData("Some scope value")]
647+
[InlineData('a')]
648+
public void ToOtlpLog_WhenOptionsIncludeScopesIsTrue_ContainsScopeAttributeStringValue(object scopeValue)
649+
{
650+
// Arrange.
651+
var logRecords = new List<LogRecord>(1);
652+
using var loggerFactory = LoggerFactory.Create(builder =>
653+
{
654+
builder.AddOpenTelemetry(options =>
655+
{
656+
options.IncludeScopes = true;
657+
options.AddInMemoryExporter(logRecords);
658+
});
659+
});
660+
var logger = loggerFactory.CreateLogger(nameof(OtlpLogExporterTests));
661+
662+
const string scopeKey = "Some scope key";
663+
664+
// Act.
665+
using (logger.BeginScope(new List<KeyValuePair<string, object>>
666+
{
667+
new KeyValuePair<string, object>(scopeKey, scopeValue),
668+
}))
669+
{
670+
logger.LogInformation("Some log information message.");
671+
}
672+
673+
// Assert.
674+
var logRecord = logRecords.Single();
675+
var otlpLogRecord = logRecord.ToOtlpLog(DefaultSdkLimitOptions);
676+
var actualScope = TryGetAttribute(otlpLogRecord, scopeKey);
677+
Assert.NotNull(actualScope);
678+
Assert.Equal(scopeKey, actualScope.Key);
679+
Assert.Equal(ValueOneofCase.StringValue, actualScope.Value.ValueCase);
680+
Assert.Equal(scopeValue.ToString(), actualScope.Value.StringValue);
681+
}
682+
683+
[Theory]
684+
[InlineData(true)]
685+
[InlineData(false)]
686+
public void ToOtlpLog_WhenOptionsIncludeScopesIsTrue_ContainsScopeAttributeBoolValue(bool scopeValue)
687+
{
688+
// Arrange.
689+
var logRecords = new List<LogRecord>(1);
690+
using var loggerFactory = LoggerFactory.Create(builder =>
691+
{
692+
builder.AddOpenTelemetry(options =>
693+
{
694+
options.IncludeScopes = true;
695+
options.AddInMemoryExporter(logRecords);
696+
});
697+
});
698+
var logger = loggerFactory.CreateLogger(nameof(OtlpLogExporterTests));
699+
700+
const string scopeKey = "Some scope key";
701+
702+
// Act.
703+
using (logger.BeginScope(new List<KeyValuePair<string, object>>
704+
{
705+
new KeyValuePair<string, object>(scopeKey, scopeValue),
706+
}))
707+
{
708+
logger.LogInformation("Some log information message.");
709+
}
710+
711+
// Assert.
712+
var logRecord = logRecords.Single();
713+
var otlpLogRecord = logRecord.ToOtlpLog(DefaultSdkLimitOptions);
714+
var actualScope = TryGetAttribute(otlpLogRecord, scopeKey);
715+
Assert.NotNull(actualScope);
716+
Assert.Equal(scopeKey, actualScope.Key);
717+
Assert.Equal(ValueOneofCase.BoolValue, actualScope.Value.ValueCase);
718+
Assert.Equal(scopeValue.ToString(), actualScope.Value.BoolValue.ToString());
719+
}
720+
721+
[Theory]
722+
[InlineData(byte.MinValue)]
723+
[InlineData(byte.MaxValue)]
724+
[InlineData(sbyte.MinValue)]
725+
[InlineData(sbyte.MaxValue)]
726+
[InlineData(short.MinValue)]
727+
[InlineData(short.MaxValue)]
728+
[InlineData(ushort.MinValue)]
729+
[InlineData(ushort.MaxValue)]
730+
[InlineData(int.MinValue)]
731+
[InlineData(int.MaxValue)]
732+
[InlineData(uint.MinValue)]
733+
[InlineData(uint.MaxValue)]
734+
[InlineData(long.MinValue)]
735+
[InlineData(long.MaxValue)]
736+
public void ToOtlpLog_WhenOptionsIncludeScopesIsTrue_ContainsScopeAttributeIntValue(object scopeValue)
737+
{
738+
// Arrange.
739+
var logRecords = new List<LogRecord>(1);
740+
using var loggerFactory = LoggerFactory.Create(builder =>
741+
{
742+
builder.AddOpenTelemetry(options =>
743+
{
744+
options.IncludeScopes = true;
745+
options.AddInMemoryExporter(logRecords);
746+
});
747+
});
748+
var logger = loggerFactory.CreateLogger(nameof(OtlpLogExporterTests));
749+
750+
const string scopeKey = "Some scope key";
751+
752+
// Act.
753+
using (logger.BeginScope(new List<KeyValuePair<string, object>>
754+
{
755+
new KeyValuePair<string, object>(scopeKey, scopeValue),
756+
}))
757+
{
758+
logger.LogInformation("Some log information message.");
759+
}
760+
761+
// Assert.
762+
var logRecord = logRecords.Single();
763+
var otlpLogRecord = logRecord.ToOtlpLog(DefaultSdkLimitOptions);
764+
var actualScope = TryGetAttribute(otlpLogRecord, scopeKey);
765+
Assert.NotNull(actualScope);
766+
Assert.Equal(scopeKey, actualScope.Key);
767+
Assert.Equal(ValueOneofCase.IntValue, actualScope.Value.ValueCase);
768+
Assert.Equal(scopeValue.ToString(), actualScope.Value.IntValue.ToString());
769+
}
770+
771+
[Theory]
772+
[InlineData(float.MinValue)]
773+
[InlineData(float.MaxValue)]
774+
public void ToOtlpLog_WhenOptionsIncludeScopesIsTrue_ContainsScopeAttributeDoubleValueForFloat(float scopeValue)
775+
{
776+
// Arrange.
777+
var logRecords = new List<LogRecord>(1);
778+
using var loggerFactory = LoggerFactory.Create(builder =>
779+
{
780+
builder.AddOpenTelemetry(options =>
781+
{
782+
options.IncludeScopes = true;
783+
options.AddInMemoryExporter(logRecords);
784+
});
785+
});
786+
var logger = loggerFactory.CreateLogger(nameof(OtlpLogExporterTests));
787+
788+
const string scopeKey = "Some scope key";
789+
790+
// Act.
791+
using (logger.BeginScope(new List<KeyValuePair<string, object>>
792+
{
793+
new KeyValuePair<string, object>(scopeKey, scopeValue),
794+
}))
795+
{
796+
logger.LogInformation("Some log information message.");
797+
}
798+
799+
// Assert.
800+
var logRecord = logRecords.Single();
801+
var otlpLogRecord = logRecord.ToOtlpLog(DefaultSdkLimitOptions);
802+
var actualScope = TryGetAttribute(otlpLogRecord, scopeKey);
803+
Assert.NotNull(actualScope);
804+
Assert.Equal(scopeKey, actualScope.Key);
805+
Assert.Equal(ValueOneofCase.DoubleValue, actualScope.Value.ValueCase);
806+
Assert.Equal(((double)scopeValue).ToString(), actualScope.Value.DoubleValue.ToString());
807+
}
808+
809+
[Theory]
810+
[InlineData(double.MinValue)]
811+
[InlineData(double.MaxValue)]
812+
public void ToOtlpLog_WhenOptionsIncludeScopesIsTrue_ContainsScopeAttributeDoubleValueForDouble(double scopeValue)
813+
{
814+
// Arrange.
815+
var logRecords = new List<LogRecord>(1);
816+
using var loggerFactory = LoggerFactory.Create(builder =>
817+
{
818+
builder.AddOpenTelemetry(options =>
819+
{
820+
options.IncludeScopes = true;
821+
options.AddInMemoryExporter(logRecords);
822+
});
823+
});
824+
var logger = loggerFactory.CreateLogger(nameof(OtlpLogExporterTests));
825+
826+
const string scopeKey = "Some scope key";
827+
828+
// Act.
829+
using (logger.BeginScope(new List<KeyValuePair<string, object>>
830+
{
831+
new KeyValuePair<string, object>(scopeKey, scopeValue),
832+
}))
833+
{
834+
logger.LogInformation("Some log information message.");
835+
}
836+
837+
// Assert.
838+
var logRecord = logRecords.Single();
839+
var otlpLogRecord = logRecord.ToOtlpLog(DefaultSdkLimitOptions);
840+
var actualScope = TryGetAttribute(otlpLogRecord, scopeKey);
841+
Assert.NotNull(actualScope);
842+
Assert.Equal(scopeKey, actualScope.Key);
843+
Assert.Equal(scopeValue.ToString(), actualScope.Value.DoubleValue.ToString());
844+
}
845+
610846
private static OtlpCommon.KeyValue TryGetAttribute(OtlpLogs.LogRecord record, string key)
611847
{
612848
return record.Attributes.FirstOrDefault(att => att.Key == key);

0 commit comments

Comments
 (0)