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 6 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
16 changes: 16 additions & 0 deletions src/OpenTelemetry.Exporter.Geneva/GenevaBaseExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// limitations under the License.
// </copyright>

using System;
using System.Collections.Generic;

namespace OpenTelemetry.Exporter.Geneva;
Expand Down Expand Up @@ -89,4 +90,19 @@ internal static int AddPartAField(byte[] buffer, int cursor, string name, object
cursor = MessagePackSerializer.Serialize(buffer, cursor, value);
return cursor;
}

internal static int AddPartAField(byte[] buffer, int cursor, string name, Span<byte> span, int size)
{
if (V40_PART_A_MAPPING.TryGetValue(name, out string replacementKey))
{
cursor = MessagePackSerializer.SerializeAsciiString(buffer, cursor, replacementKey);
}
else
{
cursor = MessagePackSerializer.SerializeUnicodeString(buffer, cursor, name);
}

cursor = MessagePackSerializer.Serialize(buffer, cursor, span, size);
return cursor;
}
}
127 changes: 115 additions & 12 deletions src/OpenTelemetry.Exporter.Geneva/GenevaLogExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class GenevaLogExporter : GenevaBaseExporter<LogRecord>
};

private readonly IDataTransport m_dataTransport;
private readonly bool shouldPassThruTableMappings;
private bool isDisposed;
private Func<object, string> convertToJson;

Expand All @@ -65,7 +66,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 +212,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 +242,45 @@ 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;

Span<byte> data = stackalloc byte[0];
int validNameLength = 0;

// 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 (categoryName.Length > 0)
{
int cursorStartIdx = cursor;
cursor = Sanitize(buffer, cursor, ref cursorStartIdx, ref validNameLength, categoryName);
if (validNameLength > 0)
{
Yun-Ting marked this conversation as resolved.
Show resolved Hide resolved
data = stackalloc byte[validNameLength + 2];
for (int i = 0; i < validNameLength + 2; i++)
Yun-Ting marked this conversation as resolved.
Show resolved Hide resolved
{
data[i] = buffer[cursorStartIdx + i];
}
}
else
{
Yun-Ting marked this conversation as resolved.
Show resolved Hide resolved
cursor = MessagePackSerializer.SerializeNull(buffer, cursor);
}
}
else
{
cursor = MessagePackSerializer.SerializeNull(buffer, cursor);
}

cursor = MessagePackSerializer.WriteArrayHeader(buffer, cursor, 1);
cursor = MessagePackSerializer.WriteArrayHeader(buffer, cursor, 2);
cursor = MessagePackSerializer.SerializeUtcDateTime(buffer, cursor, timestamp);
Expand Down Expand Up @@ -282,7 +320,15 @@ internal int SerializeLogRecord(LogRecord logRecord)
}

// Part A - core envelope
cursor = AddPartAField(buffer, cursor, Schema.V40.PartA.Name, eventName);
if (!data.IsEmpty)
{
cursor = AddPartAField(buffer, cursor, Schema.V40.PartA.Name, data, validNameLength + 2);
}
else
{
cursor = AddPartAField(buffer, cursor, Schema.V40.PartA.Name, eventName);
}

cntFields += 1;

cursor = AddPartAField(buffer, cursor, Schema.V40.PartA.Time, 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,62 @@ 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.WriteCategoryNameHeader(buffer, cursorStartIdx, validNameLength);
}

return cursor;
}
}
#endif
24 changes: 24 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 WriteCategoryNameHeader(byte[] buffer, int nameStartIdx, int validNameLength)
{
buffer[nameStartIdx] = STR8;
buffer[nameStartIdx + 1] = unchecked((byte)validNameLength);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int SerializeAsciiString(byte[] buffer, int cursor, string value)
{
Expand Down Expand Up @@ -447,6 +454,18 @@ public static int SerializeArray<T>(byte[] buffer, int cursor, T[] array)
return cursor;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]

public static int SerializeSpan(byte[] buffer, int cursor, Span<byte> span, int size)
Yun-Ting marked this conversation as resolved.
Show resolved Hide resolved
{
for (int i = 0; i < size; ++i)
{
buffer[cursor++] = span[i];
}

return cursor;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int WriteMapHeader(byte[] buffer, int cursor, int count)
{
Expand Down Expand Up @@ -571,4 +590,9 @@ public static int Serialize(byte[] buffer, int cursor, object obj)
return SerializeUnicodeString(buffer, cursor, repr);
}
}

public static int Serialize(byte[] buffer, int cursor, Span<byte> span, int size)
{
return SerializeSpan(buffer, cursor, span, size);
}
}
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);
}
}
}
Loading