Skip to content

Commit

Permalink
Add app logging custom attributes settings (#1291)
Browse files Browse the repository at this point in the history
* Add logging custom attributes settings.

* Deserialize new logging settings.

* Add unit tests

* Report new configs.

* fix failing test.

* Update src/Agent/NewRelic/Agent/Core/Config/Configuration.xsd

Co-authored-by: Alex Hemsath <[email protected]>

* Update src/Agent/NewRelic/Agent/Core/Config/Configuration.xsd

Co-authored-by: Alex Hemsath <[email protected]>

* Fix another failing test.

* Update src/Agent/NewRelic/Agent/Core/Config/Configuration.xsd

Co-authored-by: Alex Hemsath <[email protected]>

* Update src/Agent/NewRelic/Agent/Core/Config/Configuration.xsd

Co-authored-by: Alex Hemsath <[email protected]>

Co-authored-by: Alex Hemsath <[email protected]>
  • Loading branch information
vuqtran88 and nr-ahemsath authored Oct 28, 2022
1 parent 3ab9337 commit 1eeaa2e
Show file tree
Hide file tree
Showing 9 changed files with 217 additions and 2 deletions.
91 changes: 91 additions & 0 deletions src/Agent/NewRelic/Agent/Core/Config/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5069,6 +5069,8 @@ public virtual configurationApplicationLoggingMetrics Clone()
public partial class configurationApplicationLoggingForwarding
{

private configurationApplicationLoggingForwardingContextData contextDataField;

private bool enabledField;

private int maxSamplesStoredField;
Expand All @@ -5078,10 +5080,23 @@ public partial class configurationApplicationLoggingForwarding
/// </summary>
public configurationApplicationLoggingForwarding()
{
this.contextDataField = new configurationApplicationLoggingForwardingContextData();
this.enabledField = true;
this.maxSamplesStoredField = 10000;
}

public configurationApplicationLoggingForwardingContextData contextData
{
get
{
return this.contextDataField;
}
set
{
this.contextDataField = value;
}
}

[System.Xml.Serialization.XmlAttributeAttribute()]
[System.ComponentModel.DefaultValueAttribute(true)]
public bool enabled
Expand Down Expand Up @@ -5121,6 +5136,82 @@ public virtual configurationApplicationLoggingForwarding Clone()
#endregion
}

[System.CodeDom.Compiler.GeneratedCodeAttribute("Xsd2Code", "3.6.0.20097")]
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="urn:newrelic-config")]
public partial class configurationApplicationLoggingForwardingContextData
{

private bool enabledField;

private string includeField;

private string excludeField;

/// <summary>
/// configurationApplicationLoggingForwardingContextData class constructor
/// </summary>
public configurationApplicationLoggingForwardingContextData()
{
this.enabledField = false;
this.includeField = "";
this.excludeField = "";
}

[System.Xml.Serialization.XmlAttributeAttribute()]
[System.ComponentModel.DefaultValueAttribute(false)]
public bool enabled
{
get
{
return this.enabledField;
}
set
{
this.enabledField = value;
}
}

[System.Xml.Serialization.XmlAttributeAttribute()]
[System.ComponentModel.DefaultValueAttribute("")]
public string include
{
get
{
return this.includeField;
}
set
{
this.includeField = value;
}
}

[System.Xml.Serialization.XmlAttributeAttribute()]
[System.ComponentModel.DefaultValueAttribute("")]
public string exclude
{
get
{
return this.excludeField;
}
set
{
this.excludeField = value;
}
}

#region Clone method
/// <summary>
/// Create a clone of this configurationApplicationLoggingForwardingContextData object
/// </summary>
public virtual configurationApplicationLoggingForwardingContextData Clone()
{
return ((configurationApplicationLoggingForwardingContextData)(this.MemberwiseClone()));
}
#endregion
}

[System.CodeDom.Compiler.GeneratedCodeAttribute("Xsd2Code", "3.6.0.20097")]
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
Expand Down
32 changes: 32 additions & 0 deletions src/Agent/NewRelic/Agent/Core/Config/Configuration.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -1615,6 +1615,38 @@
</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:sequence>
<xs:element name="contextData" minOccurs="0" maxOccurs="1">
<xs:annotation>
<xs:documentation>
Log context attributes filtering.
</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:attribute name="enabled" type="xs:boolean" default="false">
<xs:annotation>
<xs:documentation>
Controls whether or not context attributes filtering is used. Defaults to false.
</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="include" type="xs:string" default="">
<xs:annotation>
<xs:documentation>
A comma-separated list of case-sensitive strings that define the context attributes that should be added to the log record. Empty means include all.
</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="exclude" type="xs:string" default="">
<xs:annotation>
<xs:documentation>
A comma-separated list of case-sensitive strings that define the context attributes that should NOT be added to the log record. "exclude" wins over "include".
</xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="enabled" type="xs:boolean" default="true">
<xs:annotation>
<xs:documentation>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1881,6 +1881,35 @@ public virtual bool LogEventCollectorEnabled
}
}

