Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exporter.Geneva - Refactor unit test #616

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"cSpell.words": [
"fluentdData"
]
}
149 changes: 149 additions & 0 deletions test/OpenTelemetry.Exporter.Geneva.Tests/LogSerializationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
// <copyright file="LogSerializationTests.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Sockets;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using Microsoft.Extensions.Logging;
using OpenTelemetry.Logs;
using Xunit;
using Xunit.Abstractions;

namespace OpenTelemetry.Exporter.Geneva.Tests;

public class LogSerializationTests
{
/*
Run from the current directory:
dotnet test -f net6.0 --filter FullyQualifiedName~LogSerializationTests -l "console;verbosity=detailed"
*/
private readonly ITestOutputHelper output;

public LogSerializationTests(ITestOutputHelper output)
{
this.output = output;
}

[Fact]
public void SerializationTestForException()
{
var exceptionMessage = "Exception Message";
var exportedFields = GetExportedFieldsAfterLogging(logger => logger.Log<object>(
logLevel: LogLevel.Information,
eventId: default,
state: null,
exception: new Exception(exceptionMessage),
formatter: null));

PrintFields(this.output, exportedFields);
cijothomas marked this conversation as resolved.
Show resolved Hide resolved
var actualExceptionMessage = exportedFields["env_ex_msg"];
Assert.Equal(exceptionMessage, actualExceptionMessage);
}

private static void PrintFields(ITestOutputHelper output, Dictionary<object, object> fields)
{
foreach (var field in fields)
{
output.WriteLine($"{field.Key}:{field.Value}");
}
}

private static Dictionary<object, object> GetExportedFieldsAfterLogging(Action<ILogger> doLog)
cijothomas marked this conversation as resolved.
Show resolved Hide resolved
{
Socket server = null;
string path = string.Empty;
try
{
var logRecordList = new List<LogRecord>();
using var loggerFactory = LoggerFactory.Create(builder => builder
.AddOpenTelemetry(options =>
{
options.AddInMemoryExporter(logRecordList);
})
.AddFilter(typeof(GenevaLogExporterTests).FullName, LogLevel.Trace)); // Enable all LogLevels

var logger = loggerFactory.CreateLogger<GenevaLogExporterTests>();
doLog(logger);

Assert.Single(logRecordList);
var exporterOptions = new GenevaExporterOptions();
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
exporterOptions.ConnectionString = "EtwSession=OpenTelemetry";
}
else
{
path = GenerateTempFilePath();
exporterOptions.ConnectionString = "Endpoint=unix:" + path;
var endpoint = new UnixDomainSocketEndPoint(path);
server = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.IP);
server.Bind(endpoint);
server.Listen(1);
}

using var exporter = new MsgPackLogExporter(exporterOptions);
var m_buffer = typeof(MsgPackLogExporter).GetField("m_buffer", BindingFlags.NonPublic | BindingFlags.Static).GetValue(exporter) as ThreadLocal<byte[]>;
_ = exporter.SerializeLogRecord(logRecordList[0]);
object fluentdData = MessagePack.MessagePackSerializer.Deserialize<object>(m_buffer.Value, MessagePack.Resolvers.ContractlessStandardResolver.Instance);

return GetFields(fluentdData);
}
finally
{
server?.Dispose();
try
{
File.Delete(path);
}
catch
{
}
}
}

private static string GenerateTempFilePath()
{
while (true)
cijothomas marked this conversation as resolved.
Show resolved Hide resolved
{
string path = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
if (!File.Exists(path))
{
return path;
}
}
}

private static Dictionary<object, object> GetFields(object fluentdData)
{
/* Fluentd Forward Mode:
[
"Log",
[
[ <timestamp>, { "env_ver": "4.0", ... } ]
],
{ "TimeFormat": "DateTime" }
]
*/

var TimeStampAndMappings = ((fluentdData as object[])[1] as object[])[0];
var mapping = (TimeStampAndMappings as object[])[1] as Dictionary<object, object>;
return mapping;
}
}