github.com/cs3org/reva/v2@v2.27.7/internal/grpc/interceptors/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  	"context"
    23  
    24  	"github.com/cs3org/reva/v2/pkg/rgrpc"
    25  	"github.com/prometheus/client_golang/prometheus"
    26  	"github.com/prometheus/client_golang/prometheus/promauto"
    27  	"google.golang.org/grpc"
    28  )
    29  
    30  const (
    31  	defaultPriority = 100
    32  )
    33  
    34  func init() {
    35  	rgrpc.RegisterUnaryInterceptor("prometheus", NewUnary)
    36  }
    37  
    38  // NewUnary returns a new unary interceptor
    39  // that counts grpc calls.
    40  func NewUnary(m map[string]interface{}) (grpc.UnaryServerInterceptor, int, error) {
    41  	interceptor, err := interceptorFromConfig(m)
    42  	if err != nil {
    43  		return nil, 0, err
    44  	}
    45  	return interceptor, defaultPriority, nil
    46  }
    47  
    48  // NewStream returns a new server stream interceptor
    49  // that counts grpc calls.
    50  func NewStream() grpc.StreamServerInterceptor {
    51  	interceptor := func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
    52  		err := handler(srv, ss)
    53  		// TODO count res codes & errors
    54  		return err
    55  	}
    56  	return interceptor
    57  }
    58  
    59  func interceptorFromConfig(m map[string]interface{}) (grpc.UnaryServerInterceptor, error) {
    60  	namespace := m["namespace"].(string)
    61  	if namespace == "" {
    62  		namespace = "reva"
    63  	}
    64  	subsystem := m["subsystem"].(string)
    65  	reqProcessed := promauto.NewCounter(prometheus.CounterOpts{
    66  		Namespace: namespace,
    67  		Subsystem: subsystem,
    68  		Name:      "grpc_requests_total",
    69  		Help:      "The total number of processed " + subsystem + " GRPC requests for " + namespace,
    70  	})
    71  	interceptor := func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
    72  		res, err := handler(ctx, req)
    73  		reqProcessed.Inc()
    74  		return res, err
    75  	}
    76  	return interceptor, nil
    77  }