github.com/decred/dcrlnd@v0.7.6/monitoring/monitoring_on.go (about)

     1  //go:build monitoring
     2  // +build monitoring
     3  
     4  package monitoring
     5  
     6  import (
     7  	"net/http"
     8  	"sync"
     9  
    10  	"google.golang.org/grpc"
    11  
    12  	"github.com/decred/dcrlnd/lncfg"
    13  	"github.com/grpc-ecosystem/go-grpc-prometheus"
    14  	"github.com/prometheus/client_golang/prometheus/promhttp"
    15  )
    16  
    17  var started sync.Once
    18  
    19  // GetPromInterceptors returns the set of interceptors for Prometheus
    20  // monitoring.
    21  func GetPromInterceptors() ([]grpc.UnaryServerInterceptor, []grpc.StreamServerInterceptor) {
    22  	unaryInterceptors := []grpc.UnaryServerInterceptor{
    23  		grpc_prometheus.UnaryServerInterceptor,
    24  	}
    25  	streamInterceptors := []grpc.StreamServerInterceptor{
    26  		grpc_prometheus.StreamServerInterceptor,
    27  	}
    28  	return unaryInterceptors, streamInterceptors
    29  }
    30  
    31  // ExportPrometheusMetrics sets server options, registers gRPC metrics and
    32  // launches the Prometheus exporter on the specified address.
    33  func ExportPrometheusMetrics(grpcServer *grpc.Server, cfg lncfg.Prometheus) error {
    34  	started.Do(func() {
    35  		log.Infof("Prometheus exporter started on %v/metrics", cfg.Listen)
    36  
    37  		grpc_prometheus.Register(grpcServer)
    38  
    39  		http.Handle("/metrics", promhttp.Handler())
    40  		go func() {
    41  			http.ListenAndServe(cfg.Listen, nil)
    42  		}()
    43  	})
    44  
    45  	return nil
    46  }