Skip to content

Commit

Permalink
feature/BCF-2404-grpc-tracing: refactoring SetupTelemetry API for sep…
Browse files Browse the repository at this point in the history
…aration of concerns to NewGRPCOpts and SetupTracing
  • Loading branch information
patrickhuie19 committed Oct 17, 2023
1 parent e19306c commit 9a94069
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 21 deletions.
21 changes: 18 additions & 3 deletions .github/workflows/golangci_lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,22 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.20'
go-version: '1.21'

- name: Run lint (Run `make lint` locally to reproduce)
run: make lint
- name: Install golangci-lint
run: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.54.2

# go.work makes it necessary to run linter manually
- name: Run golangci-lint
run: find . -name "go.mod" -execdir $(go env GOPATH)/bin/golangci-lint run --enable=gofmt --tests=false --exclude-use-default --timeout=5m0s --out-format checkstyle:golangci-lint-report.xml \;

- name: Check golangci-lint report for errors
run: find . -name "golangci-lint-report.xml" -exec grep "error" {} + && exit 1 || true

- name: Upload golangci-lint report
if: always()
uses: actions/upload-artifact@v3
with:
name: golangci-lint-report
path: |
./golangci-lint-report.xml
1 change: 0 additions & 1 deletion pkg/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ type Config struct {
var defaultConfig Config

// New returns a new Logger with the default configuration.
// TODO: why do API consumers have to worry about err here? Not useful?
func New() (Logger, error) { return defaultConfig.New() }

// New returns a new Logger for Config.
Expand Down
26 changes: 9 additions & 17 deletions pkg/loop/telem.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"

"github.com/smartcontractkit/chainlink-relay/pkg/logger"
"github.com/smartcontractkit/chainlink-relay/pkg/loop/internal"
)

Expand All @@ -40,15 +39,9 @@ type TracingConfig struct {
SamplingRatio float64
}

// SetupTelemetry initializes open telemetry and returns GRPCOpts with telemetry interceptors.
// NewGRPCOpts initializes open telemetry and returns GRPCOpts with telemetry interceptors.
// It is called from the host and each plugin - intended as there is bidirectional communication
func SetupTelemetry(registerer prometheus.Registerer, config TracingConfig) GRPCOpts {
if config.Enabled {
SetupTracing(config)
}

otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))

func NewGRPCOpts(registerer prometheus.Registerer) GRPCOpts {
if registerer == nil {
registerer = prometheus.DefaultRegisterer
}
Expand All @@ -57,10 +50,9 @@ func SetupTelemetry(registerer prometheus.Registerer, config TracingConfig) GRPC

// SetupTracing initializes open telemetry with the provided config.
// It sets the global trace provider and opens a connection to the configured collector.
func SetupTracing(config TracingConfig) {
log, err := logger.New()
if err != nil {
panic(err)
func SetupTracing(config TracingConfig) error {
if !config.Enabled {
return nil
}

ctx := context.Background()
Expand All @@ -75,22 +67,20 @@ func SetupTracing(config TracingConfig) {
grpc.WithBlock(),
)
if err != nil {
log.Errorf("Connecting to OTEL collector %w failed: %v", config.CollectorTarget, err)
return err
}

// Set up a trace exporter
// Shutting down the traceExporter will not shutdown the underlying connection.
traceExporter, err := otlptracegrpc.New(ctx, otlptracegrpc.WithGRPCConn(conn))
if err != nil {
log.Errorf("failed to create trace exporter: %w", err)
return
return err
}

var version string
var service string
buildInfo, ok := debug.ReadBuildInfo()
if !ok {
log.Errorf("failed to read build info: %w", err)
version = "unknown"
service = "cl-node"
} else {
Expand Down Expand Up @@ -120,6 +110,8 @@ func SetupTracing(config TracingConfig) {
)

otel.SetTracerProvider(tracerProvider)
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))
return nil
}

var grpcpromBuckets = []float64{0.001, 0.01, 0.1, 0.3, 0.6, 1, 3, 6, 9, 20, 30, 60, 90, 120}
Expand Down

0 comments on commit 9a94069

Please sign in to comment.