-
-
Notifications
You must be signed in to change notification settings - Fork 69
/
ProducerActivity.cs
65 lines (52 loc) · 2.37 KB
/
ProducerActivity.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright (C) Ubiquitous AS. All rights reserved
// Licensed under the Apache License, Version 2.0.
using System.Diagnostics;
using Eventuous.Diagnostics;
namespace Eventuous.Producers.Diagnostics;
public static class ProducerActivity {
public static (Activity? act, ProducedMessage msgs) Start(ProducedMessage message, IEnumerable<KeyValuePair<string, object?>>? tags) {
var activity = GetActivity(tags);
var meta = GetMeta(message.Metadata);
var messageId = message.MessageId.ToString();
activity?
.SetTag(TelemetryTags.Message.Type, message.MessageType)
.SetTag(TelemetryTags.Message.Id, messageId)
.SetTag(TelemetryTags.Messaging.MessageId, messageId)
.CopyParentTag(TelemetryTags.Messaging.ConversationId)
.SetOrCopyParentTag(
TelemetryTags.Messaging.CausationId,
meta.GetCausationId(),
TelemetryTags.Message.Id
)
.SetOrCopyParentTag(TelemetryTags.Messaging.CorrelationId, meta.GetCorrelationId())
.Start();
meta.AddActivityTags(activity);
return (activity, message with { Metadata = meta });
}
public static (Activity? act, IEnumerable<ProducedMessage> msgs) Start(
IEnumerable<ProducedMessage> messages,
IEnumerable<KeyValuePair<string, object?>>? tags
) {
var activity = GetActivity(tags);
activity?
.CopyParentTag(TelemetryTags.Messaging.ConversationId)
.CopyParentTag(TelemetryTags.Messaging.CausationId, TelemetryTags.Message.Id)
.CopyParentTag(TelemetryTags.Messaging.CorrelationId)
.Start();
return (activity, messages.Select(GetMessage));
ProducedMessage GetMessage(ProducedMessage message)
=> message with { Metadata = GetMeta(message.Metadata).AddActivityTags(activity) };
}
static Activity? GetActivity(IEnumerable<KeyValuePair<string, object?>>? tags)
=> EventuousDiagnostics.ActivitySource.CreateActivity(
"produce",
ActivityKind.Producer,
parentContext: default,
tags,
idFormat: ActivityIdFormat.W3C
);
static Metadata GetMeta(Metadata? metadata)
=> Metadata
.FromMeta(metadata)
.WithCorrelationId(metadata?.GetCorrelationId());
}