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

Add metrics for SCIOND client API #3254

Merged
merged 3 commits into from
Oct 16, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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/sciond/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ go_library(
"//go/lib/hostinfo:go_default_library",
"//go/lib/infra/disp:go_default_library",
"//go/lib/log:go_default_library",
"//go/lib/sciond/internal/metrics:go_default_library",
"//go/lib/serrors:go_default_library",
"//go/lib/sock/reliable:go_default_library",
"//go/lib/util:go_default_library",
Expand Down
12 changes: 12 additions & 0 deletions go/lib/sciond/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/sciond/internal/metrics",
visibility = ["//go/lib/sciond:__subpackages__"],
deps = [
"//go/lib/prom:go_default_library",
"@com_github_prometheus_client_golang//prometheus:go_default_library",
],
)
110 changes: 110 additions & 0 deletions go/lib/sciond/internal/metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Copyright 2019 ETH Zurich
//
// 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"
)

const (
// Namespace is the metrics namespace for the SCIOND client API.
Namespace = "lib_sciond"

subsystemConn = "conn"
subsystemPath = "path"
subsystemASInfo = "as_info"
subsystemIFInfo = "if_info"
subsystemSVCInfo = "service_info"
subsystemRevocation = "revocation"
)

// Result values
const (
OkSuccess = prom.Success
ErrTimeout = prom.ErrTimeout
ErrNotClassified = prom.ErrNotClassified
)

var resultLabel = []string{prom.LabelResult}

// Metric accessors.
var (
// PathRequests contains metrics for path requests.
PathRequests = newPathRequest()
// Revocations contains metrics for revocations.
Revocations = newRevocation()
// ASInfos contains metrics for AS info requests.
ASInfos = newASInfoRequest()
// IFInfos contains metrics for IF info requests.
IFInfos = newIFInfo()
// SVCInfos contains metrics for SVC info requests.
SVCInfos = newSVCInfo()
// Conns contains metrics for connections to SCIOND.
Conns = newConn()
)

// Request is the generic metric for requests.
type Request struct {
count *prometheus.CounterVec
}

// Inc increases the metric count. The result parameter is used to label the increment.
func (r Request) Inc(result string) {
r.count.WithLabelValues(result).Inc()
}

func newConn() Request {
return Request{
count: prom.NewCounterVec(Namespace, subsystemConn, "connections_total",
"The amount of SCIOND connection attempts.", resultLabel),
}
}

func newPathRequest() Request {
return Request{
count: prom.NewCounterVec(Namespace, subsystemPath, "request_total",
"The amount of Path requests sent.", resultLabel),
}
}

func newRevocation() Request {
return Request{
count: prom.NewCounterVec(Namespace, subsystemRevocation, "requests_total",
"The amount of Revocation requests sent.", resultLabel),
}
}

func newASInfoRequest() Request {
return Request{
count: prom.NewCounterVec(Namespace, subsystemASInfo, "requests_total",
"The amount of AS info requests sent.", resultLabel),
}
}

func newSVCInfo() Request {
return Request{
count: prom.NewCounterVec(Namespace, subsystemSVCInfo, "requests_total",
"The amount of SVC info requests sent.", resultLabel),
}
}

func newIFInfo() Request {
return Request{
count: prom.NewCounterVec(Namespace, subsystemIFInfo, "requests_total",
"The amount of IF info requests sent.", resultLabel),
}
}
29 changes: 29 additions & 0 deletions go/lib/sciond/sciond.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"github.com/scionproto/scion/go/lib/ctrl/path_mgmt"
"github.com/scionproto/scion/go/lib/infra/disp"
"github.com/scionproto/scion/go/lib/log"
"github.com/scionproto/scion/go/lib/sciond/internal/metrics"
"github.com/scionproto/scion/go/lib/serrors"
"github.com/scionproto/scion/go/lib/sock/reliable"
"github.com/scionproto/scion/go/proto"
Expand Down Expand Up @@ -177,12 +178,14 @@ func (c *conn) ctxAwareConnect(ctx context.Context) (*disp.Dispatcher, error) {

select {
case rValue := <-barrier:
metrics.Conns.Inc(errorToPrometheusLabel(rValue.err))
return rValue.dispatcher, rValue.err
case <-ctx.Done():
// In the situation where ConnectTimeout doesn't finish and ctx is Done
// via a cancellation function, this may (1) permanently leak a
// goroutine, if ctx doesn't have a deadline, or (2) for a long amount
// of time, if the deadline is very far into the future.
metrics.Conns.Inc(errorToPrometheusLabel(ctx.Err()))
return nil, ctx.Err()
}
}
Expand All @@ -192,6 +195,7 @@ func (c *conn) Paths(ctx context.Context, dst, src addr.IA, max uint16,

roundTripper, err := c.ctxAwareConnect(ctx)
if err != nil {
metrics.PathRequests.Inc(errorToPrometheusLabel(err))
return nil, serrors.Wrap(ErrUnableToConnect, err)
}
defer roundTripper.Close(ctx)
Expand All @@ -210,14 +214,17 @@ func (c *conn) Paths(ctx context.Context, dst, src addr.IA, max uint16,
nil,
)
if err != nil {
metrics.PathRequests.Inc(errorToPrometheusLabel(err))
return nil, serrors.WrapStr("[sciond-API] Failed to get Paths", err)
}
metrics.PathRequests.Inc(metrics.OkSuccess)
return reply.(*Pld).PathReply, nil
}

