Skip to content

Commit

Permalink
Change tracing meta field names and remove parent span id (#393)
Browse files Browse the repository at this point in the history
* Change tracing meta field names and remove parent span id
  • Loading branch information
alexeyzimarev authored Dec 16, 2024
1 parent f2681e6 commit 9c75ba2
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/Core/src/Eventuous.Diagnostics/ActivityExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static Activity SetOrCopyParentTag(this Activity activity, string tag, st
}

public static TracingMeta GetTracingData(this Activity activity)
=> new(activity.TraceId.ToString(), activity.SpanId.ToString(), activity.ParentSpanId.ToString());
=> new(activity.TraceId.ToString(), activity.SpanId.ToString());

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Activity SetActivityStatus(this Activity activity, ActivityStatus status) {
Expand Down
5 changes: 2 additions & 3 deletions src/Core/src/Eventuous.Diagnostics/MetadataExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ static Metadata AddTracingMeta(this Metadata metadata, TracingMeta tracingMeta)
? metadata // don't override existing tracing data
: metadata
.AddNotNull(TraceId, tracingMeta.TraceId)
.AddNotNull(SpanId, tracingMeta.SpanId)
.AddNotNull(ParentSpanId, tracingMeta.ParentSpanId == EmptyId ? null : tracingMeta.ParentSpanId);
.AddNotNull(SpanId, tracingMeta.SpanId);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TracingMeta GetTracingMeta(this Metadata metadata)
=> new(metadata.GetString(TraceId), metadata.GetString(SpanId), metadata.GetString(ParentSpanId));
=> new(metadata.GetString(TraceId), metadata.GetString(SpanId));

const string EmptyId = "0000000000000000";
}
5 changes: 2 additions & 3 deletions src/Core/src/Eventuous.Diagnostics/Tags/MetaTags.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
namespace Eventuous.Diagnostics;

public static class DiagnosticTags {
public const string TraceId = "trace-id";
public const string SpanId = "span-id";
public const string ParentSpanId = "parent-span-id";
public const string TraceId = "$traceId";
public const string SpanId = "$spanId";
}
2 changes: 1 addition & 1 deletion src/Core/src/Eventuous.Diagnostics/Tags/TracingMeta.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Eventuous.Diagnostics;

public record TracingMeta(string? TraceId, string? SpanId, string? ParentSpanId) {
public record TracingMeta(string? TraceId, string? SpanId) {
bool IsValid() => TraceId != null && SpanId != null;

public ActivityContext? ToActivityContext(bool isRemote) {
Expand Down
2 changes: 1 addition & 1 deletion src/EventStore/src/Eventuous.EventStore/EsdbEventStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ CancellationToken cancellationToken
() => _client.SetStreamMetadataAsync(stream, expectedVersion.AsStreamRevision(), meta, cancellationToken: cancellationToken)
),
stream,
() => new ErrorInfo("Unable to truncate stream {Stream} at {Position}", stream, truncatePosition),
() => new("Unable to truncate stream {Stream} at {Position}", stream, truncatePosition),
(s, ex) => new TruncateStreamException(s, ex)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public async Task ShouldPropagateRemoteContext(CancellationToken cancellationTok
var writtenEvent = (await StoreFixture.EventStore.ReadEvents(Stream, StreamReadPosition.Start, 1, cancellationToken))[0];

var meta = writtenEvent.Metadata;
var (traceId, spanId, _) = meta.GetTracingMeta();
var (traceId, spanId) = meta.GetTracingMeta();

traceId.Should().NotBe(RecordedTrace.DefaultTraceId);
spanId.Should().NotBe(RecordedTrace.DefaultSpanId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public async Task AppendedEventShouldBeTraced(CancellationToken cancellationToke
var events = await _fixture.EventStore.ReadStream(streamName, StreamReadPosition.Start, cancellationToken: cancellationToken);
var first = events[0];

first.Metadata["trace-id"].Should().NotBeNull();
first.Metadata["span-id"].Should().NotBeNull();
first.Metadata["$traceId"].Should().NotBeNull();
first.Metadata["$spanId"].Should().NotBeNull();
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<IncludeTestHost>true</IncludeTestHost>
<InvariantGlobalization>true</InvariantGlobalization>
<OutputType>Exe</OutputType>
<NoWarn>CA1822</NoWarn>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MicroElements.AutoFixture.NodaTime"/>
Expand Down
23 changes: 20 additions & 3 deletions src/SqlServer/src/Eventuous.SqlServer/SchemaInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,30 @@
namespace Eventuous.SqlServer;

public class SchemaInitializer(SqlServerStoreOptions options, ILoggerFactory? loggerFactory = null) : IHostedService {
public Task StartAsync(CancellationToken cancellationToken) {
if (!options.InitializeDatabase) return Task.CompletedTask;
readonly ILogger<Schema>? _log = loggerFactory?.CreateLogger<Schema>();

public async Task StartAsync(CancellationToken cancellationToken) {
if (!options.InitializeDatabase) return;

var schema = new Schema(options.Schema);
var connectionString = Ensure.NotEmptyString(options.ConnectionString);

return schema.CreateSchema(connectionString, loggerFactory?.CreateLogger<Schema>(), cancellationToken);
Exception? ex = null;

for (var i = 0; i < 10; i++) {
try {
await schema.CreateSchema(connectionString, _log, cancellationToken);

return;
} catch (SqlException e) {
_log?.LogError("Unable to initialize the database schema: {Message}", e.Message);
ex = e;
}

await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken);
}

throw ex!;
}

public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
Expand Down

0 comments on commit 9c75ba2

Please sign in to comment.