Skip to content
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

discovery: Add metrics for itopo and idiscovery #3181

Merged
merged 3 commits into from
Sep 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go/lib/infra/modules/idiscovery/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ go_library(
"//go/lib/discovery:go_default_library",
"//go/lib/discovery/topofetcher:go_default_library",
"//go/lib/fatal:go_default_library",
"//go/lib/infra/modules/idiscovery/internal/metrics:go_default_library",
"//go/lib/infra/modules/itopo:go_default_library",
"//go/lib/log:go_default_library",
"//go/lib/periodic:go_default_library",
Expand Down
32 changes: 29 additions & 3 deletions go/lib/infra/modules/idiscovery/idiscovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import (
"github.com/scionproto/scion/go/lib/discovery"
"github.com/scionproto/scion/go/lib/discovery/topofetcher"
"github.com/scionproto/scion/go/lib/fatal"
"github.com/scionproto/scion/go/lib/infra/modules/idiscovery/internal/metrics"
"github.com/scionproto/scion/go/lib/infra/modules/itopo"
"github.com/scionproto/scion/go/lib/log"
"github.com/scionproto/scion/go/lib/periodic"
Expand Down Expand Up @@ -329,19 +330,40 @@ func (t *task) Run(ctx context.Context) {

func (t *task) handleErr(ctx context.Context, err error) {
t.logger(ctx).Error("[discovery] Unable to fetch topology", "err", err)
l := metrics.FetcherLabels{Static: t.static(), Result: metrics.ErrRequest}
metrics.Fetcher.Sent(l).Inc()
}

func (t *task) handleRaw(ctx context.Context, raw common.RawBytes, topo *topology.Topo) {
l := metrics.FetcherLabels{Static: t.static(), Result: metrics.Success}
updated, err := t.callHandler(ctx, topo)
switch {
case err != nil:
l.Result = metrics.ErrUpdate
case !updated:
l.Result = metrics.OkIgnored
}
if err != nil || t.filename == "" || !updated {
metrics.Fetcher.Sent(l).Inc()
return
}
if err := t.writeFile(ctx, raw); err != nil {
l.Result = metrics.ErrWriteFile
}
metrics.Fetcher.Sent(l).Inc()
}

func (t *task) writeFile(ctx context.Context, raw common.RawBytes) error {
l := metrics.FetcherLabels{Static: t.static(), Result: metrics.Success}
if err := util.WriteFile(t.filename, raw, 0644); err != nil {
t.logger(ctx).Error("[discovery] Unable to write new topology to filesystem", "err", err)
return
metrics.Fetcher.File(l.WithResult(metrics.ErrWriteFile)).Inc()
return err
}
t.logger(ctx).Trace("[discovery] Topology written to filesystem",
"file", t.filename, "params", t.fetcher.Params)
t.logger(ctx).Trace("[discovery] Topology written to filesystem", "file", t.filename,
"params", t.fetcher.Params)
metrics.Fetcher.File(l).Inc()
return nil
}

func (t *task) callHandler(ctx context.Context, topo *topology.Topo) (bool, error) {
Expand All @@ -354,6 +376,10 @@ func (t *task) callHandler(ctx context.Context, topo *topology.Topo) (bool, erro
return updated, err
}

func (t *task) static() bool {
return t.mode == discovery.Static
}

func (t *task) logger(ctx context.Context) log.Logger {
return log.FromCtx(ctx).New("mode", t.mode)
}
Expand Down
12 changes: 12 additions & 0 deletions go/lib/infra/modules/idiscovery/internal/metrics/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "go_default_library",
srcs = ["metrics.go"],
importpath = "github.com/scionproto/scion/go/lib/infra/modules/idiscovery/internal/metrics",
visibility = ["//go/lib/infra/modules/idiscovery:__subpackages__"],
deps = [
"//go/lib/prom:go_default_library",
"@com_github_prometheus_client_golang//prometheus:go_default_library",
],
)
90 changes: 90 additions & 0 deletions go/lib/infra/modules/idiscovery/internal/metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright 2019 Anapaya Systems
//
// 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.

package metrics

import (
"github.com/prometheus/client_golang/prometheus"

"github.com/scionproto/scion/go/lib/prom"
)

// Namespace is the metrics namespace for the infra discovery module.
const Namespace = "idiscovery"

// Topology types.
const (
Static = "static"
Dynamic = "dynamic"
)

// Result labels.
const (
Success = prom.Success
OkIgnored = "ok_ignored"
ErrRequest = "err_request"
ErrUpdate = "err_update"
ErrWriteFile = "err_write_file"
)

// Fetcher is the single-instance struct to get prometheus counters.
var Fetcher = newFetcher()

// FetcherLabels defines the requests label set.
type FetcherLabels struct {
Static bool
Result string
}

// Labels returns the name of the labels in correct order.
func (l FetcherLabels) Labels() []string {
return []string{"type", "result"}
}

// Values returns the values of the label in correct order.
func (l FetcherLabels) Values() []string {
if l.Static {
return []string{Static, l.Result}
}
return []string{Dynamic, l.Result}
}

// WithResult returns the label set with the modified result.
func (l FetcherLabels) WithResult(result string) FetcherLabels {
l.Result = result
return l
}

type fetcher struct {
sent, file *prometheus.CounterVec
}

func newFetcher() fetcher {
return fetcher{
sent: prom.NewCounterVec(Namespace, "", "sent_requests_total",
"The total number of requests sent to the discovey service", FetcherLabels{}.Labels()),
file: prom.NewCounterVec(Namespace, "", "file_writes_total",
"The total number of file writes on updated topology", FetcherLabels{}.Labels()),
}
}

// Sent returns the prometheus counter.
func (r fetcher) Sent(l FetcherLabels) prometheus.Counter {
return r.sent.WithLabelValues(l.Values()...)
}

// File returns the prometheus counter.
func (r fetcher) File(l FetcherLabels) prometheus.Counter {
return r.file.WithLabelValues(l.Values()...)
}
1 change: 1 addition & 0 deletions go/lib/infra/modules/itopo/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ go_library(
visibility = ["//visibility:public"],
deps = [
"//go/lib/common:go_default_library",
"//go/lib/infra/modules/itopo/internal/metrics:go_default_library",
"//go/lib/log:go_default_library",
"//go/lib/periodic:go_default_library",
"//go/lib/topology:go_default_library",
Expand Down
3 changes: 1 addition & 2 deletions go/lib/infra/modules/itopo/cleaner.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ func (c cleaner) Run(ctx context.Context) {
log.FromCtx(ctx).Info("[itopo.cleaner] Dropping expired dynamic topology",
"ts", st.topo.dynamic.Timestamp, "ttl", st.topo.dynamic.TTL,
"expired", st.topo.dynamic.Expiry())
st.topo.dynamic = nil
call(st.clbks.CleanDynamic)
st.dropDynamic()
}
}
12 changes: 12 additions & 0 deletions go/lib/infra/modules/itopo/internal/metrics/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "go_default_library",
srcs = ["metrics.go"],
importpath = "github.com/scionproto/scion/go/lib/infra/modules/itopo/internal/metrics",
visibility = ["//go/lib/infra/modules/itopo:__subpackages__"],
deps = [
"//go/lib/prom:go_default_library",
"@com_github_prometheus_client_golang//prometheus:go_default_library",
],
)
159 changes: 159 additions & 0 deletions go/lib/infra/modules/itopo/internal/metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
// Copyright 2019 Anapaya Systems
//
// 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.

package metrics

import (
"math"
"time"

"github.com/prometheus/client_golang/prometheus"

"github.com/scionproto/scion/go/lib/prom"
)

// Namespace is the metrics namespace for the infra topology module.
const Namespace = "itopo"

// Topology types.
const (
Static = "static"
Dynamic = "dynamic"
)

// Result labels.
const (
Success = prom.Success
OkIgnored = "ok_ignored"
ErrValidate = "err_validate"
ErrCommit = "err_commit"
)

var (
// Current is the single-instance struct to get prometheus gauges.
Current = newCurrent()
// Updates is the single-instance struct to get prometheus counters and gauges.
Updates = newUpdates()
)

// CurrentLabels defines the current topology label set.
type CurrentLabels struct {
Type string
}

// Labels returns the name of the labels in correct order.
func (l CurrentLabels) Labels() []string {
return []string{"type"}
}

// Values returns the values of the label in correct order.
func (l CurrentLabels) Values() []string {
return []string{l.Type}
}

type current struct {
timestamp *prometheus.GaugeVec
expiry *prometheus.GaugeVec
active prometheus.Gauge
}

func newCurrent() current {
return current{
timestamp: prom.NewGaugeVec(Namespace, "", "creation_time_seconds",
"The creation time specified in the current topology."+
"Remains set for dynamic topology, even when inactive.", CurrentLabels{}.Labels()),
expiry: prom.NewGaugeVec(Namespace, "", "expiry_time_seconds",
"The expiry time specified in the current topology. Set to +Inf, if TTL is zero."+
"Remains set for dynamic topology, even when inactive.", CurrentLabels{}.Labels()),
active: prom.NewGauge(Namespace, "", "dynamic_active",
"Indicate whether the dynamic topology is set and active. 0=inactive, 1=active."),
}
}

// Timestamp returns the prometheus gauge.
func (c current) Timestamp(l CurrentLabels) prometheus.Gauge {
return c.timestamp.WithLabelValues(l.Values()...)
}

// Expiry returns the prometheus gauge.
func (c current) Expiry(l CurrentLabels) prometheus.Gauge {
return c.expiry.WithLabelValues(l.Values()...)
}

// Active returns the prometheus gauge.
func (c current) Active() prometheus.Gauge {
return c.active
}

// UpdateLabels defines the update label set.
type UpdateLabels struct {
Type, Result string
}

// Labels returns the name of the labels in correct order.
func (l UpdateLabels) Labels() []string {
return []string{"type", prom.LabelResult}
}

// Values returns the values of the label in correct order.
func (l UpdateLabels) Values() []string {
return []string{l.Type, l.Result}
}

// WithResult returns the label set with the modified result.
func (l UpdateLabels) WithResult(result string) UpdateLabels {
l.Result = result
return l
}

type updates struct {
last *prometheus.GaugeVec
total *prometheus.CounterVec
}

func newUpdates() updates {
return updates{
last: prom.NewGaugeVec(Namespace, "", "last_updates",
"Timestamp of the last update attempts.", UpdateLabels{}.Labels()),
total: prom.NewCounterVec(Namespace, "", "updates_total",
"The total number of updates.", UpdateLabels{}.Labels()),
}
}

// Last returns the prometheus gauge.
func (u updates) Last(l UpdateLabels) prometheus.Gauge {
return u.last.WithLabelValues(l.Values()...)
}

// Total returns the prometheus counter.
func (u updates) Total(l UpdateLabels) prometheus.Counter {
return u.total.WithLabelValues(l.Values()...)
}

// Timestamp returns the time as unix time in seconds.
func Timestamp(ts time.Time) float64 {
if ts.IsZero() {
return 0
}
return float64(ts.UnixNano() / 1e9)
}

// Expiry returns the expiry time as unix time in seconds. In case of the zero
// value, +inf is returned.
func Expiry(ts time.Time) float64 {
if ts.IsZero() {
return math.Inf(+1)
}
return float64(ts.UnixNano() / 1e9)
}
Loading