-
Notifications
You must be signed in to change notification settings - Fork 113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Read metrics from file (v2) #1118
Changes from all commits
5393f65
7783939
e7f2c0c
8c25641
0935163
db1fc17
c28411c
dac5a4f
256b4af
0eaecaf
51c6b14
81a59e0
7bb316a
db498a9
55ce5a9
c3b0242
3616aec
3e503b9
cf0eac5
2f86b9b
5e60a12
1059a7a
9d1d53f
29f220a
db62bc1
c271c33
e3f47c5
6191a44
e16eb4a
93180dc
c418d20
f9b3103
e419e36
e6a14f9
ab30488
6aa7da8
69d8a66
66cd435
c6bbced
1f8b376
375b588
3cf7ae2
473da68
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
address = "0.0.0.0:5550" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We're reading the file each time for all the three metrics, but they would be accessed simultaneously. Can you see if we can prevent reading the file three times? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ishank011 Hmm, that's like the initial implementation where the whole pkg got initialized on each metrics server call and all methods where read in one go. It is my recollection that it was decided to changed it to be like this. |
||
} | ||
|
||
// 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 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These need to be defined for the service, not shared conf.