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 9, 2024
1 parent 0e91fe1 commit c0a1157
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 180 deletions.
11 changes: 2 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,9 @@ 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),
); err != nil {
return fmt.Errorf("failed to start metrics: %v", err)
}
Expand Down
105 changes: 98 additions & 7 deletions pkg/instrumentation/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,105 @@
package metrics

import (
oc "github.com/containers/nri-plugins/pkg/instrumentation/metrics/opencensus"
"fmt"
"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
enable []string
poll []string
reportPeriod time.Duration
mux *http.ServeMux
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 {
enable = enable
poll = 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(enable, poll),
)
if err != nil {
return fmt.Errorf("failed to create metrics gatherer: %v", err)
}

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
}
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 mimick 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

0 comments on commit c0a1157

Please sign in to comment.