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

Feature - add timestamp to metric telemetry tracking #123

Merged
Merged
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 @@ -60,6 +60,7 @@ public static class MetricTracking
{
public const string MetricName = "MetricName";
public const string MetricValue = "MetricValue";
public const string Timestamp = "Timestamp";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ public static class ILoggerExtensions
private const string MetricFormat =
MessagePrefixes.Metric + " {"
+ ContextProperties.MetricTracking.MetricName + "}: {"
+ ContextProperties.MetricTracking.MetricValue
+ ContextProperties.MetricTracking.MetricValue + "} at {"
+ ContextProperties.MetricTracking.Timestamp
+ "} (Context: {@" + ContextProperties.EventTracking.EventContext + "})";

/// <summary>
Expand Down Expand Up @@ -669,7 +670,25 @@ public static void LogMetric(this ILogger logger, string name, double value, Dic

context = context ?? new Dictionary<string, object>();

logger.LogInformation(MetricFormat, name, value, context);
LogMetric(logger, name, value, DateTimeOffset.UtcNow, context);
}

/// <summary>
/// Logs a custom metric
/// </summary>
/// <param name="logger">Logger to use</param>
/// <param name="name">Name of the metric</param>
/// <param name="value">Value of the metric</param>
/// <param name="timestamp">Timestamp of the metric</param>
/// <param name="context">Context that provides more insights on the event that occured</param>
public static void LogMetric(this ILogger logger, string name, double value, DateTimeOffset timestamp, Dictionary<string, object> context = null)
{
Guard.NotNull(logger, nameof(logger));
Guard.NotNullOrWhitespace(name, nameof(name));

context = context ?? new Dictionary<string, object>();

logger.LogInformation(MetricFormat, name, value, timestamp.ToString(CultureInfo.InvariantCulture), context);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@ protected override MetricTelemetry CreateTelemetryEntry(LogEvent logEvent, IForm
{
var metricName = logEvent.Properties.GetAsRawString(ContextProperties.MetricTracking.MetricName);
var metricValue = logEvent.Properties.GetAsDouble(ContextProperties.MetricTracking.MetricValue);
var timestamp = logEvent.Properties.GetAsDateTimeOffset(ContextProperties.MetricTracking.Timestamp);

var metricTelemetry = new MetricTelemetry(metricName, metricValue)
{
Timestamp = timestamp
};

var metricTelemetry = new MetricTelemetry(metricName, metricValue);
return metricTelemetry;
}

Expand All @@ -34,6 +39,7 @@ protected override void RemoveIntermediaryProperties(LogEvent logEvent)
{
logEvent.RemovePropertyIfPresent(ContextProperties.MetricTracking.MetricName);
logEvent.RemovePropertyIfPresent(ContextProperties.MetricTracking.MetricValue);
logEvent.RemovePropertyIfPresent(ContextProperties.MetricTracking.Timestamp);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Arcus.Observability.Telemetry.Serilog.Sinks.ApplicationInsights;
using Arcus.Observability.Telemetry.Serilog.Sinks.ApplicationInsights.Converters;
using Bogus;
using Bogus.DataSets;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.AspNetCore.Http;
Expand Down Expand Up @@ -705,6 +706,7 @@ public void LogMetric_WithMetric_CreatesMetricTelemetry()
// Arrange
const string metricName = "Request stream";
const double metricValue = 0.13;
var timestamp = DateTimeOffset.UtcNow;
var spySink = new InMemoryLogSink();
string operationId = $"operation-id-{Guid.NewGuid()}";
ILogger logger = CreateLogger(spySink, config => config.Enrich.WithProperty(ContextProperties.Correlation.OperationId, operationId));
Expand All @@ -713,7 +715,7 @@ public void LogMetric_WithMetric_CreatesMetricTelemetry()
{
["Capacity"] = "0.45"
};
logger.LogMetric(metricName, metricValue, telemetryContext);
logger.LogMetric(metricName, metricValue, timestamp, telemetryContext);
LogEvent logEvent = Assert.Single(spySink.CurrentLogEmits);
Assert.NotNull(logEvent);

Expand All @@ -725,12 +727,14 @@ public void LogMetric_WithMetric_CreatesMetricTelemetry()
// Assert
AssertDoesNotContainLogProperty(logEvent, MetricTracking.MetricName);
AssertDoesNotContainLogProperty(logEvent, MetricTracking.MetricValue);
AssertDoesNotContainLogProperty(logEvent, MetricTracking.Timestamp);
AssertDoesNotContainLogProperty(logEvent, EventTracking.EventContext);
Assert.Collection(telemetries, telemetry =>
{
var metricTelemetry = Assert.IsType<MetricTelemetry>(telemetry);
Assert.Equal(metricName, metricTelemetry.Name);
Assert.Equal(metricValue, metricTelemetry.Sum);
Assert.Equal(TruncateToSeconds(timestamp), metricTelemetry.Timestamp);
AssertOperationContext(metricTelemetry, operationId);

AssertContainsTelemetryProperty(metricTelemetry, "Capacity", "0.45");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,26 @@ public void LogMetric_ValidArguments_Succeeds()
Assert.Contains(metricValue.ToString(CultureInfo.InvariantCulture), logMessage);
}

[Fact]
public void LogMetric_ValidArgumentsWithTimestamp_Succeeds()
{
// Arrange
var logger = new TestLogger();
string metricName = _bogusGenerator.Name.FullName();
double metricValue = _bogusGenerator.Random.Double();
DateTimeOffset timestamp = _bogusGenerator.Date.RecentOffset();

// Act
logger.LogMetric(metricName, metricValue, timestamp);

// Assert
var logMessage = logger.WrittenMessage;
Assert.StartsWith(MessagePrefixes.Metric, logMessage);
Assert.Contains(metricName, logMessage);
Assert.Contains(metricValue.ToString(CultureInfo.InvariantCulture), logMessage);
Assert.Contains(timestamp.ToString(CultureInfo.InvariantCulture), logMessage);
}

[Fact]
public void LogMetric_NoMetricNameWasSpecified_ThrowsException()
{
Expand Down