Add startTime and endTime parameters to OpenTelemetry scope classes#205
Add startTime and endTime parameters to OpenTelemetry scope classes#205nikhilNava merged 2 commits intomainfrom
Conversation
Port of Python PR #181. Enables explicit span timestamps for operations recorded after completion (e.g., LangChain tracer using run.start_time/run.end_time). Changes: - OpenTelemetryScope: Add endTime constructor param to complement existing startTime - ExecuteToolScope: Add startTime/endTime params to Start() method - InferenceScope: Add startTime/endTime params to Start() method - InvokeAgentScope: Add startTime/endTime params to Start() method - OutputScope: Add startTime/endTime params to Start() method - Added unit tests for custom time functionality Co-authored-by: nikhilNava <211831449+nikhilNava@users.noreply.github.com>
Dependency Review✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.Scanned FilesNone |
There was a problem hiding this comment.
Pull request overview
Adds optional startTime/endTime parameters to the .NET OpenTelemetry scope APIs to support explicit span timestamps (useful for post-hoc span recording scenarios, e.g., tracing integrations that already have run.start_time/run.end_time).
Changes:
- Extend
OpenTelemetryScopeto accept an optionalendTime(in addition to existingstartTime) and carry it through lifecycle/end-of-scope logic. - Update scope factory
Start(...)methods (ExecuteToolScope,InferenceScope,InvokeAgentScope,OutputScope) to accept and forwardstartTime/endTime. - Add/extend unit tests to exercise custom start/end time usage paths.
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| src/Observability/Runtime/Tracing/Scopes/OpenTelemetryScope.cs | Adds endTime parameter/storage and end-of-scope timing handling. |
| src/Observability/Runtime/Tracing/Scopes/ExecuteToolScope.cs | Forwards optional startTime/endTime through Start(...). |
| src/Observability/Runtime/Tracing/Scopes/InferenceScope.cs | Forwards optional startTime/endTime through Start(...). |
| src/Observability/Runtime/Tracing/Scopes/InvokeAgentScope.cs | Forwards optional startTime/endTime through Start(...). |
| src/Observability/Runtime/Tracing/Scopes/OutputScope.cs | Forwards optional startTime/endTime through Start(...). |
| src/Tests/.../Tracing/Scopes/ExecuteToolScopeTest.cs | Adds tests for custom start/end time/override scenarios. |
| src/Tests/.../Tracing/Scopes/InferenceScopeTest.cs | Adds tests for custom start/end time/override scenarios. |
| src/Tests/.../Tracing/Scopes/InvokeAgentScopeTest.cs | Adds tests for custom start/end time/override scenarios. |
| src/Tests/.../Tracing/Scopes/OutputScopeTest.cs | Adds tests for custom start/end time/override scenarios. |
Comments suppressed due to low confidence (9)
src/Tests/Microsoft.Agents.A365.Observability.Runtime.Tests/Tracing/Scopes/InvokeAgentScopeTest.cs:350
- This test passes an explicit
endTimebut only asserts onStartTimeUtc, so it doesn't confirm that the span end timestamp/duration was set fromendTime. Add an assertion onactivity.Duration(or derived end time) to verifyendTimeis honored.
// Assert - Start time should be set to custom time
var startTime = new DateTimeOffset(activity.StartTimeUtc);
startTime.Should().BeCloseTo(customStartTime, TimeSpan.FromMilliseconds(100));
}
src/Tests/Microsoft.Agents.A365.Observability.Runtime.Tests/Tracing/Scopes/InvokeAgentScopeTest.cs:374
- This test calls
SetEndTime(laterEndTime)but only asserts the start time, so it doesn't validate that the override took effect. Add an assertion that the final duration/end time corresponds tolaterEndTime(notinitialEndTime).
// Assert - The start time should be set
var startTime = new DateTimeOffset(activity.StartTimeUtc);
startTime.Should().BeCloseTo(customStartTime, TimeSpan.FromMilliseconds(100));
}
src/Tests/Microsoft.Agents.A365.Observability.Runtime.Tests/Tracing/Scopes/InferenceScopeTest.cs:363
- This test sets
laterEndTimeviaSetEndTimebut only checks the start time. Add an assertion to verify the recorded end time/duration matcheslaterEndTime - customStartTime(and not the initially provided end time).
// Assert - The start time should be set
var startTime = new DateTimeOffset(activity.StartTimeUtc);
startTime.Should().BeCloseTo(customStartTime, TimeSpan.FromMilliseconds(100));
}
src/Tests/Microsoft.Agents.A365.Observability.Runtime.Tests/Tracing/Scopes/ExecuteToolScopeTest.cs:329
- This test calls
SetEndTime(laterEndTime)but doesn't assert anything about end time/duration. Add an assertion that the final duration/end time corresponds tolaterEndTimerather thaninitialEndTime.
// Assert - The start time should be set
var startTime = new DateTimeOffset(activity.StartTimeUtc);
startTime.Should().BeCloseTo(customStartTime, TimeSpan.FromMilliseconds(100));
}
src/Observability/Runtime/Tracing/Scopes/OpenTelemetryScope.cs:55
endTimeis accepted/stored by the constructor, but the scope currently only applies a custom end timestamp whencustomStartTimeis also set (see theEnd()logic). If a caller provides onlyendTime(or callsSetEndTimewithout setting a custom start time), the end time is effectively ignored and the span will end at wall-clock time, which conflicts with the parameter documentation. Consider either (a) honoringcustomEndTimeregardless of whethercustomStartTimeis set by using the activity's actualStartTimeUtcas the start for duration calculation, or (b) validating/throwing whenendTimeis provided withoutstartTimeand documenting that requirement.
protected OpenTelemetryScope(ActivityKind kind, AgentDetails agentDetails, TenantDetails tenantDetails, string operationName, string activityName, DateTimeOffset? startTime = null, DateTimeOffset? endTime = null, string? parentId = null, string? conversationId = null, SourceMetadata? sourceMetadata = null, CallerDetails? callerDetails = null)
{
customStartTime = startTime;
customEndTime = endTime;
activity = ActivitySource.CreateActivity(activityName, kind, default(ActivityContext));
src/Tests/Microsoft.Agents.A365.Observability.Runtime.Tests/Tracing/Scopes/OutputScopeTest.cs:142
- This test passes an explicit
endTimebut only asserts onStartTimeUtc, so it doesn't actually verify that the end timestamp (or resulting duration) was applied. Add an assertion againstactivity.Duration(or derived end time) to confirm theendTimeparameter is honored.
// Assert - Start time should be set to custom time
var startTime = new DateTimeOffset(activity.StartTimeUtc);
startTime.Should().BeCloseTo(customStartTime, TimeSpan.FromMilliseconds(100));
}
src/Tests/Microsoft.Agents.A365.Observability.Runtime.Tests/Tracing/Scopes/OutputScopeTest.cs:170
- This test is named as if it validates the end time override, but it only asserts the start time. Add an assertion that the final duration/end time reflects
laterEndTimerather thaninitialEndTime(e.g.,activity.DurationmatcheslaterEndTime - customStartTime).
// Assert - The start time should be set
var startTime = new DateTimeOffset(activity.StartTimeUtc);
startTime.Should().BeCloseTo(customStartTime, TimeSpan.FromMilliseconds(100));
}
src/Tests/Microsoft.Agents.A365.Observability.Runtime.Tests/Tracing/Scopes/InferenceScopeTest.cs:334
- This test includes an
endTimeargument but never asserts the end timestamp/duration. Add an assertion onactivity.Duration(or compute end time fromStartTimeUtc + Duration) to ensureendTimeis applied.
// Assert - Start time should be set to custom time
var startTime = new DateTimeOffset(activity.StartTimeUtc);
startTime.Should().BeCloseTo(customStartTime, TimeSpan.FromMilliseconds(100));
}
src/Tests/Microsoft.Agents.A365.Observability.Runtime.Tests/Tracing/Scopes/ExecuteToolScopeTest.cs:304
- This test passes both
startTimeandendTimebut only asserts onStartTimeUtc, so it doesn't validate the newendTimebehavior. Add an assertion thatactivity.Duration(or derived end time) matchescustomEndTime - customStartTime.
// Assert - Start time should be set to custom time
var startTime = new DateTimeOffset(activity.StartTimeUtc);
startTime.Should().BeCloseTo(customStartTime, TimeSpan.FromMilliseconds(100));
}
Port of Agent365-python#181. Enables explicit span timestamps for post-hoc span recording (e.g., LangChain tracer using
run.start_time/run.end_time).Changes
endTimeconstructor param (complements existingstartTime)startTime/endTimethroughStart()factory methodsUsage
🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.