github.com/cs3org/reva/v2@v2.27.7/internal/http/services/metrics/metrics.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 metrics
    20  
    21  /*
    22  This service initializes the metrics package according to the metrics configuration.
    23  */
    24  import (
    25  	"net/http"
    26  	"os"
    27  
    28  	"github.com/cs3org/reva/v2/pkg/logger"
    29  	"github.com/mitchellh/mapstructure"
    30  	"github.com/rs/zerolog"
    31  
    32  	"github.com/cs3org/reva/v2/pkg/metrics"
    33  	"github.com/cs3org/reva/v2/pkg/metrics/config"
    34  	"github.com/cs3org/reva/v2/pkg/rhttp/global"
    35  )
    36  
    37  func init() {
    38  	global.Register(serviceName, New)
    39  }
    40  
    41  const (
    42  	serviceName = "metrics"
    43  )
    44  
    45  // Close is called when this service is being stopped.
    46  func (s *svc) Close() error {
    47  	return nil
    48  }
    49  
    50  // Prefix returns the main endpoint of this service.
    51  func (s *svc) Prefix() string {
    52  	// We use a dummy endpoint as the service is not expected to be exposed
    53  	// directly to the user, but just start a background process.
    54  	return "register_metrics"
    55  }
    56  
    57  // Unprotected returns all endpoints that can be queried without prior authorization.
    58  func (s *svc) Unprotected() []string {
    59  	return []string{}
    60  }
    61  
    62  // Handler serves all HTTP requests.
    63  func (s *svc) Handler() http.Handler {
    64  	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    65  		log := logger.New().With().Int("pid", os.Getpid()).Logger()
    66  		if _, err := w.Write([]byte("This is the metrics service.\n")); err != nil {
    67  			log.Error().Err(err).Msg("error writing metrics response")
    68  		}
    69  	})
    70  }
    71  
    72  // New returns a new metrics service.
    73  func New(m map[string]interface{}, log *zerolog.Logger) (global.Service, error) {
    74  	// Prepare the configuration
    75  	conf := &config.Config{}
    76  	if err := mapstructure.Decode(m, conf); err != nil {
    77  		return nil, err
    78  	}
    79  
    80  	conf.Init()
    81  
    82  	// initialize metrics using the configuration
    83  	err := metrics.Init(conf)
    84  	if err != nil {
    85  		return nil, err
    86  	}
    87  
    88  	// Create the service
    89  	s := &svc{}
    90  	return s, nil
    91  }
    92  
    93  type svc struct {
    94  }