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

PassThru TableNameMappings using the logger category name. #345

Merged
merged 22 commits into from
May 24, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
129 changes: 118 additions & 11 deletions src/OpenTelemetry.Exporter.Geneva/GenevaLogExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#if NETSTANDARD2_0 || NET461
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Globalization;
using System.Runtime.CompilerServices;
Expand Down Expand Up @@ -44,6 +45,8 @@ public class GenevaLogExporter : GenevaBaseExporter<LogRecord>
};

private readonly IDataTransport m_dataTransport;
private static readonly ConcurrentDictionary<string, string> rawNameToSanitizedName = new();
Yun-Ting marked this conversation as resolved.
Show resolved Hide resolved
Yun-Ting marked this conversation as resolved.
Show resolved Hide resolved
private readonly bool shouldPassThruTableMappings;
private bool isDisposed;
private Func<object, string> convertToJson;

Expand All @@ -65,7 +68,14 @@ public GenevaLogExporter(GenevaExporterOptions options)

if (kv.Key == "*")
{
this.m_defaultEventName = kv.Value;
if (kv.Value == "*")
{
this.shouldPassThruTableMappings = true;
}
else
{
this.m_defaultEventName = kv.Value;
}
}
else
{
Expand Down Expand Up @@ -204,14 +214,6 @@ internal int SerializeLogRecord(LogRecord logRecord)
listKvp = logRecord.State as IReadOnlyList<KeyValuePair<string, object>>;
}

var name = logRecord.CategoryName;

// If user configured explicit TableName, use it.
if (this.m_tableMappings == null || !this.m_tableMappings.TryGetValue(name, out var eventName))
{
eventName = this.m_defaultEventName;
}

var buffer = m_buffer.Value;
if (buffer == null)
{
Expand Down Expand Up @@ -242,7 +244,51 @@ internal int SerializeLogRecord(LogRecord logRecord)
var timestamp = logRecord.Timestamp;
var cursor = 0;
cursor = MessagePackSerializer.WriteArrayHeader(buffer, cursor, 3);
cursor = MessagePackSerializer.SerializeAsciiString(buffer, cursor, eventName);

var categoryName = logRecord.CategoryName;
string eventName = null;

// If user configured explicit TableName, use it.
if (this.m_tableMappings != null && this.m_tableMappings.TryGetValue(categoryName, out eventName))
{
cursor = MessagePackSerializer.SerializeAsciiString(buffer, cursor, eventName);
}
else if (!this.shouldPassThruTableMappings)
{
eventName = this.m_defaultEventName;
cursor = MessagePackSerializer.SerializeAsciiString(buffer, cursor, eventName);
}
else if (this.shouldPassThruTableMappings)
reyang marked this conversation as resolved.
Show resolved Hide resolved
{
if (!rawNameToSanitizedName.TryGetValue(categoryName, out var sanitizedName))
{
Yun-Ting marked this conversation as resolved.
Show resolved Hide resolved
if (categoryName.Length > 0)
{
int cursorStartIdx = cursor;
int validNameLength = 0;
cursor = Sanitize(buffer, cursor, ref cursorStartIdx, ref validNameLength, categoryName);
if (validNameLength > 0)
{
eventName = Encoding.UTF8.GetString(buffer, cursorStartIdx + 2, validNameLength);
Yun-Ting marked this conversation as resolved.
Show resolved Hide resolved
}
else
{
cursor = MessagePackSerializer.SerializeNull(buffer, cursor);
}
}

if (rawNameToSanitizedName.Count <= 1024)
{
rawNameToSanitizedName.TryAdd(categoryName, eventName);
}
}
else
{
Yun-Ting marked this conversation as resolved.
Show resolved Hide resolved
eventName = sanitizedName;
cursor = MessagePackSerializer.SerializeAsciiString(buffer, cursor, eventName);
}
}

cursor = MessagePackSerializer.WriteArrayHeader(buffer, cursor, 1);
cursor = MessagePackSerializer.WriteArrayHeader(buffer, cursor, 2);
cursor = MessagePackSerializer.SerializeUtcDateTime(buffer, cursor, timestamp);
Expand Down Expand Up @@ -329,7 +375,7 @@ internal int SerializeLogRecord(LogRecord logRecord)
cntFields += 1;

cursor = MessagePackSerializer.SerializeAsciiString(buffer, cursor, "name");
cursor = MessagePackSerializer.SerializeUnicodeString(buffer, cursor, name);
cursor = MessagePackSerializer.SerializeUnicodeString(buffer, cursor, categoryName);
cijothomas marked this conversation as resolved.
Show resolved Hide resolved
cntFields += 1;

bool hasEnvProperties = false;
Expand Down Expand Up @@ -443,5 +489,66 @@ private static byte GetSeverityNumber(LogLevel logLevel)
return 1;
}
}

