Skip to content

Commit

Permalink
Cleanup auditor just a tad
Browse files Browse the repository at this point in the history
  • Loading branch information
Mpdreamz committed Nov 1, 2024
1 parent 95ea6c6 commit 111754f
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 123 deletions.
9 changes: 0 additions & 9 deletions src/Elastic.Transport/Components/Pipeline/RequestPipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,6 @@ private async ValueTask<TResponse> CallProductEndpointCoreAsync<TResponse>(
{
using var audit = auditor?.Add(HealthyResponse, _dateTimeProvider, endpoint.Node);

if (audit is not null)
audit.PathAndQuery = endpoint.PathAndQuery;

try
{
TResponse response;
Expand Down Expand Up @@ -417,9 +414,6 @@ private async ValueTask PingCoreAsync(bool isAsync, Node node, Auditor? auditor,

using var audit = auditor?.Add(PingSuccess, _dateTimeProvider, node);

if (audit is not null)
audit.PathAndQuery = pingEndpoint.PathAndQuery;

TransportResponse response;

try
Expand Down Expand Up @@ -468,9 +462,6 @@ private async ValueTask SniffCoreAsync(bool isAsync, Auditor? auditor, Cancellat
var sniffEndpoint = _productRegistration.CreateSniffEndpoint(node, PingAndSniffRequestConfiguration, _settings);
using var audit = auditor?.Add(SniffSuccess, _dateTimeProvider, node);

if (audit is not null)
audit.PathAndQuery = sniffEndpoint.PathAndQuery;

Tuple<TransportResponse, IReadOnlyCollection<Node>> result;

try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ public TransportConfiguration(ITransportConfiguration config)
// it's important url formatter is repointed to the new instance of ITransportConfiguration
UrlFormatter = new UrlFormatter(this);


Accept = config.Accept;
AllowedStatusCodes = config.AllowedStatusCodes;
Authentication = config.Authentication;
Expand Down
4 changes: 2 additions & 2 deletions src/Elastic.Transport/Diagnostics/AuditDiagnosticObserver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal sealed class AuditDiagnosticObserver : TypedDiagnosticObserver<Audit>
/// <inheritdoc cref="AuditDiagnosticObserver"/>
public AuditDiagnosticObserver(
Action<KeyValuePair<string, Audit>> onNext,
Action<Exception> onError = null,
Action onCompleted = null
Action<Exception>? onError = null,
Action? onCompleted = null
) : base(onNext, onError, onCompleted) { }
}
9 changes: 3 additions & 6 deletions src/Elastic.Transport/Diagnostics/Auditing/Audit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ internal Audit(AuditEvent type, DateTimeOffset started)
/// </summary>
public Node? Node { get; internal init; }

/// <summary>
/// The path of the request.
/// </summary>
public string PathAndQuery { get; internal set; }

/// <summary>
/// The end date and time of the audit.
/// </summary>
Expand All @@ -55,6 +50,8 @@ public override string ToString()
var tookString = string.Empty;
if (took >= TimeSpan.Zero) tookString = $" Took: {took}";

return Node == null ? $"Event: {Event.GetStringValue()}{tookString}" : $"Event: {Event.GetStringValue()} Node: {Node?.Uri} NodeAlive: {Node?.IsAlive}Took: {tookString}";
return Node == null
? $"Event: {Event.GetStringValue()}{tookString}"
: $"Event: {Event.GetStringValue()} Node: {Node?.Uri} NodeAlive: {Node?.IsAlive}Took: {tookString}";
}
}
43 changes: 0 additions & 43 deletions src/Elastic.Transport/Diagnostics/Auditing/AuditEventExtensions.cs

This file was deleted.

45 changes: 0 additions & 45 deletions src/Elastic.Transport/Diagnostics/Auditing/Auditable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,9 @@
// See the LICENSE file in the project root for more information

using System;
using System.Collections;
using System.Collections.Generic;
using Elastic.Transport.Extensions;

namespace Elastic.Transport.Diagnostics.Auditing;

/// Collects <see cref="Audit"/> events
public class Auditor : IReadOnlyCollection<Audit>
{
private readonly DateTimeProvider _dateTimeProvider;
private List<Audit>? _audits;

internal Auditor(DateTimeProvider dateTimeProvider) => _dateTimeProvider = dateTimeProvider;

/// <inheritdoc cref="IEnumerable{T}.GetEnumerator"/>
public IEnumerator<Audit> GetEnumerator() =>
_audits?.GetEnumerator() ?? (IEnumerator<Audit>)new EmptyEnumerator<Audit>();

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

internal Auditable Add(Auditable auditable)
{
_audits ??= new List<Audit>();
_audits.Add(auditable.Audit);
return auditable;
}
internal Auditable Add(AuditEvent type, DateTimeProvider dateTimeProvider, Node? node = null)
{
_audits ??= new List<Audit>();
var auditable = new Auditable(type, dateTimeProvider, node);
_audits.Add(auditable.Audit);
return auditable;
}

/// Emits an event that does not need to track a duration
public void Emit(AuditEvent type) => Add(type, _dateTimeProvider).Dispose();
/// Emits an event that does not need to track a duration
public void Emit(AuditEvent type, Node node) => Add(type, _dateTimeProvider, node).Dispose();

/// <inheritdoc cref="IReadOnlyCollection{T}.Count"/>
public int Count => _audits?.Count ?? 0;
}

internal class Auditable : IDisposable
{
private readonly Audit _audit;
Expand Down Expand Up @@ -73,11 +33,6 @@ public Exception Exception
set => Audit.Exception = value;
}

public string PathAndQuery
{
set => Audit.PathAndQuery = value;
}

public Audit Audit => _audit;

public void Dispose() => Audit.Ended = _dateTimeProvider.Now();
Expand Down
48 changes: 48 additions & 0 deletions src/Elastic.Transport/Diagnostics/Auditing/Auditor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information

using System.Collections;
using System.Collections.Generic;
using Elastic.Transport.Extensions;

namespace Elastic.Transport.Diagnostics.Auditing;

/// Collects <see cref="Audit"/> events
public class Auditor : IReadOnlyCollection<Audit>
{
private readonly DateTimeProvider _dateTimeProvider;
private List<Audit>? _audits;

internal Auditor(DateTimeProvider dateTimeProvider) => _dateTimeProvider = dateTimeProvider;

/// <inheritdoc cref="IEnumerable{T}.GetEnumerator"/>
public IEnumerator<Audit> GetEnumerator() =>
_audits?.GetEnumerator() ?? (IEnumerator<Audit>)new EmptyEnumerator<Audit>();

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

internal Auditable Add(Auditable auditable)
{
_audits ??= new List<Audit>();
_audits.Add(auditable.Audit);
return auditable;
}

internal Auditable Add(AuditEvent type, DateTimeProvider dateTimeProvider, Node? node = null)
{
_audits ??= new List<Audit>();
var auditable = new Auditable(type, dateTimeProvider, node);
_audits.Add(auditable.Audit);
return auditable;
}

/// Emits an event that does not need to track a duration
public void Emit(AuditEvent type) => Add(type, _dateTimeProvider).Dispose();

/// Emits an event that does not need to track a duration
public void Emit(AuditEvent type, Node node) => Add(type, _dateTimeProvider, node).Dispose();

/// <inheritdoc cref="IReadOnlyCollection{T}.Count"/>
public int Count => _audits?.Count ?? 0;
}
34 changes: 17 additions & 17 deletions src/Elastic.Transport/Diagnostics/TypedDiagnosticObserver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ namespace Elastic.Transport.Diagnostics;
internal abstract class TypedDiagnosticObserver<TOnNext> : IObserver<KeyValuePair<string, object>>
{
private readonly Action<KeyValuePair<string, TOnNext>> _onNext;
private readonly Action<Exception> _onError;
private readonly Action _onCompleted;
private readonly Action<Exception>? _onError;
private readonly Action? _onCompleted;

/// <inheritdoc cref="TypedDiagnosticObserver{TOnNext}"/>
protected TypedDiagnosticObserver(
Action<KeyValuePair<string, TOnNext>> onNext,
Action<Exception> onError = null,
Action onCompleted = null
Action<Exception>? onError = null,
Action? onCompleted = null
)
{
_onNext= onNext;
_onNext= onNext ?? throw new ArgumentNullException(nameof(onNext));
_onError = onError;
_onCompleted = onCompleted;
}
Expand All @@ -36,8 +36,8 @@ protected TypedDiagnosticObserver(

void IObserver<KeyValuePair<string, object>>.OnNext(KeyValuePair<string, object> value)
{
if (value.Value is TOnNext next) _onNext?.Invoke(new KeyValuePair<string, TOnNext>(value.Key, next));
else if (value.Value == null) _onNext?.Invoke(new KeyValuePair<string, TOnNext>(value.Key, default));
if (value.Value is TOnNext next) _onNext.Invoke(new KeyValuePair<string, TOnNext>(value.Key, next));
else if (value.Value == null) _onNext.Invoke(new KeyValuePair<string, TOnNext>(value.Key, default));

else throw new Exception($"{value.Key} received unexpected type {value.Value.GetType()}");

Expand All @@ -49,19 +49,19 @@ public abstract class TypedDiagnosticObserver<TOnNextStart, TOnNextEnd> : IObser
{
private readonly Action<KeyValuePair<string, TOnNextStart>> _onNextStart;
private readonly Action<KeyValuePair<string, TOnNextEnd>> _onNextEnd;
private readonly Action<Exception> _onError;
private readonly Action _onCompleted;
private readonly Action<Exception>? _onError;
private readonly Action? _onCompleted;

/// <inheritdoc cref="TypedDiagnosticObserver{TOnNext}"/>
protected TypedDiagnosticObserver(
Action<KeyValuePair<string, TOnNextStart>> onNextStart,
Action<KeyValuePair<string, TOnNextEnd>> onNextEnd,
Action<Exception> onError = null,
Action onCompleted = null
Action<Exception>? onError = null,
Action? onCompleted = null
)
{
_onNextStart = onNextStart;
_onNextEnd = onNextEnd;
_onNextStart = onNextStart ?? throw new ArgumentNullException(nameof(onNextStart));
_onNextEnd = onNextEnd ?? throw new ArgumentNullException(nameof(onNextEnd));
_onError = onError;
_onCompleted = onCompleted;
}
Expand All @@ -72,11 +72,11 @@ protected TypedDiagnosticObserver(

void IObserver<KeyValuePair<string, object>>.OnNext(KeyValuePair<string, object> value)
{
if (value.Value is TOnNextStart nextStart) _onNextStart?.Invoke(new KeyValuePair<string, TOnNextStart>(value.Key, nextStart));
else if (value.Key.EndsWith(".Start") && value.Value is null) _onNextStart?.Invoke(new KeyValuePair<string, TOnNextStart>(value.Key, default));
if (value.Value is TOnNextStart nextStart) _onNextStart.Invoke(new KeyValuePair<string, TOnNextStart>(value.Key, nextStart));
else if (value.Key.EndsWith(".Start") && value.Value is null) _onNextStart.Invoke(new KeyValuePair<string, TOnNextStart>(value.Key, default));

else if (value.Value is TOnNextEnd nextEnd) _onNextEnd?.Invoke(new KeyValuePair<string, TOnNextEnd>(value.Key, nextEnd));
else if (value.Key.EndsWith(".Stop") && value.Value is null) _onNextEnd?.Invoke(new KeyValuePair<string, TOnNextEnd>(value.Key, default));
else if (value.Value is TOnNextEnd nextEnd) _onNextEnd.Invoke(new KeyValuePair<string, TOnNextEnd>(value.Key, nextEnd));
else if (value.Key.EndsWith(".Stop") && value.Value is null) _onNextEnd.Invoke(new KeyValuePair<string, TOnNextEnd>(value.Key, default));

else throw new Exception($"{value.Key} received unexpected type {value.Value.GetType()}");

Expand Down

0 comments on commit 111754f

Please sign in to comment.