Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added `client.WithUnversionedActivityNames` and `task.WithUnversionedActivityNames`, the activity counterparts of the existing unversioned-orchestrator allow-lists. An activity inherits its caller's version, so a system component whose orchestration runs unversioned schedules unversioned activities that a strict-version worker would otherwise refuse to accept and would advertise under its own version.
- Added `task.IsCompleted`, a non-blocking check that reports whether an orchestration task has succeeded, failed, or been canceled, without consuming its result or advancing history. It observes completion at the current replay position: after `WhenAny` or `Select` wakes on a completion, only the selected task is guaranteed complete. It panics for nil or custom `Task` implementations, matching `OnTask`, and outside an orchestration coroutine, matching `Select`. The `Task` interface is unchanged.
- Added opt-in, replay-aware OpenTelemetry spans for orchestrations, activities, sub-orchestrations, and timers via `client.WithWorkerTracerProvider`, using the `Microsoft.DurableTask` instrumentation scope. The provider must use `tracing.WithIDGenerator` and `tracing.WithSampler`; otherwise the option fails with `tracing.ErrUnsupportedTracerProvider`. Span export is best effort, not exactly once. Default behavior is unchanged: the worker only propagates W3C trace context. Scheduling an orchestration or signaling an entity now propagates an unsampled caller context, with its tracestate, instead of dropping it, so an unsampled caller no longer starts a new, independently sampled trace. The `durabletasktest` in-memory service persists the orchestration span identity between dispatches, as DTS does.
- Added opt-in client PRODUCER spans via `client.WithTracerProvider`, which accepts any provider: `create_orchestration:<name>[@(<version>)]` for `ScheduleNewOrchestration`, parenting the new orchestration, and `orchestration_event:<name>` for `RaiseEvent`. `durabletaskscheduler.NewClient` accepts trailing `client.TaskHubGrpcClientOption` values.

### Removed

Expand Down
61 changes: 47 additions & 14 deletions client/client_grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"google.golang.org/protobuf/types/known/wrapperspb"

"github.com/microsoft/durabletask-go/api"
"github.com/microsoft/durabletask-go/internal/dttrace"
"github.com/microsoft/durabletask-go/internal/failure"
"github.com/microsoft/durabletask-go/internal/helpers"
"github.com/microsoft/durabletask-go/internal/largepayload"
Expand All @@ -30,6 +31,7 @@ type TaskHubGrpcClient struct {
largePayloads *api.LargePayloadOptions
defaultVersion string
converter api.DataConverter
tracer *dttrace.ClientTracer

listenerMu sync.Mutex
listener *TaskHubGrpcWorker
Expand Down Expand Up @@ -72,6 +74,25 @@ func WithDataConverter(converter api.DataConverter) TaskHubGrpcClientOption {
}
}

// WithTracerProvider enables Durable Task PRODUCER spans for
// ScheduleNewOrchestration ("create_orchestration:<name>") and RaiseEvent
// ("orchestration_event:<name>"). Spans are children of the span in the call's
// context, and a scheduled orchestration is parented to its
// create_orchestration span. Failed calls mark the span as an error.
//
// Any provider is accepted, including otel.GetTracerProvider();
// tracing.WithIDGenerator and tracing.WithSampler are
// required only for worker tracing, and a provider configured with them can be
// shared with the worker. The application owns the provider's configuration and
// shutdown. Without this option, or with a nil provider, the client emits no
// spans and only propagates the caller's valid trace context, sampled or
// unsampled, including its tracestate.
func WithTracerProvider(provider trace.TracerProvider) TaskHubGrpcClientOption {
return func(c *TaskHubGrpcClient) {
c.tracer = dttrace.NewClientTracer(provider)
}
}

// NewTaskHubGrpcClient creates a client that can be used to manage orchestrations over a borrowed gRPC connection.
// The caller retains ownership of the connection; this client neither closes nor recreates it. DTS applications that
// want an owned, resilient management channel should use durabletaskscheduler.NewClient.
Expand All @@ -89,37 +110,45 @@ func NewTaskHubGrpcClient(cc grpc.ClientConnInterface, logger api.Logger, opts .
}