private static int Sanitize(byte[] buffer, int cursor, ref int cursorStartIdx, ref int validNameLength, string categoryName)
Yun-Ting marked this conversation as resolved.
Show resolved Hide resolved
{
if (categoryName.Length > (1 << 8) - 1)
Yun-Ting marked this conversation as resolved.
Show resolved Hide resolved
{
// The size of categoryName should not be greater than 2^8 -1.
return cursor;
Yun-Ting marked this conversation as resolved.
Show resolved Hide resolved
}

// Reserve 2 bytes for storing LIMIT_MAX_STR8_LENGTH_IN_BYTES and (byte)validNameLength -
// these 2 bytes will be back filled after iterating through categoryName.
cursor += 2;

// Special treatment for the first character.
var firstChar = categoryName[0];
if (firstChar >= 'A' && firstChar <= 'Z')
{
buffer[cursor++] = (byte)firstChar;
++validNameLength;
}
else if (firstChar >= 'a' && firstChar <= 'z')
{
// If the first character in the resulting string is lower-case ALPHA,
// it will be converted to the corresponding upper-case.
buffer[cursor++] = (byte)(firstChar - 32);
++validNameLength;
}
else
{
// Not a valid name - Part B name should follow PascalCase naming convention.
return cursor -= 2;
}

const int maxNameCharCount = 50;
Yun-Ting marked this conversation as resolved.
Show resolved Hide resolved
for (int i = 1; i < categoryName.Length; ++i)
{
if (validNameLength >= maxNameCharCount - 1)
Yun-Ting marked this conversation as resolved.
Show resolved Hide resolved
{
break;
}

var cur = categoryName[i];
if ((cur >= '0' && cur <= '9') || (cur >= 'A' && cur <= 'Z') || (cur >= 'a' && cur <= 'z'))
Yun-Ting marked this conversation as resolved.
Show resolved Hide resolved
{
buffer[cursor++] = (byte)cur;
++validNameLength;
}
}

if (validNameLength > 0)
Yun-Ting marked this conversation as resolved.
Show resolved Hide resolved
{
// Backfilling MessagePack serialization protocol and valid category length to the startIdx of the cateoryName byte array.
MessagePackSerializer.BackFill(buffer, cursorStartIdx, validNameLength);
}
else
{
cursor -= 2;
Yun-Ting marked this conversation as resolved.
Show resolved Hide resolved
}

return cursor;
}
}
#endif
7 changes: 7 additions & 0 deletions src/OpenTelemetry.Exporter.Geneva/MessagePackSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,13 @@ private static unsafe long Float64ToInt64(double value)
return *(long*)&value;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void BackFill(byte[] buffer, int nameStartIdx, int validNameLength)
Yun-Ting marked this conversation as resolved.
Show resolved Hide resolved
{
buffer[nameStartIdx] = STR8;
buffer[nameStartIdx + 1] = unchecked((byte)validNameLength);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int SerializeAsciiString(byte[] buffer, int cursor, string value)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// <copyright file="LogExporterTableMappingsBenchmarks.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.Collections.Generic;
using BenchmarkDotNet.Attributes;
using Microsoft.Extensions.Logging;

/*
Summary
BenchmarkDotNet=v0.13.1, OS=Windows 10.0.19044.1645 (21H2)
Intel Xeon CPU E5-1650 v4 3.60GHz, 1 CPU, 12 logical and 6 physical cores
.NET SDK=6.0.202
[Host] : .NET Core 3.1.24 (CoreCLR 4.700.22.16002, CoreFX 4.700.22.17909), X64 RyuJIT
DefaultJob : .NET Core 3.1.24 (CoreCLR 4.700.22.16002, CoreFX 4.700.22.17909), X64 RyuJIT
| Method | Mean | Error | StdDev | Gen 0 | Allocated |
|------------------------------------------------ |---------:|----------:|----------:|-------:|----------:|
| CategoryTableNameMappingsDefinedInConfiguration | 1.560 us | 0.0307 us | 0.0673 us | 0.0324 | 256 B |
| PassThruTableNameMappingsWhenTheRuleIsEnbled | 1.573 us | 0.0314 us | 0.0574 us | 0.0324 | 256 B |
Yun-Ting marked this conversation as resolved.
Show resolved Hide resolved
*/

namespace OpenTelemetry.Exporter.Geneva.Benchmark
{
[MemoryDiagnoser]
public class LogExporterTableMappingsBenchmarks
{
private readonly ILoggerFactory loggerFactory;
private readonly ILogger storeALogger;
private readonly ILogger storeBLogger;

public LogExporterTableMappingsBenchmarks()
{
this.loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddOpenTelemetry(loggerOptions =>
{
loggerOptions.AddGenevaLogExporter(exporterOptions =>
{
exporterOptions.ConnectionString = "EtwSession=OpenTelemetry";
exporterOptions.PrepopulatedFields = new Dictionary<string, object>
{
["cloud.role"] = "BusyWorker",
["cloud.roleInstance"] = "CY1SCH030021417",
["cloud.roleVer"] = "9.0.15289.2",
};

exporterOptions.TableNameMappings = new Dictionary<string, string>
{
["Company.StoreA"] = "Store",
["*"] = "*",
};
});
});
});

this.storeALogger = this.loggerFactory.CreateLogger("Company.StoreA");
this.storeBLogger = this.loggerFactory.CreateLogger("Company.StoreB");
}

[Benchmark]
public void CategoryTableNameMappingsDefinedInConfiguration()
{
this.storeALogger.LogInformation("Hello from {storeName} {number}.", "Tokyo", 6);
}

[Benchmark]
public void PassThruTableNameMappingsWhenTheRuleIsEnbled()
{
this.storeBLogger.LogInformation("Hello from {storeName} {number}.", "Kyoto", 2);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,103 @@ public void TableNameMappingTest(params string[] category)
}
}

