Skip to content

Commit

Permalink
Get coverage to 100%
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Taillefer committed Jul 21, 2023
1 parent 814728a commit eafbc67
Show file tree
Hide file tree
Showing 18 changed files with 127 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Diagnostics.CodeAnalysis;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Shared.DiagnosticIds;
using Microsoft.Shared.Diagnostics;

namespace Microsoft.Extensions.Telemetry.Enrichment;
Expand Down Expand Up @@ -50,7 +51,7 @@ public static IServiceCollection AddLogEnricher(this IServiceCollection services
/// <typeparam name="T">Enricher type.</typeparam>
/// <returns>The value of <paramref name="services"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="services"/> is <see langword="null"/>.</exception>
[Experimental(diagnosticId: "TBD", UrlFormat = "TBD")]
[Experimental(diagnosticId: Experiments.Telemetry, UrlFormat = Experiments.UrlFormat)]
public static IServiceCollection AddStaticLogEnricher<T>(this IServiceCollection services)
where T : class, IStaticLogEnricher
{
Expand All @@ -66,7 +67,7 @@ public static IServiceCollection AddStaticLogEnricher<T>(this IServiceCollection
/// <param name="enricher">The enricher instance to add.</param>
/// <returns>The value of <paramref name="services"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="services"/> or <paramref name="enricher"/> are <see langword="null"/>.</exception>
[Experimental(diagnosticId: "TBD", UrlFormat = "TBD")]
[Experimental(diagnosticId: Experiments.Telemetry, UrlFormat = Experiments.UrlFormat)]
public static IServiceCollection AddStaticLogEnricher(this IServiceCollection services, IStaticLogEnricher enricher)
{
_ = Throw.IfNull(services);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics.CodeAnalysis;
using Microsoft.Shared.DiagnosticIds;

namespace Microsoft.Extensions.Telemetry.Enrichment;

/// <summary>
/// A component that augments log records with additional properties which are unchanging over the life of the object.
/// </summary>
[Experimental(diagnosticId: "TBD", UrlFormat = "TBD")]
[Experimental(diagnosticId: Experiments.Telemetry, UrlFormat = Experiments.UrlFormat)]
public interface IStaticLogEnricher
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Diagnostics.CodeAnalysis;
using Microsoft.Extensions.Compliance.Classification;

Check failure on line 6 in src/Libraries/Microsoft.Extensions.Telemetry.Abstractions/Logging/ILogPropertyCollector.cs

View check run for this annotation

Azure Pipelines / extensions-ci (Correctness WarningsCheck)

src/Libraries/Microsoft.Extensions.Telemetry.Abstractions/Logging/ILogPropertyCollector.cs#L6

src/Libraries/Microsoft.Extensions.Telemetry.Abstractions/Logging/ILogPropertyCollector.cs(6,28): error CS0234: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'Compliance' does not exist in the namespace 'Microsoft.Extensions' (are you missing an assembly reference?)

Check failure on line 6 in src/Libraries/Microsoft.Extensions.Telemetry.Abstractions/Logging/ILogPropertyCollector.cs

View check run for this annotation

Azure Pipelines / extensions-ci (Build Ubuntu)

src/Libraries/Microsoft.Extensions.Telemetry.Abstractions/Logging/ILogPropertyCollector.cs#L6

src/Libraries/Microsoft.Extensions.Telemetry.Abstractions/Logging/ILogPropertyCollector.cs(6,28): error CS0234: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'Compliance' does not exist in the namespace 'Microsoft.Extensions' (are you missing an assembly reference?)
using Microsoft.Shared.DiagnosticIds;

namespace Microsoft.Extensions.Telemetry.Logging;

Expand Down Expand Up @@ -36,6 +37,6 @@ public interface ILogPropertyCollector
/// <exception cref="ArgumentException"><paramref name="propertyName" /> is empty or contains exclusively whitespace,
/// or when a property of the same name has already been added.
/// </exception>
[Experimental(diagnosticId: "TBD", UrlFormat = "TBD")]
[Experimental(diagnosticId: Experiments.Telemetry, UrlFormat = Experiments.UrlFormat)]
void Add(string propertyName, object? propertyValue, DataClassification classification);

Check failure on line 41 in src/Libraries/Microsoft.Extensions.Telemetry.Abstractions/Logging/ILogPropertyCollector.cs

View check run for this annotation

Azure Pipelines / extensions-ci (Correctness WarningsCheck)

src/Libraries/Microsoft.Extensions.Telemetry.Abstractions/Logging/ILogPropertyCollector.cs#L41

src/Libraries/Microsoft.Extensions.Telemetry.Abstractions/Logging/ILogPropertyCollector.cs(41,58): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'DataClassification' could not be found (are you missing a using directive or an assembly reference?)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.ObjectPool;
using Microsoft.Extensions.Telemetry.Enrichment;
using Microsoft.Shared.DiagnosticIds;
using Microsoft.Shared.Diagnostics;
using Microsoft.Shared.Pools;

Expand Down Expand Up @@ -37,7 +38,7 @@ public void Add(string propertyName, object? propertyValue)
}

/// <inheritdoc/>
[Experimental(diagnosticId: "TBD", UrlFormat = "TBD")]
[Experimental(diagnosticId: Experiments.Telemetry, UrlFormat = Experiments.UrlFormat)]
public void Add(string propertyName, object? propertyValue, DataClassification classification) => Add(propertyName, propertyValue);

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using Microsoft.Extensions.ObjectPool;
using Microsoft.Shared.DiagnosticIds;
using Microsoft.Shared.Pools;

namespace Microsoft.Extensions.Telemetry.Logging;
Expand All @@ -15,7 +16,7 @@ namespace Microsoft.Extensions.Telemetry.Logging;
/// Utility type to support generated logging methods.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
[Experimental(diagnosticId: "TBD", UrlFormat = "TBD")]
[Experimental(diagnosticId: Experiments.Telemetry, UrlFormat = Experiments.UrlFormat)]
public static class LoggerMessageHelper
{
[ThreadStatic]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,33 @@
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using Microsoft.Extensions.Logging;
using Microsoft.Shared.DiagnosticIds;
using Microsoft.Shared.Pools;

namespace Microsoft.Extensions.Telemetry.Logging;

/// <summary>
/// Additional state to use with <see cref="ILogger.Log"/>.
/// </summary>
[Experimental(diagnosticId: "TBD", UrlFormat = "TBD")]
[Experimental(diagnosticId: Experiments.Telemetry, UrlFormat = Experiments.UrlFormat)]
[EditorBrowsable(EditorBrowsableState.Never)]
public sealed partial class LoggerMessageState
{
private KeyValuePair<string, object?>[] _properties = Array.Empty<KeyValuePair<string, object?>>();
private KeyValuePair<string, object?>[] _redactedProperties = Array.Empty<KeyValuePair<string, object?>>();
private ClassifiedProperty[] _classifiedProperties = Array.Empty<ClassifiedProperty>();

#pragma warning disable CA1819 // Properties should not return arrays
/// <summary>
/// Gets the array of properties.
/// </summary>
#pragma warning disable CA1819 // Properties should not return arrays
public KeyValuePair<string, object?>[] PropertyArray => _properties;

/// <summary>
/// Gets the array of properties.
/// </summary>
public KeyValuePair<string, object?>[] RedactedPropertyArray => _redactedProperties;

/// <summary>
/// Gets the array of classified properties.
/// </summary>
Expand All @@ -51,6 +58,25 @@ public int EnsurePropertySpace(int count)
return index;
}

/// <summary>
/// Allocates some room to put some redacted properties.
/// </summary>
/// <param name="count">The amount of space to allocate.</param>
/// <returns>The index in the <see cref="RedactedPropertyArray"/> where to store the properties.</returns>
public int EnsureRedactedPropertySpace(int count)
{
int avail = _redactedProperties.Length - NumRedactedProperties;
if (count > avail)
{
var need = _redactedProperties.Length + (count - avail);
Array.Resize(ref _redactedProperties, need);
}

var index = NumRedactedProperties;
NumRedactedProperties += count;
return index;
}

/// <summary>
/// Allocates some room to put some properties.
/// </summary>
Expand All @@ -76,8 +102,10 @@ public int EnsureClassifiedPropertySpace(int count)
public void Clear()
{
Array.Clear(_properties, 0, NumProperties);
Array.Clear(_redactedProperties, 0, NumRedactedProperties);
Array.Clear(_classifiedProperties, 0, NumClassifiedProperties);
NumProperties = 0;
NumRedactedProperties = 0;
NumClassifiedProperties = 0;
PropertyNamePrefix = string.Empty;
}
Expand All @@ -87,6 +115,11 @@ public void Clear()
/// </summary>
public int NumProperties { get; private set; }

/// <summary>
/// Gets a value indicating the number of redacted properties currently in this instance.
/// </summary>
public int NumRedactedProperties { get; private set; }

/// <summary>
/// Gets a value indicating the number of classified properties currently in this instance.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

<PropertyGroup>
<Stage>normal</Stage>
<MinCodeCoverage>99</MinCodeCoverage>
<MinCodeCoverage>100</MinCodeCoverage>
<MinMutationScore>100</MinMutationScore>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ internal sealed class ModernPropertyJoiner : IReadOnlyList<KeyValuePair<string,
private const int PropCapacity = 4;
private readonly List<KeyValuePair<string, object?>> _extraProperties = new(PropCapacity);
private KeyValuePair<string, object?>[]? _incomingProperties;
private KeyValuePair<string, object?>[]? _redactedProperties;
private int _incomingPropertiesCount;
private int _redactedPropertiesCount;

public ModernPropertyJoiner()
{
Expand All @@ -35,6 +37,7 @@ public void Clear()
{
_extraProperties.Clear();
_incomingProperties = null;
_redactedProperties = null;
State = null;
Formatter = null;
}
Expand All @@ -44,6 +47,9 @@ public void SetIncomingProperties(LoggerMessageState value)
{
_incomingProperties = value.PropertyArray;
_incomingPropertiesCount = value.NumProperties;

_redactedProperties = value.RedactedPropertyArray;
_redactedPropertiesCount = value.NumRedactedProperties;
}

public KeyValuePair<string, object?> this[int index]
Expand All @@ -54,18 +60,22 @@ public void SetIncomingProperties(LoggerMessageState value)
{
return _incomingProperties![index];
}
else if (index < _incomingPropertiesCount + _extraProperties.Count)
else if (index < _incomingPropertiesCount + _redactedPropertiesCount)
{
return _redactedProperties![index - _incomingPropertiesCount];
}
else if (index < _incomingPropertiesCount + _redactedPropertiesCount + _extraProperties.Count)
{
return _extraProperties[index - _incomingPropertiesCount];
return _extraProperties[index - _incomingPropertiesCount - _redactedPropertiesCount];
}
else
{
return StaticProperties![index - _incomingPropertiesCount - _extraProperties.Count];
return StaticProperties![index - _incomingPropertiesCount - _redactedPropertiesCount - _extraProperties.Count];
}
}
}

public int Count => _incomingPropertiesCount + _extraProperties.Count + StaticProperties!.Length;
public int Count => _incomingPropertiesCount + _redactedPropertiesCount + _extraProperties.Count + StaticProperties!.Length;

public IEnumerator<KeyValuePair<string, object?>> GetEnumerator()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,6 @@ private void ModernPath(LogLevel logLevel, EventId eventId, LoggerMessageState m
var loggers = MessageLoggers;
var config = _factory.Config;

var joiner = ModernJoiner;
joiner.StaticProperties = config.StaticProperties;
joiner.Formatter = formatter;
joiner.SetIncomingProperties(msgState);

List<Exception>? exceptions = null;

// redact
Expand All @@ -210,7 +205,8 @@ private void ModernPath(LogLevel logLevel, EventId eventId, LoggerMessageState m
jr.Next = jitRedactors;
jitRedactors = jr;

joiner.PropertyBag.Add(cp.Name, jr);
var index = msgState.EnsureRedactedPropertySpace(1);
msgState.RedactedPropertyArray[index] = new(cp.Name, jr);
}
catch (Exception ex)
{
Expand All @@ -219,6 +215,11 @@ private void ModernPath(LogLevel logLevel, EventId eventId, LoggerMessageState m
}
}

var joiner = ModernJoiner;
joiner.StaticProperties = config.StaticProperties;
joiner.Formatter = formatter;
joiner.SetIncomingProperties(msgState);

// enrich
foreach (var enricher in config.Enrichers)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options.Validation;
using Microsoft.Shared.DiagnosticIds;
using Microsoft.Shared.Diagnostics;

namespace Microsoft.Extensions.Telemetry.Logging;

/// <summary>
/// Extensions for configuring logging enrichment features.
/// </summary>
[Experimental(diagnosticId: "TBD", UrlFormat = "TBD")]
[Experimental(diagnosticId: Experiments.Telemetry, UrlFormat = Experiments.UrlFormat)]
public static class LoggingEnrichmentExtensions
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
using Microsoft.Shared.DiagnosticIds;
using Microsoft.Shared.Diagnostics;

namespace Microsoft.Extensions.Telemetry.Logging;

/// <summary>
/// Extensions for configuring logging redaction features.
/// </summary>
[Experimental(diagnosticId: "TBD", UrlFormat = "TBD")]
[Experimental(diagnosticId: Experiments.Telemetry, UrlFormat = Experiments.UrlFormat)]
public static class LoggingRedactionExtensions
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using Microsoft.Extensions.Compliance.Classification;
using Microsoft.Extensions.Compliance.Redaction;
using Microsoft.Extensions.Compliance.Testing;
using Microsoft.Extensions.Telemetry.Enrichment.Test;
using Moq;
using Xunit;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@

using System;
using System.Collections.Generic;
using Microsoft.Extensions.Telemetry.Enrichment;

namespace Microsoft.Extensions.Telemetry.Enrichment.Test;
namespace Microsoft.AspNetCore.Telemetry.RequestHeaders.Test;

public class TestLogEnrichmentPropertyBag : IEnrichmentPropertyBag
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@

using System;
using System.Collections.Generic;
using Microsoft.Extensions.Telemetry.Enrichment;

namespace Microsoft.Extensions.Telemetry.Enrichment.Test;
namespace Microsoft.AspNetCore.Telemetry.Test.Enrichment.RequestHeaders.Tests;

public class TestMetricEnrichmentPropertyBag : IEnrichmentPropertyBag
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
<ProjectReference Include="..\..\..\src\Libraries\Microsoft.Extensions.Telemetry.Testing\Microsoft.Extensions.Telemetry.Testing.csproj" />
<ProjectReference Include="..\..\..\src\Libraries\Microsoft.Extensions.Compliance.Testing\Microsoft.Extensions.Compliance.Testing.csproj" />
<ProjectReference Include="..\..\..\src\Libraries\Microsoft.Extensions.Compliance.Redaction\Microsoft.Extensions.Compliance.Redaction.csproj" />
<ProjectReference Include="..\Microsoft.Extensions.Telemetry.Abstractions.Tests\Microsoft.Extensions.Telemetry.Abstractions.Tests.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit eafbc67

Please sign in to comment.