Skip to content

Commit

Permalink
Add ToOtlpLog unit tests for scopes
Browse files Browse the repository at this point in the history
  • Loading branch information
mfogliatto committed May 27, 2023
1 parent a5b14aa commit 231ebb2
Showing 1 changed file with 236 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
using OpenTelemetry.Tests;
using OpenTelemetry.Trace;
using Xunit;
using static OpenTelemetry.Proto.Common.V1.AnyValue;
using OtlpCollector = OpenTelemetry.Proto.Collector.Logs.V1;
using OtlpCommon = OpenTelemetry.Proto.Common.V1;
using OtlpLogs = OpenTelemetry.Proto.Logs.V1;
Expand Down Expand Up @@ -607,6 +608,241 @@ public void Export_WhenExportIsSuccessful_ReturnsExportResultSuccess()
Assert.Equal(ExportResult.Success, result);
}

[Fact]
public void ToOtlpLog_WhenOptionsIncludeScopesIsFalse_DoesNotContainScopeAttribute()
{
// Arrange.
var logRecords = new List<LogRecord>(1);
using var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddOpenTelemetry(options =>
{
options.IncludeScopes = false;
options.AddInMemoryExporter(logRecords);
});
});
var logger = loggerFactory.CreateLogger("Some category");

const string expectedScopeKey = "Some scope key";
const string expectedScopeValue = "Some scope value";

// Act.
using (logger.BeginScope(new List<KeyValuePair<string, object>>
{
new KeyValuePair<string, object>(expectedScopeKey, expectedScopeValue),
}))
{
logger.LogInformation("Some log information message.");
}

// Assert.
var logRecord = logRecords.Single();
var otlpLogRecord = logRecord.ToOtlpLog(DefaultSdkLimitOptions);
var actualScope = TryGetAttribute(otlpLogRecord, expectedScopeKey);
Assert.Null(actualScope);
}

[Theory]
[InlineData("Some scope value")]
[InlineData('a')]
public void ToOtlpLog_WhenOptionsIncludeScopesIsTrue_ContainsScopeAttributeStringValue(object scopeValue)
{
// Arrange.
var logRecords = new List<LogRecord>(1);
using var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddOpenTelemetry(options =>
{
options.IncludeScopes = true;
options.AddInMemoryExporter(logRecords);
});
});
var logger = loggerFactory.CreateLogger(nameof(OtlpLogExporterTests));

const string scopeKey = "Some scope key";

// Act.
using (logger.BeginScope(new List<KeyValuePair<string, object>>
{
new KeyValuePair<string, object>(scopeKey, scopeValue),
}))
{
logger.LogInformation("Some log information message.");
}

// Assert.
var logRecord = logRecords.Single();
var otlpLogRecord = logRecord.ToOtlpLog(DefaultSdkLimitOptions);
var actualScope = TryGetAttribute(otlpLogRecord, scopeKey);
Assert.NotNull(actualScope);
Assert.Equal(scopeKey, actualScope.Key);
Assert.Equal(ValueOneofCase.StringValue, actualScope.Value.ValueCase);
Assert.Equal(scopeValue.ToString(), actualScope.Value.StringValue);
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public void ToOtlpLog_WhenOptionsIncludeScopesIsTrue_ContainsScopeAttributeBoolValue(bool scopeValue)
{
// Arrange.
var logRecords = new List<LogRecord>(1);
using var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddOpenTelemetry(options =>
{
options.IncludeScopes = true;
options.AddInMemoryExporter(logRecords);
});
});
var logger = loggerFactory.CreateLogger(nameof(OtlpLogExporterTests));

const string scopeKey = "Some scope key";

// Act.
using (logger.BeginScope(new List<KeyValuePair<string, object>>
{
new KeyValuePair<string, object>(scopeKey, scopeValue),
}))
{
logger.LogInformation("Some log information message.");
}

// Assert.
var logRecord = logRecords.Single();
var otlpLogRecord = logRecord.ToOtlpLog(DefaultSdkLimitOptions);
var actualScope = TryGetAttribute(otlpLogRecord, scopeKey);
Assert.NotNull(actualScope);
Assert.Equal(scopeKey, actualScope.Key);
Assert.Equal(ValueOneofCase.BoolValue, actualScope.Value.ValueCase);
Assert.Equal(scopeValue.ToString(), actualScope.Value.BoolValue.ToString());
}

