Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows to modify the start time and end time properties of the SegmentSpan object. #437

Merged
merged 1 commit into from
Aug 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/SkyApm.Abstractions/Tracing/ISegmentContextFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ namespace SkyApm.Tracing
{
public interface ISegmentContextFactory
{
SegmentContext CreateEntrySegment(string operationName, ICarrier carrier);
SegmentContext CreateEntrySegment(string operationName, ICarrier carrier, long startTimeMilliseconds = default);

SegmentContext CreateLocalSegment(string operationName);
SegmentContext CreateLocalSegment(string operationName, long startTimeMilliseconds = default);

SegmentContext CreateExitSegment(string operationName, StringOrIntValue networkAddress);
void Release(SegmentContext segmentContext);
SegmentContext CreateExitSegment(string operationName, StringOrIntValue networkAddress, long startTimeMilliseconds = default);

void Release(SegmentContext segmentContext, long endTimeMilliseconds = default);
}
}
8 changes: 4 additions & 4 deletions src/SkyApm.Abstractions/Tracing/ITracingContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ namespace SkyApm.Tracing
{
public interface ITracingContext
{
SegmentContext CreateEntrySegmentContext(string operationName, ICarrierHeaderCollection carrierHeader);
SegmentContext CreateEntrySegmentContext(string operationName, ICarrierHeaderCollection carrierHeader, long startTimeMilliseconds = default);

SegmentContext CreateLocalSegmentContext(string operationName);
SegmentContext CreateLocalSegmentContext(string operationName, long startTimeMilliseconds = default);

SegmentContext CreateExitSegmentContext(string operationName, string networkAddress,
ICarrierHeaderCollection carrierHeader = default(ICarrierHeaderCollection));
ICarrierHeaderCollection carrierHeader = default, long startTimeMilliseconds = default);

void Release(SegmentContext segmentContext);
void Release(SegmentContext segmentContext, long endTimeMilliseconds = default);
}
}
4 changes: 2 additions & 2 deletions src/SkyApm.Abstractions/Tracing/Segments/SegmentContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ public class SegmentContext
public SegmentReferenceCollection References { get; } = new SegmentReferenceCollection();

public SegmentContext(string traceId, string segmentId, bool sampled, string serviceId, string serviceInstanceId,
string operationName, SpanType spanType)
string operationName, SpanType spanType, long startTimeMilliseconds = default)
{
TraceId = traceId;
Sampled = sampled;
SegmentId = segmentId;
ServiceId = serviceId;
ServiceInstanceId = serviceInstanceId;
Span = new SegmentSpan(operationName, spanType);
Span = new SegmentSpan(operationName, spanType, startTimeMilliseconds);
}
}
}
9 changes: 5 additions & 4 deletions src/SkyApm.Abstractions/Tracing/Segments/SegmentSpan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class SegmentSpan

public int ParentSpanId { get; } = -1;

public long StartTime { get; } = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
public long StartTime { get; }

public long EndTime { get; private set; }

Expand All @@ -49,10 +49,11 @@ public class SegmentSpan

public LogCollection Logs { get; } = new LogCollection();

public SegmentSpan(string operationName, SpanType spanType)
public SegmentSpan(string operationName, SpanType spanType, long startTimeMilliseconds = default)
{
OperationName = new StringOrIntValue(operationName);
SpanType = spanType;
StartTime = startTimeMilliseconds == default ? DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() : startTimeMilliseconds;
}

public SegmentSpan AddTag(string key, string value)
Expand All @@ -79,9 +80,9 @@ public void AddLog(params LogEvent[] events)
Logs.AddLog(log);
}

public void Finish()
public void Finish(long endTimeMilliseconds = default)
{
EndTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
EndTime = endTimeMilliseconds == default ? DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() : endTimeMilliseconds;
}
}

Expand Down
18 changes: 9 additions & 9 deletions src/SkyApm.Core/Tracing/SegmentContextFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ public SegmentContextFactory(IRuntimeEnvironment runtimeEnvironment,
_instrumentConfig = configAccessor.Get<InstrumentConfig>();
}

public SegmentContext CreateEntrySegment(string operationName, ICarrier carrier)
public SegmentContext CreateEntrySegment(string operationName, ICarrier carrier, long startTimeMilliseconds = default)
{
var traceId = GetTraceId(carrier);
var segmentId = GetSegmentId();
var sampled = GetSampled(carrier, operationName);
var segmentContext = new SegmentContext(traceId, segmentId, sampled,
_instrumentConfig.ServiceName ?? _instrumentConfig.ApplicationCode,
_instrumentConfig.ServiceInstanceName,
operationName, SpanType.Entry);
operationName, SpanType.Entry, startTimeMilliseconds);

