github.com/filecoin-project/bacalhau@v0.3.23-0.20230228154132-45c989550ace/pkg/system/lifecycle_eventhandler.go (about)

     1  package system
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/filecoin-project/bacalhau/pkg/model"
     8  	"go.opentelemetry.io/otel/attribute"
     9  	"go.opentelemetry.io/otel/trace"
    10  )
    11  
    12  // A job event handler that adds lifecycle events to the job tracing span, both for events consumed and events published.
    13  type JobLifecycleEventHandler struct {
    14  	nodeID string
    15  }
    16  
    17  func NewJobLifecycleEventHandler(nodeID string) *JobLifecycleEventHandler {
    18  	return &JobLifecycleEventHandler{
    19  		nodeID: nodeID,
    20  	}
    21  }
    22  
    23  func (t *JobLifecycleEventHandler) HandleConsumedJobEvent(ctx context.Context, event model.JobEvent) error {
    24  	return t.addJobLifecycleEvent(ctx, event.JobID, fmt.Sprintf("read_%s", event.EventName))
    25  }
    26  
    27  func (t *JobLifecycleEventHandler) HandlePublishedJobEvent(ctx context.Context, event model.JobEvent) error {
    28  	return t.addJobLifecycleEvent(ctx, event.JobID, fmt.Sprintf("write_%s", event.EventName))
    29  }
    30  
    31  func (t *JobLifecycleEventHandler) addJobLifecycleEvent(ctx context.Context, jobID, eventName string) error {
    32  	span := trace.SpanFromContext(ctx)
    33  	span.AddEvent(eventName,
    34  		trace.WithAttributes(
    35  			attribute.String(model.TracerAttributeNameNodeID, t.nodeID),
    36  			attribute.String(model.TracerAttributeNameJobID, jobID),
    37  		),
    38  	)
    39  	return nil
    40  }