[Fact]
public void PassThruTableMappingsWhenTheRuleIsEnabled()
{
var userInitializedCategoryToTableNameMappings = new Dictionary<string, string>
{
["Company.Store"] = "Store",
["Company.Orders"] = "Orders",
["*"] = "*",
};

var expectedCategoryToTableNameList = new List<KeyValuePair<string, string>>
{
// The category name must match "^[A-Z][a-zA-Z0-9]*$"; any character that is not allowed will be removed.
new KeyValuePair<string, string>("Company.Customer", "CompanyCustomer"),

// testing caching code-path.
new KeyValuePair<string, string>("Company.Customer", "CompanyCustomer"),

new KeyValuePair<string, string>("Company-%-Customer*Region$##", "CompanyCustomerRegion"),

// If the first character in the resulting string is lower-case ALPHA,
// it will be converted to the corresponding upper-case.
new KeyValuePair<string, string>("company.Calendar", "CompanyCalendar"),

// After removing not allowed characters,
// if the resulting string is still an illegal Part B name, the data will get dropped on the floor.
new KeyValuePair<string, string>("$&-.$~!!", null),

// If the resulting string is longer than 50 characters, only the first 50 characters will be taken.
new KeyValuePair<string, string>("Company.Customer.rsLiheLClHJasBOvM.XI4uW7iop6ghvwBzahfs", "CompanyCustomerrsLiheLClHJasBOvMXI4uW7iop6ghvwBza"),
Yun-Ting marked this conversation as resolved.
Show resolved Hide resolved

Yun-Ting marked this conversation as resolved.
Show resolved Hide resolved
// The data will be dropped on the floor as the exporter cannot deduce a valid table name.
new KeyValuePair<string, string>("1.2", null),
};

var logRecordList = new List<LogRecord>();
var exporterOptions = new GenevaExporterOptions
{
TableNameMappings = userInitializedCategoryToTableNameMappings,
ConnectionString = "EtwSession=OpenTelemetry",
};

using var loggerFactory = LoggerFactory.Create(builder => builder
.AddOpenTelemetry(options =>
{
options.AddInMemoryExporter(logRecordList);
})
.AddFilter("*", LogLevel.Trace)); // Enable all LogLevels

// Create a test exporter to get MessagePack byte data to validate if the data was serialized correctly.
using var exporter = new GenevaLogExporter(exporterOptions);

ILogger passThruTableMappingsLogger, userInitializedTableMappingsLogger;
ThreadLocal<byte[]> m_buffer;
object fluentdData;
string actualTableName;

// Verify that the category table mappings specified by the users in the Geneva Configuration are mapped correctly.
foreach (var mapping in userInitializedCategoryToTableNameMappings)
{
if (mapping.Key != "*")
{
userInitializedTableMappingsLogger = loggerFactory.CreateLogger(mapping.Key);
userInitializedTableMappingsLogger.LogInformation("This information does not matter.");
Assert.Single(logRecordList);
m_buffer = typeof(GenevaLogExporter).GetField("m_buffer", BindingFlags.NonPublic | BindingFlags.Static).GetValue(exporter) as ThreadLocal<byte[]>;
Yun-Ting marked this conversation as resolved.
Show resolved Hide resolved
_ = exporter.SerializeLogRecord(logRecordList[0]);
fluentdData = MessagePack.MessagePackSerializer.Deserialize<object>(m_buffer.Value, MessagePack.Resolvers.ContractlessStandardResolver.Instance);
actualTableName = (fluentdData as object[])[0] as string;
userInitializedCategoryToTableNameMappings.TryGetValue(mapping.Key, out var expectedTableNme);
Assert.Equal(expectedTableNme, actualTableName);
logRecordList.Clear();
}
}

// Verify that when the "*" = "*" were enabled, the correct table names were being deduced following the set of rules.
foreach (var mapping in expectedCategoryToTableNameList)
{
passThruTableMappingsLogger = loggerFactory.CreateLogger(mapping.Key);
passThruTableMappingsLogger.LogInformation("This information does not matter.");
Assert.Single(logRecordList);
m_buffer = typeof(GenevaLogExporter).GetField("m_buffer", BindingFlags.NonPublic | BindingFlags.Static).GetValue(exporter) as ThreadLocal<byte[]>;
_ = exporter.SerializeLogRecord(logRecordList[0]);
fluentdData = MessagePack.MessagePackSerializer.Deserialize<object>(m_buffer.Value, MessagePack.Resolvers.ContractlessStandardResolver.Instance);
actualTableName = (fluentdData as object[])[0] as string;

string expectedTableName = string.Empty;
if (expectedCategoryToTableNameList.Contains(new KeyValuePair<string, string>(mapping.Key, mapping.Value)))
Yun-Ting marked this conversation as resolved.
Show resolved Hide resolved
{
expectedTableName = mapping.Value;
}

Assert.Equal(expectedTableName, actualTableName);
logRecordList.Clear();
}
}

[Theory]
[InlineData(true)]
[InlineData(false)]
Expand Down