Skip to content
This repository was archived by the owner on Aug 3, 2024. It is now read-only.

Commit 492ddbb

Browse files
authored
Expose ability to custom timestamp metrics (#158)
* Expose ability to custom timestamp metrics * Cosmetic fixes.
1 parent 8a6eb30 commit 492ddbb

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

src/NuGet.Services.Contracts/Logging/ITelemetryClient.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ void TrackMetric(
1313
double value,
1414
IDictionary<string, string> properties = null);
1515

16+
void TrackMetric(
17+
DateTimeOffset timestamp,
18+
string metricName,
19+
double value,
20+
IDictionary<string, string> properties = null);
21+
1622
void TrackException(
1723
Exception exception,
1824
IDictionary<string, string> properties = null,

src/NuGet.Services.Logging/TelemetryClientWrapper.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using Microsoft.ApplicationInsights;
7+
using Microsoft.ApplicationInsights.DataContracts;
78

89
namespace NuGet.Services.Logging
910
{
@@ -45,5 +46,36 @@ public void TrackMetric(
4546
// logging failed, don't allow exception to escape
4647
}
4748
}
49+
50+
public void TrackMetric(
51+
DateTimeOffset timestamp,
52+
string metricName,
53+
double value,
54+
IDictionary<string, string> properties = null)
55+
{
56+
try
57+
{
58+
var metricTelemetry = new MetricTelemetry
59+
{
60+
Timestamp = timestamp,
61+
Name = metricName,
62+
Value = value
63+
};
64+
65+
if (properties != null)
66+
{
67+
foreach (var key in properties.Keys)
68+
{
69+
metricTelemetry.Properties.Add(key, properties[key]);
70+
}
71+
}
72+
73+
_telemetryClient.TrackMetric(metricTelemetry);
74+
}
75+
catch
76+
{
77+
// logging failed, don't allow exception to escape
78+
}
79+
}
4880
}
4981
}

0 commit comments

Comments
 (0)