Skip to content

Commit

Permalink
refactoring to use labeled context utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickhuie19 committed Sep 30, 2024
1 parent 3075350 commit 93c6a17
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
7 changes: 3 additions & 4 deletions core/services/workflows/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"errors"
"fmt"
"github.com/smartcontractkit/chainlink-common/pkg/beholder"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"sync"
"time"
Expand Down Expand Up @@ -270,9 +269,9 @@ func (e *Engine) init(ctx context.Context) {
if oerr != nil {
e.logger.With(cIDKey, t.ID).Errorf("failed to register trigger failure: %s", oerr)
}
labels := []attribute.KeyValue{
attribute.String("WorkflowID", "cl-node"),
attribute.String("WorkflowExecutionID", "demo-job"),
labels, oerr := getOtelAttributesFromCtx(ctx)
if oerr != nil {
e.logger.With(cIDKey, t.ID).Errorf("failed to get otel attributes from context: %s", oerr)
}
counter.Add(ctx, 1, metric.WithAttributes(labels...))
}
Expand Down
18 changes: 18 additions & 0 deletions core/services/workflows/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"github.com/smartcontractkit/chainlink-common/pkg/beholder"
"github.com/smartcontractkit/chainlink-common/pkg/beholder/pb"
"go.opentelemetry.io/otel/attribute"
"google.golang.org/protobuf/proto"
"log"
"reflect"
Expand Down Expand Up @@ -41,6 +42,13 @@ func (k *KeystoneWorkflowLabels) ToMap() map[string]string {
return labels
}

func (k *KeystoneWorkflowLabels) ToOtelAttributes() []attribute.KeyValue {
return []attribute.KeyValue{
attribute.String(WorkflowID, k.WorkflowID),
attribute.String(WorkflowExecutionID, k.WorkflowExecutionID),
}
}

// GetKeystoneLabelsFromContext extracts the KeystoneWorkflowLabels struct set on the
// unexported keystoneContextKey. Call NewKeystoneContext first before usage -
// if the key is unset or the value is not of the expected type GetKeystoneLabelsFromContext will error.
Expand Down Expand Up @@ -125,3 +133,13 @@ func composeLabeledMsg(ctx context.Context, format string, values ...interface{}

return msg, nil
}

func getOtelAttributesFromCtx(ctx context.Context) ([]attribute.KeyValue, error) {
labelsStruct, err := GetKeystoneLabelsFromContext(ctx)
if err != nil {
return nil, err
}

otelLabels := labelsStruct.ToOtelAttributes()
return otelLabels, nil
}

0 comments on commit 93c6a17

Please sign in to comment.