From 0b15ec7c662a4f209fa062fd5d3d32d1558e91ee Mon Sep 17 00:00:00 2001 From: Antoon P Date: Mon, 14 Sep 2020 11:20:42 +0200 Subject: [PATCH] Read metrics from file (v2) (#1118) --- .../unreleased/read-metrics-from-file-2.md | 5 + cmd/revad/runtime/loader.go | 2 +- examples/metrics/metrics.toml | 11 +- internal/http/services/loader/loader.go | 1 + internal/http/services/metrics/metrics.go | 92 ++++++++++++ .../http/services/prometheus/prometheus.go | 2 - pkg/metrics/config/config.go | 39 ++++++ pkg/metrics/driver/dummy/dummy.go | 60 ++++++++ pkg/metrics/driver/json/json.go | 98 +++++++++++++ pkg/metrics/driver/loader/loader.go | 26 ++++ pkg/metrics/driver/registry/registry.go | 42 ++++++ pkg/metrics/metrics.go | 130 +++++++++++++++-- pkg/metrics/reader/dummy/dummy.go | 131 ------------------ pkg/metrics/reader/reader.go | 49 +++++++ pkg/metrics/reader/script/script.go | 78 ----------- 15 files changed, 541 insertions(+), 225 deletions(-) create mode 100644 changelog/unreleased/read-metrics-from-file-2.md create mode 100644 internal/http/services/metrics/metrics.go create mode 100644 pkg/metrics/config/config.go create mode 100644 pkg/metrics/driver/dummy/dummy.go create mode 100644 pkg/metrics/driver/json/json.go create mode 100644 pkg/metrics/driver/loader/loader.go create mode 100644 pkg/metrics/driver/registry/registry.go delete mode 100644 pkg/metrics/reader/dummy/dummy.go create mode 100644 pkg/metrics/reader/reader.go delete mode 100644 pkg/metrics/reader/script/script.go diff --git a/changelog/unreleased/read-metrics-from-file-2.md b/changelog/unreleased/read-metrics-from-file-2.md new file mode 100644 index 00000000000..05473b5ba7b --- /dev/null +++ b/changelog/unreleased/read-metrics-from-file-2.md @@ -0,0 +1,5 @@ +Enhancement: Metrics module can be configured to retrieve metrics data from file + +- Export site metrics in Prometheus #698 + +https://github.com/cs3org/reva/pull/1118 diff --git a/cmd/revad/runtime/loader.go b/cmd/revad/runtime/loader.go index ffdbb3bedb8..21175fbde12 100644 --- a/cmd/revad/runtime/loader.go +++ b/cmd/revad/runtime/loader.go @@ -29,7 +29,7 @@ import ( _ "github.com/cs3org/reva/internal/http/services/loader" _ "github.com/cs3org/reva/pkg/auth/manager/loader" _ "github.com/cs3org/reva/pkg/auth/registry/loader" - _ "github.com/cs3org/reva/pkg/metrics" + _ "github.com/cs3org/reva/pkg/metrics/driver/loader" _ "github.com/cs3org/reva/pkg/ocm/invite/manager/loader" _ "github.com/cs3org/reva/pkg/ocm/provider/authorizer/loader" _ "github.com/cs3org/reva/pkg/ocm/share/manager/loader" diff --git a/examples/metrics/metrics.toml b/examples/metrics/metrics.toml index caa41975f76..3dd0a39e73f 100644 --- a/examples/metrics/metrics.toml +++ b/examples/metrics/metrics.toml @@ -1,7 +1,14 @@ [shared] -jwt_secret = "Pive-Fumkiu4" + +[http.services.metrics] +# one of dummy, json. +metrics_data_driver_type = "dummy" +# if left unspecified location /var/tmp/reva/metrics/metricsdata.json is used (for driver type json). +metrics_data_location = "" +# metrics recording interval in milliseconds +metrics_record_interval = 5000 [http.services.prometheus] [http] -address = "0.0.0.0:5550" \ No newline at end of file +address = "0.0.0.0:5550" diff --git a/internal/http/services/loader/loader.go b/internal/http/services/loader/loader.go index 06a4534a494..4c92606a6ec 100644 --- a/internal/http/services/loader/loader.go +++ b/internal/http/services/loader/loader.go @@ -25,6 +25,7 @@ import ( _ "github.com/cs3org/reva/internal/http/services/helloworld" _ "github.com/cs3org/reva/internal/http/services/mentix" _ "github.com/cs3org/reva/internal/http/services/meshdirectory" + _ "github.com/cs3org/reva/internal/http/services/metrics" _ "github.com/cs3org/reva/internal/http/services/ocmd" _ "github.com/cs3org/reva/internal/http/services/oidcprovider" _ "github.com/cs3org/reva/internal/http/services/owncloud/ocdav" diff --git a/internal/http/services/metrics/metrics.go b/internal/http/services/metrics/metrics.go new file mode 100644 index 00000000000..3ac1680afbe --- /dev/null +++ b/internal/http/services/metrics/metrics.go @@ -0,0 +1,92 @@ +// Copyright 2018-2020 CERN +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// In applying this license, CERN does not waive the privileges and immunities +// granted to it by virtue of its status as an Intergovernmental Organization +// or submit itself to any jurisdiction. + +package metrics + +/* +This service initializes the metrics package according to the metrics configuration. +*/ +import ( + "net/http" + "os" + + "github.com/cs3org/reva/pkg/logger" + "github.com/mitchellh/mapstructure" + "github.com/rs/zerolog" + + "github.com/cs3org/reva/pkg/metrics" + "github.com/cs3org/reva/pkg/metrics/config" + "github.com/cs3org/reva/pkg/rhttp/global" +) + +func init() { + global.Register(serviceName, New) +} + +const ( + serviceName = "metrics" +) + +// Close is called when this service is being stopped. +func (s *svc) Close() error { + return nil +} + +// Prefix returns the main endpoint of this service. +func (s *svc) Prefix() string { + return "" +} + +// Unprotected returns all endpoints that can be queried without prior authorization. +func (s *svc) Unprotected() []string { + return []string{} +} + +// Handler serves all HTTP requests. +func (s *svc) Handler() http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + log := logger.New().With().Int("pid", os.Getpid()).Logger() + if _, err := w.Write([]byte("This is the metrics service.\n")); err != nil { + log.Error().Err(err).Msg("error writing metrics response") + } + }) +} + +// New returns a new metrics service. +func New(m map[string]interface{}, log *zerolog.Logger) (global.Service, error) { + // Prepare the configuration + conf := &config.Config{} + if err := mapstructure.Decode(m, conf); err != nil { + return nil, err + } + + conf.Init() + + // initialize metrics using the configuration + err := metrics.Init(conf) + if err != nil { + return nil, err + } + + // Create the service + s := &svc{} + return s, nil +} + +type svc struct { +} diff --git a/internal/http/services/prometheus/prometheus.go b/internal/http/services/prometheus/prometheus.go index 7330fcb111d..3f4ff6cdd28 100644 --- a/internal/http/services/prometheus/prometheus.go +++ b/internal/http/services/prometheus/prometheus.go @@ -28,8 +28,6 @@ import ( "go.opencensus.io/stats/view" "github.com/cs3org/reva/pkg/rhttp/global" - // Initializes goroutines which periodically update stats - _ "github.com/cs3org/reva/pkg/metrics/reader/dummy" ) func init() { diff --git a/pkg/metrics/config/config.go b/pkg/metrics/config/config.go new file mode 100644 index 00000000000..f4d6dbe3b2b --- /dev/null +++ b/pkg/metrics/config/config.go @@ -0,0 +1,39 @@ +// Copyright 2018-2020 CERN +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// In applying this license, CERN does not waive the privileges and immunities +// granted to it by virtue of its status as an Intergovernmental Organization +// or submit itself to any jurisdiction. + +package config + +// Config holds the config options that need to be passed down to the metrics reader(driver) +type Config struct { + MetricsDataDriverType string `mapstructure:"metrics_data_driver_type"` + MetricsDataLocation string `mapstructure:"metrics_data_location"` + MetricsRecordInterval int `mapstructure:"metrics_record_interval"` +} + +// Init sets sane defaults +func (c *Config) Init() { + if c.MetricsDataDriverType == "json" { + // default values + if c.MetricsDataLocation == "" { + c.MetricsDataLocation = "/var/tmp/reva/metrics/metricsdata.json" + } + } + if c.MetricsRecordInterval == 0 { + c.MetricsRecordInterval = 5000 + } +} diff --git a/pkg/metrics/driver/dummy/dummy.go b/pkg/metrics/driver/dummy/dummy.go new file mode 100644 index 00000000000..3d7b8a79501 --- /dev/null +++ b/pkg/metrics/driver/dummy/dummy.go @@ -0,0 +1,60 @@ +// Copyright 2018-2020 CERN +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// In applying this license, CERN does not waive the privileges and immunities +// granted to it by virtue of its status as an Intergovernmental Organization +// or submit itself to any jurisdiction. + +package dummy + +import ( + "math/rand" + + "github.com/cs3org/reva/pkg/metrics/config" + "github.com/cs3org/reva/pkg/metrics/driver/registry" +) + +func init() { + driver := &MetricsDummyDriver{} + registry.Register(driverName(), driver) +} + +func driverName() string { + return "dummy" +} + +// MetricsDummyDriver the MetricsDummyDriver struct +type MetricsDummyDriver struct { +} + +// Configure configures this driver +func (d *MetricsDummyDriver) Configure(c *config.Config) error { + // no configuration necessary + return nil +} + +// GetNumUsers returns the number of site users; it's a random number +func (d *MetricsDummyDriver) GetNumUsers() int64 { + return int64(rand.Intn(30000)) +} + +// GetNumGroups returns the number of site groups; it's a random number +func (d *MetricsDummyDriver) GetNumGroups() int64 { + return int64(rand.Intn(200)) +} + +// GetAmountStorage returns the amount of site storage used; it's a random amount +func (d *MetricsDummyDriver) GetAmountStorage() int64 { + return int64(rand.Intn(70000000000)) +} diff --git a/pkg/metrics/driver/json/json.go b/pkg/metrics/driver/json/json.go new file mode 100644 index 00000000000..a7f23af54da --- /dev/null +++ b/pkg/metrics/driver/json/json.go @@ -0,0 +1,98 @@ +// Copyright 2018-2020 CERN +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// In applying this license, CERN does not waive the privileges and immunities +// granted to it by virtue of its status as an Intergovernmental Organization +// or submit itself to any jurisdiction. + +package json + +import ( + "encoding/json" + "errors" + "io/ioutil" + "os" + + "github.com/cs3org/reva/pkg/metrics/driver/registry" + + "github.com/cs3org/reva/pkg/logger" + "github.com/cs3org/reva/pkg/metrics/config" + "github.com/rs/zerolog" +) + +var log zerolog.Logger + +func init() { + log = logger.New().With().Int("pid", os.Getpid()).Logger() + driver := &MetricsJSONDriver{} + registry.Register(driverName(), driver) +} + +func driverName() string { + return "json" +} + +// readJSON always returns a data object but logs the error in case reading the json fails. +func readJSON(driver *MetricsJSONDriver) *data { + data := &data{} + + file, err := ioutil.ReadFile(driver.metricsDataLocation) + if err != nil { + log.Error().Err(err).Str("location", driver.metricsDataLocation).Msg("Unable to read json file from location.") + } + err = json.Unmarshal(file, data) + if err != nil { + log.Error().Err(err).Msg("Unable to unmarshall json file.") + } + + return data +} + +type data struct { + NumUsers int64 `json:"cs3_org_sciencemesh_site_total_num_users"` + NumGroups int64 `json:"cs3_org_sciencemesh_site_total_num_groups"` + AmountStorage int64 `json:"cs3_org_sciencemesh_site_total_amount_storage"` +} + +// MetricsJSONDriver the JsonDriver struct +type MetricsJSONDriver struct { + metricsDataLocation string +} + +// Configure configures this driver +func (d *MetricsJSONDriver) Configure(c *config.Config) error { + if c.MetricsDataLocation == "" { + err := errors.New("Unable to initialize a metrics data driver, has the data location (metrics_data_location) been configured?") + return err + } + + d.metricsDataLocation = c.MetricsDataLocation + + return nil +} + +// GetNumUsers returns the number of site users +func (d *MetricsJSONDriver) GetNumUsers() int64 { + return readJSON(d).NumUsers +} + +// GetNumGroups returns the number of site groups +func (d *MetricsJSONDriver) GetNumGroups() int64 { + return readJSON(d).NumGroups +} + +// GetAmountStorage returns the amount of site storage used +func (d *MetricsJSONDriver) GetAmountStorage() int64 { + return readJSON(d).AmountStorage +} diff --git a/pkg/metrics/driver/loader/loader.go b/pkg/metrics/driver/loader/loader.go new file mode 100644 index 00000000000..97adaf92788 --- /dev/null +++ b/pkg/metrics/driver/loader/loader.go @@ -0,0 +1,26 @@ +// Copyright 2018-2020 CERN +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// In applying this license, CERN does not waive the privileges and immunities +// granted to it by virtue of its status as an Intergovernmental Organization +// or submit itself to any jurisdiction. + +package loader + +import ( + // Load metrics drivers. + _ "github.com/cs3org/reva/pkg/metrics/driver/dummy" + _ "github.com/cs3org/reva/pkg/metrics/driver/json" + // Add your own here +) diff --git a/pkg/metrics/driver/registry/registry.go b/pkg/metrics/driver/registry/registry.go new file mode 100644 index 00000000000..6c982619f6d --- /dev/null +++ b/pkg/metrics/driver/registry/registry.go @@ -0,0 +1,42 @@ +// Copyright 2018-2020 CERN +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// In applying this license, CERN does not waive the privileges and immunities +// granted to it by virtue of its status as an Intergovernmental Organization +// or submit itself to any jurisdiction. + +package registry + +import ( + "github.com/cs3org/reva/pkg/metrics/reader" +) + +var drivers map[string]reader.Reader // map key is driver type name + +// Register register a driver +func Register(driverName string, r reader.Reader) { + if drivers == nil { + drivers = make(map[string]reader.Reader) + } + drivers[driverName] = r +} + +// GetDriver returns the registered driver for the specified driver name, or nil if it is not registered +func GetDriver(driverName string) reader.Reader { + driver, found := drivers[driverName] + if found { + return driver + } + return nil +} diff --git a/pkg/metrics/metrics.go b/pkg/metrics/metrics.go index 6aa00e15f22..586e4dd41ff 100644 --- a/pkg/metrics/metrics.go +++ b/pkg/metrics/metrics.go @@ -18,22 +18,130 @@ package metrics +/* +Metrics registers OpenCensus data views of the metrics. +Metrics initializes the driver as specified in the configuration. +*/ import ( + "context" + "os" + "time" + + "github.com/cs3org/reva/pkg/metrics/config" + "github.com/cs3org/reva/pkg/metrics/driver/registry" + "github.com/cs3org/reva/pkg/metrics/reader" + + "github.com/cs3org/reva/pkg/logger" + "go.opencensus.io/stats" "go.opencensus.io/stats/view" ) -// Reader is the interface that defines how the metrics will be read. -type Reader interface { +// Init intializes metrics according to the specified configuration +func Init(conf *config.Config) error { + log := logger.New().With().Int("pid", os.Getpid()).Logger() + + driver := registry.GetDriver(conf.MetricsDataDriverType) + + if driver == nil { + log.Info().Msg("No metrics are being recorded.") + // No error but just don't proceed with metrics + return nil + } + + // configure the driver + err := driver.Configure(conf) + if err != nil { + return err + } + + m := &Metrics{ + dataDriver: driver, + NumUsersMeasure: stats.Int64("cs3_org_sciencemesh_site_total_num_users", "The total number of users within this site", stats.UnitDimensionless), + NumGroupsMeasure: stats.Int64("cs3_org_sciencemesh_site_total_num_groups", "The total number of groups within this site", stats.UnitDimensionless), + AmountStorageMeasure: stats.Int64("cs3_org_sciencemesh_site_total_amount_storage", "The total amount of storage used within this site", stats.UnitBytes), + } + + if err := view.Register( + m.getNumUsersView(), + m.getNumGroupsView(), + m.getAmountStorageView(), + ); err != nil { + return err + } + + // periodically record metrics data + go func() { + for { + if err := m.recordMetrics(); err != nil { + log.Error().Err(err).Msg("Metrics recording failed.") + } + <-time.After(time.Millisecond * time.Duration(conf.MetricsRecordInterval)) + } + }() + + return nil +} + +// Metrics the metrics struct +type Metrics struct { + dataDriver reader.Reader // the metrics data driver is an implemention of Reader + NumUsersMeasure *stats.Int64Measure + NumGroupsMeasure *stats.Int64Measure + AmountStorageMeasure *stats.Int64Measure +} + +// RecordMetrics records the latest metrics from the metrics data source as OpenCensus stats views. +func (m *Metrics) recordMetrics() error { + // record all latest metrics + if m.dataDriver != nil { + m.recordNumUsers() + m.recordNumGroups() + m.recordAmountStorage() + } + return nil +} - // GetNumUsersView returns an OpenCensus stats view which records the - // number of users registered in the mesh provider. - GetNumUsersView() *view.View +// recordNumUsers records the latest number of site users figure +func (m *Metrics) recordNumUsers() { + ctx := context.Background() + stats.Record(ctx, m.NumUsersMeasure.M(m.dataDriver.GetNumUsers())) +} + +func (m *Metrics) getNumUsersView() *view.View { + return &view.View{ + Name: m.NumUsersMeasure.Name(), + Description: m.NumUsersMeasure.Description(), + Measure: m.NumUsersMeasure, + Aggregation: view.LastValue(), + } +} - // GetNumGroupsView returns an OpenCensus stats view which records the - // number of user groups registered in the mesh provider. - GetNumGroupsView() *view.View +// recordNumGroups records the latest number of site groups figure +func (m *Metrics) recordNumGroups() { + ctx := context.Background() + stats.Record(ctx, m.NumGroupsMeasure.M(m.dataDriver.GetNumGroups())) +} + +func (m *Metrics) getNumGroupsView() *view.View { + return &view.View{ + Name: m.NumGroupsMeasure.Name(), + Description: m.NumGroupsMeasure.Description(), + Measure: m.NumGroupsMeasure, + Aggregation: view.LastValue(), + } +} + +// recordAmountStorage records the latest amount storage figure +func (m *Metrics) recordAmountStorage() { + ctx := context.Background() + stats.Record(ctx, m.AmountStorageMeasure.M(m.dataDriver.GetAmountStorage())) +} - // GetAmountStorageView returns an OpenCensus stats view which records the - // amount of storage in the system. - GetAmountStorageView() *view.View +func (m *Metrics) getAmountStorageView() *view.View { + return &view.View{ + Name: m.AmountStorageMeasure.Name(), + Description: m.AmountStorageMeasure.Description(), + Measure: m.AmountStorageMeasure, + Aggregation: view.LastValue(), + } } diff --git a/pkg/metrics/reader/dummy/dummy.go b/pkg/metrics/reader/dummy/dummy.go deleted file mode 100644 index d12947c0fcd..00000000000 --- a/pkg/metrics/reader/dummy/dummy.go +++ /dev/null @@ -1,131 +0,0 @@ -// Copyright 2018-2020 CERN -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// In applying this license, CERN does not waive the privileges and immunities -// granted to it by virtue of its status as an Intergovernmental Organization -// or submit itself to any jurisdiction. - -package dummy - -import ( - "context" - "math/rand" - "os" - "time" - - "github.com/cs3org/reva/pkg/logger" - "github.com/cs3org/reva/pkg/metrics" - "go.opencensus.io/stats" - "go.opencensus.io/stats/view" -) - -func init() { - - log := logger.New().With().Int("pid", os.Getpid()).Logger() - - m := &Metrics{ - NumUsersMeasure: stats.Int64("cs3_org_sciencemesh_site_total_num_users", "The total number of users within this site", stats.UnitDimensionless), - NumGroupsMeasure: stats.Int64("cs3_org_sciencemesh_site_total_num_groups", "The total number of groups within this site", stats.UnitDimensionless), - AmountStorageMeasure: stats.Int64("cs3_org_sciencemesh_site_total_amount_storage", "The total amount of storage used within this site", stats.UnitBytes), - } - - // Verify that the struct implements the metrics.Reader interface - _, ok := interface{}(m).(metrics.Reader) - if !ok { - log.Error().Msg("the driver does not implement the metrics.Reader interface") - return - } - - // register the desired measures' views - if err := view.Register( - m.GetNumUsersView(), - m.GetNumGroupsView(), - m.GetAmountStorageView(), - ); err != nil { - log.Error().Err(err).Msg("error registering views with opencensus exporter") - return - } - - // call the actual metric provider functions for the latest metrics every 4th second - go func() { - rand.Seed(time.Now().UnixNano()) - for { - m.getNumUsers() - m.getNumGroups() - m.getAmountStorage() - time.Sleep(4 * time.Second) - } - }() -} - -// Metrics returns randomly generated values for the defined metrics. -type Metrics struct { - numUsersCounter int64 - amountStorageCounter int64 - - NumUsersMeasure *stats.Int64Measure - NumGroupsMeasure *stats.Int64Measure - AmountStorageMeasure *stats.Int64Measure -} - -// getNumberUsers links to the underlying number of site users provider -func (m *Metrics) getNumUsers() { - ctx := context.Background() - m.numUsersCounter += int64(rand.Intn(100)) - stats.Record(ctx, m.NumUsersMeasure.M(m.numUsersCounter)) -} - -// GetNumUsersView returns the number of site users measure view -func (m *Metrics) GetNumUsersView() *view.View { - return &view.View{ - Name: m.NumUsersMeasure.Name(), - Description: m.NumUsersMeasure.Description(), - Measure: m.NumUsersMeasure, - Aggregation: view.LastValue(), - } -} - -// getNumberGroups links to the underlying number of site groups provider -func (m *Metrics) getNumGroups() { - ctx := context.Background() - var numGroupsCounter = int64(rand.Intn(100)) - stats.Record(ctx, m.NumGroupsMeasure.M(numGroupsCounter)) -} - -// GetNumGroupsView returns the number of site groups measure view -func (m *Metrics) GetNumGroupsView() *view.View { - return &view.View{ - Name: m.NumGroupsMeasure.Name(), - Description: m.NumGroupsMeasure.Description(), - Measure: m.NumGroupsMeasure, - Aggregation: view.LastValue(), - } -} - -// getAmountStorage links to the underlying amount of storage provider -func (m *Metrics) getAmountStorage() { - ctx := context.Background() - m.amountStorageCounter += int64(rand.Intn(12865000)) - stats.Record(ctx, m.AmountStorageMeasure.M(m.amountStorageCounter)) -} - -// GetAmountStorageView returns the amount of site storage measure view -func (m *Metrics) GetAmountStorageView() *view.View { - return &view.View{ - Name: m.AmountStorageMeasure.Name(), - Description: m.AmountStorageMeasure.Description(), - Measure: m.AmountStorageMeasure, - Aggregation: view.LastValue(), - } -} diff --git a/pkg/metrics/reader/reader.go b/pkg/metrics/reader/reader.go new file mode 100644 index 00000000000..650e50bdd87 --- /dev/null +++ b/pkg/metrics/reader/reader.go @@ -0,0 +1,49 @@ +// Copyright 2018-2020 CERN +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// In applying this license, CERN does not waive the privileges and immunities +// granted to it by virtue of its status as an Intergovernmental Organization +// or submit itself to any jurisdiction. + +package reader + +/* +Reader is the interface that defines the metrics to read. +Any metrics data driver must implement this interface. +Each metric function should return the current/latest available metrics figure relevant to that function. +*/ + +import "github.com/cs3org/reva/pkg/metrics/config" + +// Reader the Reader interface +type Reader interface { + + // Configure configures the reader according to the specified configuration + Configure(c *config.Config) error + + // GetNumUsersView returns an OpenCensus stats view which records the + // number of users registered in the mesh provider. + // Metric name: cs3_org_sciencemesh_site_total_num_users + GetNumUsers() int64 + + // GetNumGroupsView returns an OpenCensus stats view which records the + // number of user groups registered in the mesh provider. + // Metric name: cs3_org_sciencemesh_site_total_num_groups + GetNumGroups() int64 + + // GetAmountStorageView returns an OpenCensus stats view which records the + // amount of storage in the system. + // Metric name: cs3_org_sciencemesh_site_total_amount_storage + GetAmountStorage() int64 +} diff --git a/pkg/metrics/reader/script/script.go b/pkg/metrics/reader/script/script.go deleted file mode 100644 index 79617d5c00a..00000000000 --- a/pkg/metrics/reader/script/script.go +++ /dev/null @@ -1,78 +0,0 @@ -// Copyright 2018-2020 CERN -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// In applying this license, CERN does not waive the privileges and immunities -// granted to it by virtue of its status as an Intergovernmental Organization -// or submit itself to any jurisdiction. - -package script - -import ( - "os" - - "github.com/cs3org/reva/pkg/logger" - "github.com/cs3org/reva/pkg/metrics" - "go.opencensus.io/stats" - "go.opencensus.io/stats/view" -) - -func init() { - - log := logger.New().With().Int("pid", os.Getpid()).Logger() - - m := &Metrics{ - NumUsersMeasure: stats.Int64("cs3_org_sciencemesh_site_total_num_users", "The total number of users within this site", stats.UnitDimensionless), - NumGroupsMeasure: stats.Int64("cs3_org_sciencemesh_site_total_num_groups", "The total number of groups within this site", stats.UnitDimensionless), - AmountStorageMeasure: stats.Int64("cs3_org_sciencemesh_site_total_amount_storage", "The total amount of storage used within this site", stats.UnitBytes), - } - - // Verify that the struct implements the metrics.Reader interface - _, ok := interface{}(m).(metrics.Reader) - if !ok { - log.Error().Msg("the driver does not implement the metrics.Reader interface") - return - } - - // register the desired measures' views - if err := view.Register( - m.GetNumUsersView(), - m.GetNumGroupsView(), - m.GetAmountStorageView(), - ); err != nil { - log.Error().Err(err).Msg("error registering views with opencensus exporter") - return - } -} - -// Metrics returns randomly generated values for the defined metrics. -type Metrics struct { - NumUsersMeasure *stats.Int64Measure - NumGroupsMeasure *stats.Int64Measure - AmountStorageMeasure *stats.Int64Measure -} - -// GetNumUsersView returns the number of site users measure view -func (m *Metrics) GetNumUsersView() *view.View { - return nil -} - -// GetNumGroupsView returns the number of site groups measure view -func (m *Metrics) GetNumGroupsView() *view.View { - return nil -} - -// GetAmountStorageView returns the amount of site storage measure view -func (m *Metrics) GetAmountStorageView() *view.View { - return nil -}