-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from dbones-labs/1
OpenTelemetry support + fix for headers
- Loading branch information
Showing
14 changed files
with
255 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
namespace Eventual.Tracing | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Threading.Tasks; | ||
using Fox.Middleware; | ||
using Middleware; | ||
using Middleware.Subscribing; | ||
|
||
public class OpenTelemetryConsumeAction<T> : IConsumeAction<T> | ||
{ | ||
private readonly Telemetry _telemetry; | ||
private readonly TelemetryContext _context; | ||
|
||
public OpenTelemetryConsumeAction(Telemetry telemetry, TelemetryContext context) | ||
{ | ||
_telemetry = telemetry; | ||
_context = context; | ||
} | ||
|
||
public async Task Execute(MessageReceivedContext<T> context, Next<MessageReceivedContext<T>> next) | ||
{ | ||
var traceId = context.Message.OpenTelemetryTraceId; | ||
var activity = traceId != null | ||
? _telemetry.ActivitySource.StartActivity(typeof(T).FullName, ActivityKind.Consumer, traceId) | ||
: _telemetry.ActivitySource.StartActivity(typeof(T).FullName, ActivityKind.Consumer); | ||
|
||
_context.CorrelationId = context.Message.CorrelationId; | ||
|
||
if (activity == null) | ||
{ | ||
await next(context); | ||
return; | ||
} | ||
|
||
//activity.SetIdFormat(ActivityIdFormat.W3C); | ||
activity.AddTag("adapter", "eventual"); | ||
activity.AddTag("message.id", context.Message.Id); | ||
activity.AddTag("message.correlation.id", context.Message.CorrelationId); | ||
|
||
Activity.Current = activity; | ||
_context.OpenTelemetryTraceId = activity?.Id; | ||
|
||
using (activity) | ||
{ | ||
//activity.Start(); | ||
try | ||
{ | ||
await next(context); | ||
activity.SetStatus(ActivityStatusCode.Ok); | ||
} | ||
catch (Exception e) | ||
{ | ||
activity.SetStatus(ActivityStatusCode.Error, e.Message); | ||
activity.SetTag("otel.status_code", "ERROR"); | ||
activity.SetTag("otel.status_description", e.Message); | ||
|
||
var tags = new List<KeyValuePair<string, object>> | ||
{ | ||
new("message", e.Message), | ||
new("stack", e.StackTrace) | ||
}; | ||
|
||
ActivityEvent @event = new ActivityEvent("error", tags: new ActivityTagsCollection(tags)); | ||
activity.AddEvent(@event); | ||
throw; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
namespace Eventual.Tracing | ||
{ | ||
using System.Diagnostics; | ||
using System.Threading.Tasks; | ||
using Fox.Middleware; | ||
using Middleware; | ||
using Middleware.Publishing; | ||
|
||
public class OpenTelemetryPublishAction<T> : IPublishAction<T> | ||
{ | ||
private readonly Telemetry _telemetry; | ||
private readonly TelemetryContext _context; | ||
|
||
public OpenTelemetryPublishAction(Telemetry telemetry, TelemetryContext context) | ||
{ | ||
_telemetry = telemetry; | ||
_context = context; | ||
} | ||
|
||
public async Task Execute(MessagePublishContext<T> context, Next<MessagePublishContext<T>> next) | ||
{ | ||
string parentId; | ||
var parent = Activity.Current; | ||
|
||
if (parent != null && !string.IsNullOrEmpty(parent.Id) && parent.IdFormat == ActivityIdFormat.W3C) | ||
{ | ||
parentId = parent.Id; | ||
} | ||
else | ||
{ | ||
parentId = _context?.OpenTelemetryTraceId; | ||
} | ||
|
||
var activity = parentId != null | ||
? _telemetry.ActivitySource.StartActivity(typeof(T).FullName, ActivityKind.Producer, parentId) | ||
: _telemetry.ActivitySource.StartActivity(typeof(T).FullName, ActivityKind.Producer); | ||
|
||
//user can define -> we try the parent message -> finally we re-use the current id | ||
context.Message.CorrelationId ??= _context?.CorrelationId ?? context.Message.Id; | ||
|
||
if (activity == null) | ||
{ | ||
await next(context); | ||
return; | ||
} | ||
|
||
//activity.SetIdFormat(ActivityIdFormat.W3C); | ||
activity.AddTag("adapter", "eventual"); | ||
activity.AddTag("message.id", context.Message.Id); | ||
activity.AddTag("message.correlation.id", context.Message.CorrelationId); | ||
|
||
using (activity) | ||
{ | ||
//activity.Start(); | ||
context.Message.OpenTelemetryTraceId = activity.Id; | ||
//if (!context.Message.Metadata.ContainsKey(Telemetry.Header)) context.Message.Metadata.Add(Telemetry.Header, activity.Id); | ||
await next(context); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.