package corehttp import ( "net" "net/http" "time" core "gitlab.dms3.io/dms3/go-dms3/core" "go.opencensus.io/stats/view" "go.opencensus.io/zpages" ocprom "contrib.go.opencensus.io/exporter/prometheus" prometheus "github.com/prometheus/client_golang/prometheus" promhttp "github.com/prometheus/client_golang/prometheus/promhttp" ) // This adds the scraping endpoint which Prometheus uses to fetch metrics. func MetricsScrapingOption(path string) ServeOption { return func(n *core.Dms3Node, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) { mux.Handle(path, promhttp.HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{})) return mux, nil } } // This adds collection of OpenCensus metrics func MetricsOpenCensusCollectionOption() ServeOption { return func(_ *core.Dms3Node, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) { log.Info("Init OpenCensus") promRegistry := prometheus.NewRegistry() pe, err := ocprom.NewExporter(ocprom.Options{ Namespace: "dms3_oc", Registry: promRegistry, OnError: func(err error) { log.Errorw("OC ERROR", "error", err) }, }) if err != nil { return nil, err } // register prometheus with opencensus view.RegisterExporter(pe) view.SetReportingPeriod(2 * time.Second) // Construct the mux zpages.Handle(mux, "/debug/metrics/oc/debugz") mux.Handle("/debug/metrics/oc", pe) return mux, nil } } // This adds collection of net/http-related metrics func MetricsCollectionOption(handlerName string) ServeOption { return func(_ *core.Dms3Node, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) { promRegistry := prometheus.NewRegistry() // Adapted from github.com/prometheus/client_golang/prometheus/http.go // Work around https://github.com/prometheus/client_golang/pull/311 opts := prometheus.SummaryOpts{ Namespace: "dms3", Subsystem: "http", ConstLabels: prometheus.Labels{"handler": handlerName}, Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001}, } reqCnt := prometheus.NewCounterVec( prometheus.CounterOpts{ Namespace: opts.Namespace, Subsystem: opts.Subsystem, Name: "requests_total", Help: "Total number of HTTP requests made.", ConstLabels: opts.ConstLabels, }, []string{"method", "code"}, ) if err := promRegistry.Register(reqCnt); err != nil { if are, ok := err.(prometheus.AlreadyRegisteredError); ok { reqCnt = are.ExistingCollector.(*prometheus.CounterVec) } else { return nil, err } } opts.Name = "request_duration_seconds" opts.Help = "The HTTP request latencies in seconds." reqDur := prometheus.NewSummaryVec(opts, nil) if err := promRegistry.Register(reqDur); err != nil { if are, ok := err.(prometheus.AlreadyRegisteredError); ok { reqDur = are.ExistingCollector.(*prometheus.SummaryVec) } else { return nil, err } } opts.Name = "request_size_bytes" opts.Help = "The HTTP request sizes in bytes." reqSz := prometheus.NewSummaryVec(opts, nil) if err := promRegistry.Register(reqSz); err != nil { if are, ok := err.(prometheus.AlreadyRegisteredError); ok { reqSz = are.ExistingCollector.(*prometheus.SummaryVec) } else { return nil, err } } opts.Name = "response_size_bytes" opts.Help = "The HTTP response sizes in bytes." resSz := prometheus.NewSummaryVec(opts, nil) if err := promRegistry.Register(resSz); err != nil { if are, ok := err.(prometheus.AlreadyRegisteredError); ok { resSz = are.ExistingCollector.(*prometheus.SummaryVec) } else { return nil, err } } // Construct the mux childMux := http.NewServeMux() var promMux http.Handler = childMux promMux = promhttp.InstrumentHandlerResponseSize(resSz, promMux) promMux = promhttp.InstrumentHandlerRequestSize(reqSz, promMux) promMux = promhttp.InstrumentHandlerDuration(reqDur, promMux) promMux = promhttp.InstrumentHandlerCounter(reqCnt, promMux) mux.Handle("/", promMux) return childMux, nil } } var ( peersTotalMetric = prometheus.NewDesc( prometheus.BuildFQName("dms3", "p2p", "peers_total"), "Number of connected peers", []string{"transport"}, nil) unixfsGetMetric = prometheus.NewSummaryVec(prometheus.SummaryOpts{ Namespace: "dms3", Subsystem: "http", Name: "unixfs_get_latency_seconds", Help: "The time till the first block is received when 'getting' a file from the gateway.", }, []string{"namespace"}) ) type Dms3NodeCollector struct { Node *core.Dms3Node } func (_ Dms3NodeCollector) Describe(ch chan<- *prometheus.Desc) { ch <- peersTotalMetric } func (c Dms3NodeCollector) Collect(ch chan<- prometheus.Metric) { for tr, val := range c.PeersTotalValues() { ch <- prometheus.MustNewConstMetric( peersTotalMetric, prometheus.GaugeValue, val, tr, ) } } func (c Dms3NodeCollector) PeersTotalValues() map[string]float64 { vals := make(map[string]float64) if c.Node.PeerHost == nil { return vals } for _, conn := range c.Node.PeerHost.Network().Conns() { tr := "" for _, proto := range conn.RemoteMultiaddr().Protocols() { tr = tr + "/" + proto.Name } vals[tr] = vals[tr] + 1 } return vals }