[Theory]
[InlineData(byte.MinValue)]
[InlineData(byte.MaxValue)]
[InlineData(sbyte.MinValue)]
[InlineData(sbyte.MaxValue)]
[InlineData(short.MinValue)]
[InlineData(short.MaxValue)]
[InlineData(ushort.MinValue)]
[InlineData(ushort.MaxValue)]
[InlineData(int.MinValue)]
[InlineData(int.MaxValue)]
[InlineData(uint.MinValue)]
[InlineData(uint.MaxValue)]
[InlineData(long.MinValue)]
[InlineData(long.MaxValue)]
public void ToOtlpLog_WhenOptionsIncludeScopesIsTrue_ContainsScopeAttributeIntValue(object scopeValue)
{
// Arrange.
var logRecords = new List<LogRecord>(1);
using var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddOpenTelemetry(options =>
{
options.IncludeScopes = true;
options.AddInMemoryExporter(logRecords);
});
});
var logger = loggerFactory.CreateLogger(nameof(OtlpLogExporterTests));

const string scopeKey = "Some scope key";

// Act.
using (logger.BeginScope(new List<KeyValuePair<string, object>>
{
new KeyValuePair<string, object>(scopeKey, scopeValue),
}))
{
logger.LogInformation("Some log information message.");
}

// Assert.
var logRecord = logRecords.Single();
var otlpLogRecord = logRecord.ToOtlpLog(DefaultSdkLimitOptions);
var actualScope = TryGetAttribute(otlpLogRecord, scopeKey);
Assert.NotNull(actualScope);
Assert.Equal(scopeKey, actualScope.Key);
Assert.Equal(ValueOneofCase.IntValue, actualScope.Value.ValueCase);
Assert.Equal(scopeValue.ToString(), actualScope.Value.IntValue.ToString());
}

[Theory]
[InlineData(float.MinValue)]
[InlineData(float.MaxValue)]
public void ToOtlpLog_WhenOptionsIncludeScopesIsTrue_ContainsScopeAttributeDoubleValueForFloat(float scopeValue)
{
// Arrange.
var logRecords = new List<LogRecord>(1);
using var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddOpenTelemetry(options =>
{
options.IncludeScopes = true;
options.AddInMemoryExporter(logRecords);
});
});
var logger = loggerFactory.CreateLogger(nameof(OtlpLogExporterTests));

const string scopeKey = "Some scope key";

// Act.
using (logger.BeginScope(new List<KeyValuePair<string, object>>
{
new KeyValuePair<string, object>(scopeKey, scopeValue),
}))
{
logger.LogInformation("Some log information message.");
}

// Assert.
var logRecord = logRecords.Single();
var otlpLogRecord = logRecord.ToOtlpLog(DefaultSdkLimitOptions);
var actualScope = TryGetAttribute(otlpLogRecord, scopeKey);
Assert.NotNull(actualScope);
Assert.Equal(scopeKey, actualScope.Key);
Assert.Equal(ValueOneofCase.DoubleValue, actualScope.Value.ValueCase);
Assert.Equal(((double)scopeValue).ToString(), actualScope.Value.DoubleValue.ToString());
}

[Theory]
[InlineData(double.MinValue)]
[InlineData(double.MaxValue)]
public void ToOtlpLog_WhenOptionsIncludeScopesIsTrue_ContainsScopeAttributeDoubleValueForDouble(double scopeValue)
{
// Arrange.
var logRecords = new List<LogRecord>(1);
using var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddOpenTelemetry(options =>
{
options.IncludeScopes = true;
options.AddInMemoryExporter(logRecords);
});
});
var logger = loggerFactory.CreateLogger(nameof(OtlpLogExporterTests));

const string scopeKey = "Some scope key";

// Act.
using (logger.BeginScope(new List<KeyValuePair<string, object>>
{
new KeyValuePair<string, object>(scopeKey, scopeValue),
}))
{
logger.LogInformation("Some log information message.");
}

// Assert.
var logRecord = logRecords.Single();
var otlpLogRecord = logRecord.ToOtlpLog(DefaultSdkLimitOptions);
var actualScope = TryGetAttribute(otlpLogRecord, scopeKey);
Assert.NotNull(actualScope);
Assert.Equal(scopeKey, actualScope.Key);
Assert.Equal(scopeValue.ToString(), actualScope.Value.DoubleValue.ToString());
}

private static OtlpCommon.KeyValue TryGetAttribute(OtlpLogs.LogRecord record, string key)
{
return record.Attributes.FirstOrDefault(att => att.Key == key);
Expand Down

0 comments on commit 231ebb2

Please sign in to comment.