Skip to content

Commit

Permalink
Cache UserName and Domain to improve performance when creating new Ec…
Browse files Browse the repository at this point in the history
…sDocument (#287)

Co-authored-by: Lightning Unicorn <lightning.unicorn.d@gmail.com>
Co-authored-by: Martijn Laarman <Mpdreamz@gmail.com>
  • Loading branch information
3 people authored Mar 28, 2023
1 parent e2b8208 commit e9e2bf0
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/Elastic.CommonSchema/EcsDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ public static TEcsDocument CreateNewWithDefaults<TEcsDocument>(

if (options?.IncludeHost is null or true) doc.Host = GetHost();
if (options?.IncludeProcess is null or true) doc.Process = GetProcess();
// TODO I think we can cache user? does CurrentPrincipal on Thread ever change?
if (options?.IncludeUser is null or true)
doc.User = new User { Id = Thread.CurrentPrincipal?.Identity.Name, Name = Environment.UserName, Domain = Environment.UserDomainName };
if (options?.IncludeUser is null or true) doc.User = GetUser();

return doc;
}
Expand Down Expand Up @@ -157,6 +155,12 @@ private static Process GetProcess()
};
}

private static readonly string UserName = Environment.UserName;
private static readonly string UserDomainName = Environment.UserDomainName;

//Can not cache current thread's identity as it's used for role based security, different threads can have different identities
private static User GetUser() => new User { Id = Thread.CurrentPrincipal?.Identity.Name, Name = UserName, Domain = UserDomainName };

private static Error GetError(Exception exception)
{
if (exception == null)
Expand Down

0 comments on commit e9e2bf0

Please sign in to comment.