Skip to content

Commit

Permalink
instrumentation: remove opencensus metrics exporter.
Browse files Browse the repository at this point in the history
Remove the old opencensus-based prometheus exporter. Rework
prometheus exporting using our update metrics registry and
a promhttp /metrics-handler.

Signed-off-by: Krisztian Litkey <[email protected]>
  • Loading branch information
klihub committed Nov 11, 2024
1 parent 71a1b62 commit bbdf2e8
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 277 deletions.
8 changes: 1 addition & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/containers/nri-plugins
go 1.22

require (
contrib.go.opencensus.io/exporter/prometheus v0.4.2
github.com/containerd/nri v0.6.0
github.com/containerd/otelttrpc v0.0.0-20240305015340-ea5083fda723
github.com/containerd/ttrpc v1.2.3-0.20231030150553-baadfd8e7956
Expand All @@ -17,10 +16,8 @@ require (
github.com/pelletier/go-toml/v2 v2.1.0
github.com/prometheus/client_golang v1.17.0
github.com/prometheus/client_model v0.5.0
github.com/prometheus/common v0.44.0
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.8.4
go.opencensus.io v0.24.0
go.opentelemetry.io/otel v1.19.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.19.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.19.0
Expand All @@ -44,8 +41,6 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/evanphx/json-patch v5.6.0+incompatible // indirect
github.com/go-kit/log v0.2.1 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
Expand All @@ -54,7 +49,6 @@ require (
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/godbus/dbus/v5 v5.0.4 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.6.0 // indirect
Expand All @@ -73,8 +67,8 @@ require (
github.com/opencontainers/runtime-spec v1.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/prometheus/statsd_exporter v0.24.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.19.0 // indirect
go.opentelemetry.io/otel/metric v1.19.0 // indirect
Expand Down
90 changes: 0 additions & 90 deletions go.sum

Large diffs are not rendered by default.

13 changes: 4 additions & 9 deletions pkg/instrumentation/instrumentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import (
"fmt"
"sync"

"github.com/prometheus/client_golang/prometheus"

cfgapi "github.com/containers/nri-plugins/pkg/apis/config/v1alpha1/instrumentation"
"github.com/containers/nri-plugins/pkg/http"
"github.com/containers/nri-plugins/pkg/instrumentation/metrics"
Expand Down Expand Up @@ -52,11 +50,6 @@ var (
Attribute = tracing.Attribute
)

// RegisterGatherer registers a prometheus metrics gatherer.
func RegisterGatherer(g prometheus.Gatherer) {
metrics.RegisterGatherer(g)
}

// HTTPServer returns our HTTP server.
func HTTPServer() *http.Server {
return srv
Expand Down Expand Up @@ -122,9 +115,11 @@ func start() error {

if err := metrics.Start(
srv.GetMux(),
metrics.WithNamespace("nri"),
metrics.WithExporterDisabled(!cfg.PrometheusExport),
metrics.WithServiceName(ServiceName),
metrics.WithPeriod(cfg.ReportPeriod.Duration),
metrics.WithReportPeriod(cfg.ReportPeriod.Duration),
// TODO(klihub): make this configurable via apis/config/.../instrumentation.Config
metrics.WithMetrics([]string{"misc/buildinfo"}, []string{"policy"}),
); err != nil {
return fmt.Errorf("failed to start metrics: %v", err)
}
Expand Down
123 changes: 116 additions & 7 deletions pkg/instrumentation/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,123 @@
package metrics

import (
oc "github.com/containers/nri-plugins/pkg/instrumentation/metrics/opencensus"
"fmt"
"slices"
"time"

"github.com/prometheus/client_golang/prometheus/promhttp"

"github.com/containers/nri-plugins/pkg/http"
logger "github.com/containers/nri-plugins/pkg/log"
"github.com/containers/nri-plugins/pkg/metrics"
)

type (
Option func() error
)

var (
RegisterGatherer = oc.RegisterGatherer
WithExporterDisabled = oc.WithExporterDisabled
WithPeriod = oc.WithPeriod
WithServiceName = oc.WithServiceName
Start = oc.Start
Stop = oc.Stop
disabled bool
namespace string
enabled []string
polled []string
reportPeriod time.Duration
mux *http.ServeMux
gatherer *metrics.Gatherer
log = logger.Get("metrics")
)

func WithExporterDisabled(v bool) Option {
return func() error {
disabled = v
return nil
}
}

func WithNamespace(v string) Option {
return func() error {
namespace = v
return nil
}
}

func WithReportPeriod(v time.Duration) Option {
return func() error {
reportPeriod = v
return nil
}
}

func WithMetrics(enable []string, poll []string) Option {
return func() error {
enabled = slices.Clone(enable)
polled = slices.Clone(poll)
return nil
}
}

func Start(m *http.ServeMux, options ...Option) error {
Stop()

for _, opt := range options {
if err := opt(); err != nil {
return err
}
}

if m == nil {
log.Info("no mux provided, metrics exporter disabled")
return nil
}

if disabled {
log.Info("metrics exporter disabled")
return nil
}

log.Info("starting metrics exporter...")

g, err := metrics.NewGatherer(
metrics.WithNamespace("nri"),
metrics.WithPollInterval(reportPeriod),
metrics.WithMetrics(enabled, polled),
)
if err != nil {
return fmt.Errorf("failed to create metrics gatherer: %v", err)
}

gatherer = g

handlerOpts := promhttp.HandlerOpts{
ErrorLog: log,
ErrorHandling: promhttp.ContinueOnError,
}
m.Handle("/metrics", promhttp.HandlerFor(g, handlerOpts))

mux = m

return nil
}

func Stop() {
if mux == nil {
return
}

mux.Unregister("/metrics")
mux = nil
gatherer.Stop()
gatherer = nil
}

func Block() {
if gatherer != nil {
gatherer.Block()
}
}

func Unblock() {
if gatherer != nil {
gatherer.Unblock()
}
}
164 changes: 0 additions & 164 deletions pkg/instrumentation/metrics/opencensus/metrics.go

This file was deleted.

7 changes: 7 additions & 0 deletions pkg/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ type Logger interface {
// Fatal formats and emits an error message and os.Exit()'s with status 1.
Fatal(format string, args ...interface{})

// Println to mimic minimal stdlin log.Logger interface.
Println(v ...any)

// DebugBlock formats and emits a multiline debug message.
DebugBlock(prefix string, format string, args ...interface{})
// InfoBlock formats and emits a multiline information message.
Expand Down Expand Up @@ -410,6 +413,10 @@ func (l logger) Panic(format string, args ...interface{}) {
panic(msg)
}

func (l logger) Println(a ...any) {
l.Info("%s", fmt.Sprintln(a...))
}

func (l logger) DebugBlock(prefix string, format string, args ...interface{}) {
if l.DebugEnabled() {
l.block(LevelDebug, prefix, format, args...)
Expand Down
Loading

0 comments on commit bbdf2e8

Please sign in to comment.