if (carrier.HasValue)
{
Expand All @@ -83,7 +83,7 @@ public SegmentContext CreateEntrySegment(string operationName, ICarrier carrier)
return segmentContext;
}

public SegmentContext CreateLocalSegment(string operationName)
public SegmentContext CreateLocalSegment(string operationName, long startTimeMilliseconds = default)
{
var parentSegmentContext = GetParentSegmentContext(SpanType.Local);
var traceId = GetTraceId(parentSegmentContext);
Expand All @@ -92,7 +92,7 @@ public SegmentContext CreateLocalSegment(string operationName)
var segmentContext = new SegmentContext(traceId, segmentId, sampled,
_instrumentConfig.ServiceName ?? _instrumentConfig.ApplicationCode,
_instrumentConfig.ServiceInstanceName,
operationName, SpanType.Local);
operationName, SpanType.Local, startTimeMilliseconds);

if (parentSegmentContext != null)
{
Expand All @@ -118,7 +118,7 @@ public SegmentContext CreateLocalSegment(string operationName)
return segmentContext;
}

public SegmentContext CreateExitSegment(string operationName, StringOrIntValue networkAddress)
public SegmentContext CreateExitSegment(string operationName, StringOrIntValue networkAddress, long startTimeMilliseconds = default)
{
var parentSegmentContext = GetParentSegmentContext(SpanType.Exit);
var traceId = GetTraceId(parentSegmentContext);
Expand All @@ -127,7 +127,7 @@ public SegmentContext CreateExitSegment(string operationName, StringOrIntValue n
var segmentContext = new SegmentContext(traceId, segmentId, sampled,
_instrumentConfig.ServiceName ?? _instrumentConfig.ApplicationCode,
_instrumentConfig.ServiceInstanceName,
operationName, SpanType.Exit);
operationName, SpanType.Exit, startTimeMilliseconds);

if (parentSegmentContext != null)
{
Expand All @@ -154,13 +154,13 @@ public SegmentContext CreateExitSegment(string operationName, StringOrIntValue n
return segmentContext;
}

public void Release(SegmentContext segmentContext)
public void Release(SegmentContext segmentContext, long endTimeMilliseconds = default)
{
segmentContext.Span.Finish();
segmentContext.Span.Finish(endTimeMilliseconds);
switch (segmentContext.Span.SpanType)
{
case SpanType.Entry:
_entrySegmentContextAccessor.Context = null;
_entrySegmentContextAccessor.Context = null;
break;
case SpanType.Local:
_localSegmentContextAccessor.Context = null;
Expand Down
18 changes: 9 additions & 9 deletions src/SkyApm.Core/Tracing/TracingContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,37 +37,37 @@ public TracingContext(ISegmentContextFactory segmentContextFactory, ICarrierProp
_segmentDispatcher = segmentDispatcher;
}

public SegmentContext CreateEntrySegmentContext(string operationName, ICarrierHeaderCollection carrierHeader)
public SegmentContext CreateEntrySegmentContext(string operationName, ICarrierHeaderCollection carrierHeader, long startTimeMilliseconds = default)
{
if (operationName == null) throw new ArgumentNullException(nameof(operationName));
var carrier = _carrierPropagator.Extract(carrierHeader);
return _segmentContextFactory.CreateEntrySegment(operationName, carrier);
return _segmentContextFactory.CreateEntrySegment(operationName, carrier, startTimeMilliseconds);
}

public SegmentContext CreateLocalSegmentContext(string operationName)
public SegmentContext CreateLocalSegmentContext(string operationName, long startTimeMilliseconds = default)
{
if (operationName == null) throw new ArgumentNullException(nameof(operationName));
return _segmentContextFactory.CreateLocalSegment(operationName);
return _segmentContextFactory.CreateLocalSegment(operationName, startTimeMilliseconds);
}

public SegmentContext CreateExitSegmentContext(string operationName, string networkAddress,
ICarrierHeaderCollection carrierHeader = default(ICarrierHeaderCollection))
ICarrierHeaderCollection carrierHeader = default, long startTimeMilliseconds = default)
{
var segmentContext =
_segmentContextFactory.CreateExitSegment(operationName, new StringOrIntValue(networkAddress));
_segmentContextFactory.CreateExitSegment(operationName, new StringOrIntValue(networkAddress), startTimeMilliseconds);
if (carrierHeader != null)
_carrierPropagator.Inject(segmentContext, carrierHeader);
return segmentContext;
}

public void Release(SegmentContext segmentContext)
public void Release(SegmentContext segmentContext, long endTimeMilliseconds = default)
{
if (segmentContext == null)
{
return;
}
_segmentContextFactory.Release(segmentContext);

_segmentContextFactory.Release(segmentContext, endTimeMilliseconds);
if (segmentContext.Sampled)
_segmentDispatcher.Dispatch(segmentContext);
}
Expand Down