// ScheduleNewOrchestration schedules a new orchestration instance with a specified set of options for execution.
func (c *TaskHubGrpcClient) ScheduleNewOrchestration(ctx context.Context, orchestrator string, opts ...api.NewOrchestrationOptions) (api.InstanceID, error) {
func (c *TaskHubGrpcClient) ScheduleNewOrchestration(ctx context.Context, orchestrator string, opts ...api.NewOrchestrationOptions) (_ api.InstanceID, err error) {
req := &protos.CreateInstanceRequest{Name: orchestrator}
var configErr error
for _, configure := range opts {
if err := configure(req, c.converter); err != nil {
return api.EmptyInstanceID, fmt.Errorf(
"failed to configure orchestration request: %w",
api.WrapInvalidArgument(err),
)
if configErr = configure(req, c.converter); configErr != nil {
break
}
}
if req.Version == nil && c.defaultVersion != "" {
req.Version = wrapperspb.String(c.defaultVersion)
}
if req.InstanceId == "" {
// Do not generate an ID for a request rejected by an option.
if req.InstanceId == "" && configErr == nil {
u, err := uuid.NewV7()
if err == nil {
req.InstanceId = u.String()
} else {
req.InstanceId = uuid.NewString()
}
}
var err error
var span trace.Span
if c.tracer != nil {
ctx, span = c.tracer.StartCreateOrchestration(ctx, req.Name, req.Version.GetValue(),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This span starts after WithInput has already serialized the payload, unlike RaiseEvent. A slow converter can dominate the call without showing up in the scheduling span duration. Capture the start timestamp before applying options when tracing is enabled, then supply it once the final span name and attributes are known.

req.InstanceId, req.ExecutionId.GetValue())
defer func() { dttrace.EndClientSpan(span, err) }()
Comment thread
andystaples marked this conversation as resolved.
Resolved
}
if configErr != nil {
return api.EmptyInstanceID, fmt.Errorf(
"failed to configure orchestration request: %w",
api.WrapInvalidArgument(configErr),
)
}
req.Input, err = largepayload.Externalize(ctx, c.largePayloads, req.Input)
if err != nil {
return api.EmptyInstanceID, fmt.Errorf("failed to externalize orchestration input: %w", err)
}

// Propagate the caller's distributed trace context so the service can parent the
// orchestration's trace to the code that scheduled it. Without this, every
// orchestration starts a brand new, disconnected trace. Unsampled callers
// propagate too, so their sampling decision is kept downstream.
// Propagate the PRODUCER span when tracing is enabled, or the caller's span
// otherwise. Unsampled contexts preserve the sampling decision downstream.
if req.ParentTraceContext == nil {
req.ParentTraceContext = helpers.TraceContextFromSpanContext(trace.SpanContextFromContext(ctx))
}
Expand Down Expand Up @@ -222,14 +251,18 @@ func (c *TaskHubGrpcClient) TerminateOrchestration(ctx context.Context, id api.I
}

// RaiseEvent sends an asynchronous event notification to a waiting orchestration.
func (c *TaskHubGrpcClient) RaiseEvent(ctx context.Context, id api.InstanceID, eventName string, opts ...api.RaiseEventOptions) error {
func (c *TaskHubGrpcClient) RaiseEvent(ctx context.Context, id api.InstanceID, eventName string, opts ...api.RaiseEventOptions) (err error) {
if c.tracer != nil {
var span trace.Span
ctx, span = c.tracer.StartRaiseEvent(ctx, eventName, string(id))
defer func() { dttrace.EndClientSpan(span, err) }()
}
req := &protos.RaiseEventRequest{InstanceId: string(id), Name: eventName}
for _, configure := range opts {
if err := configure(req, c.converter); err != nil {
return fmt.Errorf("failed to configure raise event request: %w", api.WrapInvalidArgument(err))
}
}
var err error
req.Input, err = largepayload.Externalize(ctx, c.largePayloads, req.Input)
if err != nil {
return fmt.Errorf("failed to externalize event payload: %w", err)
Expand Down
Loading