github.com/cs3org/reva/v2@v2.27.7/pkg/mentix/exchangers/exporters/metrics.go (about)

     1  // Copyright 2018-2020 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 exporters
    20  
    21  import (
    22  	"github.com/cs3org/reva/v2/pkg/mentix/config"
    23  	"github.com/cs3org/reva/v2/pkg/mentix/exchangers/exporters/metrics"
    24  	"github.com/cs3org/reva/v2/pkg/mentix/meshdata"
    25  	"github.com/pkg/errors"
    26  	"github.com/rs/zerolog"
    27  )
    28  
    29  // MetricsExporter exposes various Prometheus metrics.
    30  type MetricsExporter struct {
    31  	BaseExporter
    32  
    33  	metrics *metrics.Metrics
    34  }
    35  
    36  // Activate activates the exporter.
    37  func (exporter *MetricsExporter) Activate(conf *config.Configuration, log *zerolog.Logger) error {
    38  	if err := exporter.BaseExporter.Activate(conf, log); err != nil {
    39  		return err
    40  	}
    41  
    42  	// Create the metrics handler
    43  	m, err := metrics.New(conf, log)
    44  	if err != nil {
    45  		return errors.Wrap(err, "unable to create metrics")
    46  	}
    47  	exporter.metrics = m
    48  
    49  	// Store Metrics specifics
    50  	exporter.SetEnabledConnectors(conf.Exporters.Metrics.EnabledConnectors)
    51  
    52  	return nil
    53  }
    54  
    55  // Update is called whenever the mesh data set has changed to reflect these changes.
    56  func (exporter *MetricsExporter) Update(meshDataSet meshdata.Map) error {
    57  	if err := exporter.BaseExporter.Update(meshDataSet); err != nil {
    58  		return err
    59  	}
    60  
    61  	// Data is read, so acquire a read lock
    62  	exporter.Locker().RLock()
    63  	defer exporter.Locker().RUnlock()
    64  
    65  	if err := exporter.metrics.Update(exporter.MeshData()); err != nil {
    66  		return errors.Wrap(err, "error while updating the metrics")
    67  	}
    68  
    69  	return nil
    70  }
    71  
    72  // GetID returns the ID of the exporter.
    73  func (exporter *MetricsExporter) GetID() string {
    74  	return config.ExporterIDMetrics
    75  }
    76  
    77  // GetName returns the display name of the exporter.
    78  func (exporter *MetricsExporter) GetName() string {
    79  	return "Metrics"
    80  }
    81  
    82  func init() {
    83  	registerExporter(&MetricsExporter{})
    84  }