github.com/cs3org/reva/v2@v2.27.7/internal/http/services/prometheus/prometheus.go (about)

     1  // Copyright 2018-2021 CERN
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // In applying this license, CERN does not waive the privileges and immunities
    16  // granted to it by virtue of its status as an Intergovernmental Organization
    17  // or submit itself to any jurisdiction.
    18  
    19  package prometheus
    20  
    21  import (
    22  	"net/http"
    23  
    24  	"contrib.go.opencensus.io/exporter/prometheus"
    25  	"github.com/mitchellh/mapstructure"
    26  	"github.com/pkg/errors"
    27  	"github.com/rs/zerolog"
    28  	"go.opencensus.io/stats/view"
    29  
    30  	"github.com/cs3org/reva/v2/pkg/rhttp/global"
    31  )
    32  
    33  func init() {
    34  	global.Register("prometheus", New)
    35  }
    36  
    37  // New returns a new prometheus service
    38  func New(m map[string]interface{}, log *zerolog.Logger) (global.Service, error) {
    39  	conf := &config{}
    40  	if err := mapstructure.Decode(m, conf); err != nil {
    41  		return nil, err
    42  	}
    43  
    44  	conf.init()
    45  
    46  	pe, err := prometheus.NewExporter(prometheus.Options{
    47  		Namespace: "revad",
    48  	})
    49  	if err != nil {
    50  		return nil, errors.Wrap(err, "prometheus: error creating exporter")
    51  	}
    52  
    53  	view.RegisterExporter(pe)
    54  	return &svc{prefix: conf.Prefix, h: pe}, nil
    55  }
    56  
    57  type config struct {
    58  	Prefix string `mapstructure:"prefix"`
    59  }
    60  
    61  func (c *config) init() {
    62  	if c.Prefix == "" {
    63  		c.Prefix = "metrics"
    64  	}
    65  }
    66  
    67  type svc struct {
    68  	prefix string
    69  	h      http.Handler
    70  }
    71  
    72  func (s *svc) Prefix() string {
    73  	return s.prefix
    74  }
    75  
    76  func (s *svc) Handler() http.Handler {
    77  	return s.h
    78  }
    79  
    80  func (s *svc) Close() error {
    81  	return nil
    82  }
    83  
    84  func (s *svc) Unprotected() []string {
    85  	// TODO(labkode): all prometheus endpoints are public?
    86  	return []string{"/"}
    87  }