func (c *conn) ASInfo(ctx context.Context, ia addr.IA) (*ASInfoReply, error) {
roundTripper, err := c.ctxAwareConnect(ctx)
if err != nil {
metrics.ASInfos.Inc(errorToPrometheusLabel(err))
return nil, serrors.Wrap(ErrUnableToConnect, err)
}
defer roundTripper.Close(ctx)
Expand All @@ -233,14 +240,17 @@ func (c *conn) ASInfo(ctx context.Context, ia addr.IA) (*ASInfoReply, error) {
nil,
)
if err != nil {
metrics.ASInfos.Inc(errorToPrometheusLabel(err))
return nil, serrors.WrapStr("[sciond-API] Failed to get ASInfo", err)
}
metrics.ASInfos.Inc(metrics.OkSuccess)
return pld.(*Pld).AsInfoReply, nil
}

func (c *conn) IFInfo(ctx context.Context, ifs []common.IFIDType) (*IFInfoReply, error) {
roundTripper, err := c.ctxAwareConnect(ctx)
if err != nil {
metrics.IFInfos.Inc(errorToPrometheusLabel(err))
return nil, serrors.Wrap(ErrUnableToConnect, err)
}
defer roundTripper.Close(ctx)
Expand All @@ -256,8 +266,10 @@ func (c *conn) IFInfo(ctx context.Context, ifs []common.IFIDType) (*IFInfoReply,
nil,
)
if err != nil {
metrics.IFInfos.Inc(errorToPrometheusLabel(err))
return nil, serrors.WrapStr("[sciond-API] Failed to get IFInfo", err)
}
metrics.IFInfos.Inc(metrics.OkSuccess)
return pld.(*Pld).IfInfoReply, nil
}

Expand All @@ -266,6 +278,7 @@ func (c *conn) SVCInfo(ctx context.Context,

roundTripper, err := c.ctxAwareConnect(ctx)
if err != nil {
metrics.SVCInfos.Inc(errorToPrometheusLabel(err))
return nil, serrors.Wrap(ErrUnableToConnect, err)
}
defer roundTripper.Close(ctx)
Expand All @@ -281,8 +294,10 @@ func (c *conn) SVCInfo(ctx context.Context,
nil,
)
if err != nil {
metrics.SVCInfos.Inc(errorToPrometheusLabel(err))
return nil, serrors.WrapStr("[sciond-API] Failed to get SVCInfo", err)
}
metrics.SVCInfos.Inc(metrics.OkSuccess)
return pld.(*Pld).ServiceInfoReply, nil
}

Expand All @@ -300,6 +315,7 @@ func (c *conn) RevNotification(ctx context.Context,

roundTripper, err := c.ctxAwareConnect(ctx)
if err != nil {
metrics.Revocations.Inc(errorToPrometheusLabel(err))
return nil, serrors.Wrap(ErrUnableToConnect, err)
}
defer roundTripper.Close(ctx)
Expand All @@ -315,8 +331,10 @@ func (c *conn) RevNotification(ctx context.Context,
nil,
)
if err != nil {
metrics.Revocations.Inc(errorToPrometheusLabel(err))
return nil, serrors.WrapStr("[sciond-API] Failed to send RevNotification", err)
}
metrics.Revocations.Inc(metrics.OkSuccess)
return reply.(*Pld).RevReply, nil
}

Expand Down Expand Up @@ -348,3 +366,14 @@ func GetDefaultSCIONDPath(ia *addr.IA) string {
}
return fmt.Sprintf("/run/shm/sciond/sd%s.sock", ia.FileFmt(false))
}

func errorToPrometheusLabel(err error) string {
switch {
case err == nil:
return metrics.OkSuccess
case serrors.IsTimeout(err):
return metrics.ErrTimeout
default:
return metrics.ErrNotClassified
}
}