mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-08 23:18:15 -05:00
<!-- Thanks for sending a PR! Before submitting: 1. If this is your first PR, check out our contribution guide here https://docs.prylabs.network/docs/contribute/contribution-guidelines You will then need to sign our Contributor License Agreement (CLA), which will show up as a comment from a bot in this pull request after you open it. We cannot review code without a signed CLA. 2. Please file an associated tracking issue if this pull request is non-trivial and requires context for our team to understand. All features and most bug fixes should have an associated issue with a design discussed and decided upon. Small bug fixes and documentation improvements don't need issues. 3. New features and bug fixes must have tests. Documentation may need to be updated. If you're unsure what to update, send the PR, and we'll discuss in review. 4. Note that PRs updating dependencies and new Go versions are not accepted. Please file an issue instead. 5. A changelog entry is required for user facing issues. --> **What type of PR is this?** > Bug fix **What does this PR do? Why is it needed?** It's just a simple fix. I was looking at how prysm uses OpenTelemetry and I noticed it. **Which issues(s) does this PR fix?** **Other notes for review** **Acknowledgements** - [x] I have read [CONTRIBUTING.md](https://github.com/prysmaticlabs/prysm/blob/develop/CONTRIBUTING.md). - [x] I have included a uniquely named [changelog fragment file](https://github.com/prysmaticlabs/prysm/blob/develop/CONTRIBUTING.md#maintaining-changelogmd). - [x] I have added a description with sufficient context for reviewers to understand this PR. - [x] I have tested that my changes work as expected and I added a testing plan to the PR description (if applicable).
64 lines
1.9 KiB
Go
64 lines
1.9 KiB
Go
// Package tracing sets up jaeger as an opentracing tool
|
|
// for services in Prysm.
|
|
package tracing
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"time"
|
|
|
|
prysmTrace "github.com/OffchainLabs/prysm/v7/monitoring/tracing/trace"
|
|
"github.com/OffchainLabs/prysm/v7/runtime/version"
|
|
"github.com/sirupsen/logrus"
|
|
"go.opentelemetry.io/otel"
|
|
"go.opentelemetry.io/otel/attribute"
|
|
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
|
|
"go.opentelemetry.io/otel/propagation"
|
|
"go.opentelemetry.io/otel/sdk/resource"
|
|
"go.opentelemetry.io/otel/sdk/trace"
|
|
semconv "go.opentelemetry.io/otel/semconv/v1.17.0"
|
|
"go.opentelemetry.io/otel/trace/noop"
|
|
)
|
|
|
|
var log = logrus.WithField("prefix", "tracing")
|
|
|
|
// Setup creates and initializes a new Jaegar tracing configuration with opentelemetry.
|
|
func Setup(ctx context.Context, serviceName, processName, endpoint string, sampleFraction float64, enable bool) error {
|
|
if !enable {
|
|
otel.SetTracerProvider(noop.NewTracerProvider())
|
|
return nil
|
|
}
|
|
prysmTrace.TracingEnabled = true
|
|
|
|
if serviceName == "" {
|
|
return errors.New("tracing service name cannot be empty")
|
|
}
|
|
|
|
log.Infof("Starting otel exporter endpoint at address = %s", endpoint)
|
|
exporter, err := otlptracehttp.New(ctx, otlptracehttp.WithEndpointURL(endpoint))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
tp := trace.NewTracerProvider(
|
|
trace.WithSampler(trace.TraceIDRatioBased(sampleFraction)),
|
|
trace.WithBatcher(
|
|
exporter,
|
|
trace.WithMaxExportBatchSize(trace.DefaultMaxExportBatchSize),
|
|
trace.WithBatchTimeout(trace.DefaultScheduleDelay*time.Millisecond),
|
|
),
|
|
trace.WithResource(
|
|
resource.NewWithAttributes(
|
|
semconv.SchemaURL,
|
|
semconv.ServiceNameKey.String(serviceName),
|
|
attribute.String("process_name", processName),
|
|
attribute.String("build", version.BuildData()),
|
|
),
|
|
),
|
|
)
|
|
|
|
otel.SetTracerProvider(tp)
|
|
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))
|
|
|
|
return nil
|
|
}
|