diff --git a/cmd/revad/runtime/loader.go b/cmd/revad/runtime/loader.go index cae629b1ba..2298fe219b 100644 --- a/cmd/revad/runtime/loader.go +++ b/cmd/revad/runtime/loader.go @@ -30,6 +30,7 @@ import ( _ "github.com/cs3org/reva/pkg/auth/manager/loader" _ "github.com/cs3org/reva/pkg/auth/registry/loader" _ "github.com/cs3org/reva/pkg/meshdirectory/manager/loader" + _ "github.com/cs3org/reva/pkg/metrics" _ "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 new file mode 100644 index 0000000000..caa41975f7 --- /dev/null +++ b/examples/metrics/metrics.toml @@ -0,0 +1,7 @@ +[shared] +jwt_secret = "Pive-Fumkiu4" + +[http.services.prometheus] + +[http] +address = "0.0.0.0:5550" \ No newline at end of file diff --git a/internal/http/services/prometheus/prometheus.go b/internal/http/services/prometheus/prometheus.go index 456c8163fd..eb669408bd 100644 --- a/internal/http/services/prometheus/prometheus.go +++ b/internal/http/services/prometheus/prometheus.go @@ -21,6 +21,8 @@ package prometheus import ( "net/http" + "github.com/cs3org/reva/pkg/metrics" + "contrib.go.opencensus.io/exporter/prometheus" "github.com/cs3org/reva/pkg/rhttp/global" "github.com/mitchellh/mapstructure" @@ -53,6 +55,15 @@ func New(m map[string]interface{}, log *zerolog.Logger) (global.Service, error) view.RegisterExporter(pe) + // register the desired measures' views + if err = view.Register( + metrics.GetNumUsersView(), + metrics.GetNumGroupsView(), + metrics.GetAmountStorageView(), + ); err != nil { + return nil, errors.Wrap(err, "prometheus: error registering exporter") + } + return &svc{prefix: conf.Prefix, h: pe}, nil } diff --git a/pkg/metrics/metrics.go b/pkg/metrics/metrics.go new file mode 100644 index 0000000000..03e4690d0e --- /dev/null +++ b/pkg/metrics/metrics.go @@ -0,0 +1,113 @@ +// 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 package defines site metrics measures and views based on opencensus.io + +import ( + "context" + "math/rand" + "time" + + "go.opencensus.io/stats" + "go.opencensus.io/stats/view" +) + +func init() { + // call the actual metric provider functions for the latest metrics every 4th second + go func() { + rand.Seed(time.Now().UnixNano()) + for { + getNumUsers() + getNumGroups() + getAmountStorage() + time.Sleep(4 * time.Second) + } + }() +} + +// Create the measures +var ( + 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) +) + +// initialize local dummy counters +var ( + numUsersCounter = int64(0) + amountStorageCounter = int64(0) +) + +// getNumberUsers links to the underlying number of site users provider +func getNumUsers() { + ctx := context.Background() + // here we must request the actual number of site users + // for now this is a mockup: a number increasing over time + numUsersCounter += int64(rand.Intn(100)) + stats.Record(ctx, NumUsersMeasure.M(numUsersCounter)) +} + +// GetNumUsersView returns the number of site users measure view +func GetNumUsersView() *view.View { + return &view.View{ + Name: NumUsersMeasure.Name(), + Description: NumUsersMeasure.Description(), + Measure: NumUsersMeasure, + Aggregation: view.LastValue(), + } +} + +// getNumberGroups links to the underlying number of site groups provider +func getNumGroups() { + ctx := context.Background() + // here we must request the actual number of site groups + // for now this is a mockup: a number changing over time + var numGroupsCounter = int64(rand.Intn(100)) + stats.Record(ctx, NumGroupsMeasure.M(numGroupsCounter)) +} + +// GetNumGroupsView returns the number of site groups measure view +func GetNumGroupsView() *view.View { + return &view.View{ + Name: NumGroupsMeasure.Name(), + Description: NumGroupsMeasure.Description(), + Measure: NumGroupsMeasure, + Aggregation: view.LastValue(), + } +} + +// getAmountStorage links to the underlying amount of storage provider +func getAmountStorage() { + ctx := context.Background() + // here we must request the actual amount of storage used + // for now this is a mockup: a number increasing over time + amountStorageCounter += int64(rand.Intn(12865000)) + stats.Record(ctx, AmountStorageMeasure.M(amountStorageCounter)) +} + +// GetAmountStorageView returns the amount of site storage measure view +func GetAmountStorageView() *view.View { + return &view.View{ + Name: AmountStorageMeasure.Name(), + Description: AmountStorageMeasure.Description(), + Measure: AmountStorageMeasure, + Aggregation: view.LastValue(), + } +}