Skip to content

Commit 2c64f43

Browse files
authored
Merge pull request #446 from KodrAus/fix/metrics
Fix up naming of order metrics
2 parents 31e34a4 + 22df463 commit 2c64f43

6 files changed

Lines changed: 56 additions & 26 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
namespace Roastery.Metrics;
22

3-
public record struct PropertyNameMapping(string MetricDefinitions, string MetricSamples);
3+
public record struct PropertyNameMapping(string MetricDefinitions);

src/Roastery/Metrics/RoasteryMetrics.cs

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ public class Sample
2727
public record struct HttpRequestDurationKey(string Path, int StatusCode);
2828
public readonly Dictionary<HttpRequestDurationKey, ExponentialHistogram> HttpRequestDuration = new();
2929

30-
// `OrdersCreated`: counter
31-
public ulong OrdersCreated;
30+
// `OrderCreated`: counter
31+
public ulong OrderCreated;
3232

33-
// `OrdersShipped`: counter
34-
public ulong OrdersShipped;
33+
// `OrderShipped`: counter
34+
public ulong OrderShipped;
3535

3636
static readonly MessageTemplate Template = new MessageTemplateParser().Parse("Metrics sampled");
3737

@@ -45,20 +45,25 @@ public IEnumerable<LogEvent> ToLogEvents(ILogger logger, PropertyNameMapping pro
4545
timestamp,
4646
new Dictionary<string, object>
4747
{
48-
{ "HttpRequestDuration", new { kind = "Exponential", unit = "ms", description = "The time taken to fully process a request" } }
48+
[nameof(HttpRequestDuration)] = new
49+
{
50+
kind = "Exponential",
51+
unit = "ms",
52+
description = "The time taken to fully process a request."
53+
}
4954
},
50-
new
55+
new Dictionary<string, object>
5156
{
52-
HttpRequestDuration = new {
57+
[nameof(HttpRequestDuration)] = new {
5358
buckets = metric.Buckets
5459
.Select(bucket => new { midpoint = bucket.Key, count = bucket.Value }).ToArray(),
5560
scale = metric.Scale,
5661
min = metric.Min,
5762
max = metric.Max,
5863
count = metric.Total
5964
},
60-
key.Path,
61-
key.StatusCode
65+
[nameof(key.Path)] = key.Path,
66+
[nameof(key.StatusCode)] = key.StatusCode
6267
}
6368
);
6469
}
@@ -69,24 +74,46 @@ public IEnumerable<LogEvent> ToLogEvents(ILogger logger, PropertyNameMapping pro
6974
timestamp,
7075
new Dictionary<string, object>
7176
{
72-
{ "OrderCreated", new { kind = "Sum", unit = "orders", description = "An order was created" } },
73-
{ "OrderShipped", new { kind = "Sum", unit = "orders", description = "An order was shipped" } }
77+
[nameof(OrderCreated)] = new
78+
{
79+
kind = "Sum",
80+
unit = "order",
81+
description = "An order was created."
82+
},
83+
[nameof(OrderShipped)] = new
84+
{
85+
kind = "Sum",
86+
unit = "order",
87+
description = "An order was shipped."
88+
}
7489
},
75-
new
90+
new Dictionary<string, object>
7691
{
77-
OrdersCreated,
78-
OrdersShipped
92+
[nameof(OrderCreated)] = OrderCreated,
93+
[nameof(OrderShipped)] = OrderShipped
7994
}
8095
);
8196
}
8297

