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

Design review to allow underscore in event name #2232

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ internal sealed class EventNameManager
// Note: OneCollector will silently drop events which have a name less than 4 characters.
internal const int MinimumEventFullNameLength = 4;
internal const int MaximumEventFullNameLength = 100;

private static readonly Regex EventNamespaceValidationRegex = new(@"^[A-Za-z](?:\.?[A-Za-z0-9]+?)*$", RegexOptions.Compiled);
private static readonly Regex EventNameValidationRegex = new(@"^[A-Za-z][A-Za-z0-9]*$", RegexOptions.Compiled);

Expand All @@ -20,6 +21,7 @@ internal sealed class EventNameManager
private readonly IReadOnlyDictionary<string, EventFullName>? eventFullNameMappings;
private readonly ResolvedEventFullName defaultEventFullName;
private readonly Hashtable eventNamespaceCache = new(StringComparer.OrdinalIgnoreCase);
private readonly Hashtable eventFullNameCache = new(StringComparer.OrdinalIgnoreCase);

public EventNameManager(
string defaultEventNamespace,
Expand All @@ -43,15 +45,43 @@ public EventNameManager(
#endif
}

// Note: This is exposed for unit tests.
// Note: These caches are exposed for unit tests.
internal Hashtable EventNamespaceCache => this.eventNamespaceCache;

internal Hashtable EventFullNameCache => this.eventFullNameCache;

public static bool IsEventNamespaceValid(string eventNamespace)
=> EventNamespaceValidationRegex.IsMatch(eventNamespace);

public static bool IsEventNameValid(string eventName)
=> EventNameValidationRegex.IsMatch(eventName);

public ResolvedEventFullName ResolveEventFullName(
string eventFullName)
{
if (this.eventFullNameCache[eventFullName] is ResolvedEventFullName cachedEventFullName)
{
return cachedEventFullName;
}

byte[] eventFullNameBlob = BuildEventFullName(string.Empty, eventFullName);

var resolvedEventFullName = new ResolvedEventFullName(
eventFullNameBlob,
originalEventNamespace: null,
originalEventName: null);

lock (this.eventFullNameCache)
{
if (this.eventFullNameCache[eventFullName] is null)
{
this.eventFullNameCache[eventFullName] = resolvedEventFullName;
}
}

return resolvedEventFullName;
}

public ResolvedEventFullName ResolveEventFullName(
string? eventNamespace,
string? eventName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,23 @@ protected override void SerializeItemToJson(Resource resource, LogRecord item, C

Debug.Assert(writer != null, "writer was null");

var resolvedEventFullName = this.eventNameManager.ResolveEventFullName(
item.CategoryName,
item.EventId.Name);
int attributeStartIndex = 0;
EventNameManager.ResolvedEventFullName resolvedEventFullName;
if (item.Attributes != null
&& item.Attributes.Count > 0
&& item.Attributes[0].Key == "{EventFullName}"
&& item.Attributes[0].Value is string eventFullName
&& !string.IsNullOrEmpty(eventFullName))
{
attributeStartIndex++;
resolvedEventFullName = this.eventNameManager.ResolveEventFullName(eventFullName);
}
else
{
resolvedEventFullName = this.eventNameManager.ResolveEventFullName(
item.CategoryName,
item.EventId.Name);
}

writer!.WriteStartObject();

Expand Down Expand Up @@ -150,7 +164,7 @@ protected override void SerializeItemToJson(Resource resource, LogRecord item, C

if (item.Attributes != null)
{
for (int i = 0; i < item.Attributes.Count; i++)
for (int i = attributeStartIndex; i < item.Attributes.Count; i++)
{
var attribute = item.Attributes[i];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,22 @@ public void EventNameCacheTest()
Assert.Single((eventNameManager.EventNamespaceCache["Test"] as Hashtable)!);
}

[Fact]
public void EventFullNameCacheTest()
{
var eventNameManager = BuildEventNameManagerWithDefaultOptions();

Assert.Empty(eventNameManager.EventFullNameCache);

eventNameManager.ResolveEventFullName("Company_Product_EventName");

Assert.Single(eventNameManager.EventFullNameCache);

eventNameManager.ResolveEventFullName("company_product_eventName");

Assert.Single(eventNameManager.EventFullNameCache);
}

[Fact]
public void EventFullNameMappedWhenEventNamespaceMatchesTest()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public void LogRecordScopesJsonTest()
}

[Fact]
public void LogRecordStateValuesJsonTest()
public void LogRecordAttributesJsonTest()
{
string json = GetLogRecordJson(1, (index, logRecord) =>
{
Expand All @@ -216,6 +216,24 @@ public void LogRecordStateValuesJsonTest()
json);
}

[Fact]
public void LogRecordAttributesWithEventFullNameJsonTest()
{
string json = GetLogRecordJson(1, (index, logRecord) =>
{
logRecord.Attributes = new List<KeyValuePair<string, object?>>
{
new KeyValuePair<string, object?>("{EventFullName}", "company_Product_EventName"),
new KeyValuePair<string, object?>("stateKey1", "stateValue1"),
new KeyValuePair<string, object?>("stateKey2", "stateValue2"),
};
});

Assert.Equal(
"""{"ver":"4.0","name":"Company_Product_EventName","time":"2032-01-18T10:11:12Z","iKey":"o:tenant-token","data":{"severityText":"Trace","severityNumber":1,"stateKey1":"stateValue1","stateKey2":"stateValue2"}}""" + "\n",
json);
}

[Fact]
public void LogRecordTraceContextJsonTest()
{
Expand Down