Skip to content

Commit

Permalink
Expose metrics for libp2p using OpenTelemetry (#2074)
Browse files Browse the repository at this point in the history
libp2p _used_ to support metrics using OpenCensus, but this was recently
changed to use Prometheus instead -
libp2p/go-libp2p#1955. Unfortunately, it is
extremely difficult to get Prometheus metrics into OpenTelemetry without
running the external OTEL agent.

This re-implements the same metrics using OpenTelemetry using the _new_
Prometheus names rather than the old OpenCensus naming.

Fixes #2059
  • Loading branch information
wjam authored Feb 27, 2023
1 parent 4c0297d commit 695ef1d
Show file tree
Hide file tree
Showing 6 changed files with 531 additions and 34 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ require (
github.com/xeipuuv/gojsonschema v1.2.0
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.39.0
go.opentelemetry.io/otel v1.13.0
go.opentelemetry.io/otel/bridge/opencensus v0.36.0
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v0.35.0
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v0.35.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.13.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2331,8 +2331,6 @@ go.opentelemetry.io/otel v1.3.0/go.mod h1:PWIKzi6JCp7sM0k9yZ43VX+T345uNbAkDKwHVj
go.opentelemetry.io/otel v1.7.0/go.mod h1:5BdUoMIz5WEs0vt0CUEMtSSaTSHBBVwrhnz7+nrD5xk=
go.opentelemetry.io/otel v1.13.0 h1:1ZAKnNQKwBBxFtww/GwxNUyTf0AxkZzrukO8MeXqe4Y=
go.opentelemetry.io/otel v1.13.0/go.mod h1:FH3RtdZCzRkJYFTCsAKDy9l/XYjMdNv6QrkFFB8DvVg=
go.opentelemetry.io/otel/bridge/opencensus v0.36.0 h1:/RJRbdVBP+Yoh6tx8zu0L+b1KE/iGmo3adr5LUvCGWA=
go.opentelemetry.io/otel/bridge/opencensus v0.36.0/go.mod h1:otgxgSPzGNRPjDYJUrQwqGu4MXRngDPEbFxuVl355Fk=
go.opentelemetry.io/otel/exporters/jaeger v1.7.0 h1:wXgjiRldljksZkZrldGVe6XrG9u3kYDyQmkZwmm5dI0=
go.opentelemetry.io/otel/exporters/jaeger v1.7.0/go.mod h1:PwQAOqBgqbLQRKlj466DuD2qyMjbtcPpfPfj+AqbSBs=
go.opentelemetry.io/otel/exporters/otlp v0.20.0/go.mod h1:YIieizyaN77rtLJra0buKiNBOm9XQfkPEKBeuhoMwAM=
Expand Down
54 changes: 26 additions & 28 deletions pkg/libp2p/rcmgr/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"github.com/bacalhau-project/bacalhau/pkg/transport/bprotocol"
"github.com/libp2p/go-libp2p"
libp2p_rcmgr "github.com/libp2p/go-libp2p/p2p/host/resource-manager"
"github.com/libp2p/go-libp2p/p2p/host/resource-manager/obs"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/global"
)

func SetDefaultServiceLimits(config *libp2p_rcmgr.ScalingLimitConfig) {
func setDefaultServiceLimits(config *libp2p_rcmgr.ScalingLimitConfig) {
// Requester -> Compute nodes
// reasoning behind these limits:
// - Requester nodes should have a high number of outbound streams to compute nodes
Expand Down Expand Up @@ -41,33 +42,30 @@ func SetDefaultServiceLimits(config *libp2p_rcmgr.ScalingLimitConfig) {
)
}

var DefaultResourceManager = func(cfg *libp2p.Config) error {
// Default memory limit: 1/8th of total memory, minimum 128MB, maximum 1GB
limits := libp2p_rcmgr.DefaultLimits
libp2p.SetDefaultServiceLimits(&limits)
SetDefaultServiceLimits(&limits)
func DefaultResourceManager(cfg *libp2p.Config) error {
return resourceManagerWithMetricsProvider(global.MeterProvider())(cfg)
}

// TODO: libp2p removed open census and replaced with prometheus metrics. But we only use
// opentelemetry. Somebody needs to figure out how to map or convert the prom metrics to
// oltp metrics.
// // Hook up the trace reporter metrics. This will expose all opencensus
// // stats via the default prometheus registry. See https://opencensus.io/exporters/supported-exporters/go/prometheus/ for other options.
// err := view.Register(obs.DefaultViews...)
// if err != nil {
// log.Warn().Err(err).Msg("failed to register resource manager metrics")
// }
func resourceManagerWithMetricsProvider(provider metric.MeterProvider) func(cfg *libp2p.Config) error {
return func(cfg *libp2p.Config) error {
// Default memory limit: 1/8th of total memory, minimum 128MB, maximum 1GB
limits := libp2p_rcmgr.DefaultLimits
libp2p.SetDefaultServiceLimits(&limits)
setDefaultServiceLimits(&limits)

str, err := obs.NewStatsTraceReporter()
if err != nil {
return err
}
mgr, err := libp2p_rcmgr.NewResourceManager(
libp2p_rcmgr.NewFixedLimiter(limits.AutoScale()),
libp2p_rcmgr.WithTraceReporter(str),
)
if err != nil {
return err
}
metricReporter, err := metricsReporter(provider)
if err != nil {
return err
}

mgr, err := libp2p_rcmgr.NewResourceManager(
libp2p_rcmgr.NewFixedLimiter(limits.AutoScale()),
libp2p_rcmgr.WithTraceReporter(metricReporter),
)
if err != nil {
return err
}

return cfg.Apply(libp2p.ResourceManager(mgr))
return cfg.Apply(libp2p.ResourceManager(mgr))
}
}
Loading

0 comments on commit 695ef1d

Please sign in to comment.