public bool ContextDataEnabled
{
get
{
return LogEventCollectorEnabled &&
EnvironmentOverrides(_localConfiguration.applicationLogging.forwarding.contextData.enabled, "NEW_RELIC_APPLICATION_LOGGING_FORWARDING_CONTEXT_DATA_ENABLED");
}
}

public IEnumerable<string> ContextDataInclude
{
get
{
return EnvironmentOverrides(_localConfiguration.applicationLogging.forwarding.contextData.include,
"NEW_RELIC_APPLICATION_LOGGING_FORWARDING_CONTEXT_DATA_INCLUDE")
.Split(new[] { StringSeparators.CommaChar, ' ' }, StringSplitOptions.RemoveEmptyEntries);
}
}

public IEnumerable<string> ContextDataExclude
{
get
{
return EnvironmentOverrides(_localConfiguration.applicationLogging.forwarding.contextData.exclude,
"NEW_RELIC_APPLICATION_LOGGING_FORWARDING_CONTEXT_DATA_EXCLUDE")
.Split(new[] { StringSeparators.CommaChar, ' ' }, StringSplitOptions.RemoveEmptyEntries);
}
}

public TimeSpan LogEventsHarvestCycle
{
get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,16 @@ public ReportedConfiguration(IConfiguration configuration)

[JsonProperty("agent.app_settings")]
public IReadOnlyDictionary<string,string> AppSettings => GetAppSettings();

[JsonProperty("application_logging.forwarding.context_data.enabled")]
public bool ContextDataEnabled => _configuration.ContextDataEnabled;

[JsonProperty("application_logging.forwarding.context_data.include")]
public IEnumerable<string> ContextDataInclude => _configuration.ContextDataInclude;

[JsonProperty("application_logging.forwarding.context_data.exclude")]
public IEnumerable<string> ContextDataExclude => _configuration.ContextDataExclude;

public IReadOnlyDictionary<string, string> GetAppSettings()
{
return _configuration.GetAppSettings();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ public interface IConfiguration
int LogEventsMaxSamplesStored { get; }
TimeSpan LogEventsHarvestCycle { get; }
bool LogDecoratorEnabled { get; }
bool ContextDataEnabled { get; }
IEnumerable<string> ContextDataInclude { get; }
IEnumerable<string> ContextDataExclude { get; }
bool AppDomainCachingDisabled { get; }
bool ForceNewTransactionOnNewThread { get; }
bool CodeLevelMetricsEnabled { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2747,6 +2747,50 @@ public void LogEventsHarvestCycleUsesDefaultOrEventHarvestConfig()
Assert.AreEqual(10, _defaultConfig.LogEventsHarvestCycle.Seconds);
}

[Test]
public void ApplicationLogging_ContextDataEnabled_IsFalseInLocalConfigByDefault()
{
Assert.IsFalse(_defaultConfig.ContextDataEnabled);
}

[TestCase(null, null, ExpectedResult = new string[] { })]
[TestCase("aaa,bbb", "ccc,ddd", ExpectedResult = new[] { "ccc", "ddd" })]
[TestCase("aaa,bbb", null, ExpectedResult = new[] { "aaa", "bbb" })]
[TestCase(null, "ccc,ddd", ExpectedResult = new[] { "ccc", "ddd" })]
public IEnumerable<string> ApplicationLogging_ContextDataInclude_IsOverriddenByEnvironmentVariable(string local, string environment)
{
if (local != null)
{
_localConfig.applicationLogging.forwarding.contextData.include = local;
}

if (environment != null)
{
Mock.Arrange(() => _environment.GetEnvironmentVariable("NEW_RELIC_APPLICATION_LOGGING_FORWARDING_CONTEXT_DATA_INCLUDE")).Returns(environment);
}

return _defaultConfig.ContextDataInclude;
}

[TestCase(null, null, ExpectedResult = new string[] {})]
[TestCase("aaa,bbb", "ccc,ddd", ExpectedResult = new[] { "ccc", "ddd" })]
[TestCase("aaa,bbb", null, ExpectedResult = new[] { "aaa", "bbb" })]
[TestCase(null, "ccc,ddd", ExpectedResult = new[] { "ccc","ddd" })]

public IEnumerable<string> ApplicationLogging_ContextDataExclude_IsOverriddenByEnvironmentVariable(string local, string environment)
{
if(local != null)
{
_localConfig.applicationLogging.forwarding.contextData.exclude = local;
}

if(environment != null)
{
Mock.Arrange(() => _environment.GetEnvironmentVariable("NEW_RELIC_APPLICATION_LOGGING_FORWARDING_CONTEXT_DATA_EXCLUDE")).Returns(environment);
}

return _defaultConfig.ContextDataExclude;
}
#endregion


Expand Down
Loading

0 comments on commit 1eeaa2e

Please sign in to comment.