83-
static LogEvent ToLogEvent(ILogger logger, PropertyNameMapping propertyNameMapping, DateTimeOffset timestamp, Dictionary<string, object> definitions, object samples)
98+
static LogEvent ToLogEvent(ILogger logger, PropertyNameMapping propertyNameMapping, DateTimeOffset timestamp, Dictionary<string, object> definitions, Dictionary<string, object> samples)
8499
{
85-
logger.BindProperty(propertyNameMapping.MetricDefinitions, definitions, true, out var definitionsProperty);
86-
logger.BindProperty(propertyNameMapping.MetricSamples, samples, true, out var sampleProperty);
100+
var properties = new List<LogEventProperty>();
101+
102+
if (logger.BindProperty(propertyNameMapping.MetricDefinitions, definitions, true,
103+
out var definitionsProperty))
104+
{
105+
properties.Add(definitionsProperty);
106+
}
107+
108+
foreach (var (key, value) in samples)
109+
{
110+
if (logger.BindProperty(key, value, true, out var sample))
111+
{
112+
properties.Add(sample);
113+
}
114+
}
87115

88-
return new LogEvent(timestamp, LogEventLevel.Information, null, Template,
89-
[definitionsProperty!, sampleProperty!]);
116+
return new LogEvent(timestamp, LogEventLevel.Information, null, Template, properties);
90117
}
91118
}
92119

@@ -113,15 +140,15 @@ public void RecordOrderCreated()
113140
{
114141
lock (_lock)
115142
{
116-
_current.OrdersCreated += 1;
143+
_current.OrderCreated += 1;
117144
}
118145
}
119146

120147
public void RecordOrderShipped()
121148
{
122149
lock (_lock)
123150
{
124-
_current.OrdersShipped += 1;
151+
_current.OrderShipped += 1;
125152
}
126153
}
127154

src/SeqCli/Mapping/MetricsMapping.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,4 @@ namespace SeqCli.Mapping;
55
public static class MetricsMapping
66
{
77
internal static readonly string SurrogateDefinitionsProperty = $"_SeqcliMetricDefinitions_{Guid.NewGuid():N}";
8-
internal static readonly string SurrogateSamplesProperty = $"_SeqcliMetricSamples_{Guid.NewGuid():N}";
98
}

src/SeqCli/Output/OutputFormatter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ static class OutputFormatter
1515
$"{{ " +
1616
$"if {MetricsMapping.SurrogateDefinitionsProperty} is not null then " +
1717
// Emit a metric sample
18-
$"{{@t, @l: undefined(), @d: {MetricsMapping.SurrogateDefinitionsProperty}, ..{MetricsMapping.SurrogateSamplesProperty}, ..rest()}} " +
18+
$"{{@t, @l: undefined(), @d: {MetricsMapping.SurrogateDefinitionsProperty}, ..rest()}} " +
1919
$"else " +
2020
// Emit a log or span
2121
$"{{@t, @mt, @l: coalesce({LevelMapping.SurrogateLevelProperty}, if @l = 'Information' then undefined() else @l), @x, @sp, @tr, @ps: coalesce({TraceConstants.ParentSpanIdProperty}, @ps), @st: coalesce({TraceConstants.SpanStartTimestampProperty}, @st), ..rest()}} " +
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
{
22
"$schema": "http://json.schemastore.org/launchsettings.json",
33
"profiles": {
4-
"SeqCli": {
4+
"SeqCli Forwarder": {
55
"commandName": "Project",
66
"commandLineArgs": "forwarder run --pre"
7+
},
8+
"SeqCli Sample": {
9+
"commandName": "Project",
10+
"commandLineArgs": "sample ingest -y"
711
}
812
}
913
}

src/SeqCli/Sample/Loader/Simulation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public static async Task RunAsync(SeqConnection connection, string? apiKey, int
3939
var ship = Task.Run(() => LogShipper.ShipEventsAsync(connection, apiKey, buffer,
4040
InvalidDataHandling.Fail, SendFailureHandling.Continue, batchSize, null, cancellationToken), cancellationToken);
4141

42-
await Roastery.Program.Main(logger, new PropertyNameMapping(MetricsMapping.SurrogateDefinitionsProperty, MetricsMapping.SurrogateSamplesProperty), cancellationToken);
42+
await Roastery.Program.Main(logger, new PropertyNameMapping(MetricsMapping.SurrogateDefinitionsProperty), cancellationToken);
4343
await logger.DisposeAsync();
4444
await ship;
4545
}

0 commit comments

Comments
 (0)