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 translation metrics #9862

Closed
wants to merge 4 commits into from
Closed
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
6 changes: 3 additions & 3 deletions pkg/utils/setuputils/setup_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"

"github.com/solo-io/gloo/pkg/bootstrap/leaderelector"
"github.com/solo-io/gloo/pkg/utils/statsutils"

"github.com/solo-io/gloo/pkg/utils"
"github.com/solo-io/gloo/pkg/utils/settingsutil"
v1 "github.com/solo-io/gloo/projects/gloo/pkg/api/v1"
"github.com/solo-io/go-utils/contextutils"
Expand All @@ -17,7 +17,7 @@ import (
)

var (
mSetupsRun = utils.MakeSumCounter("gloo.solo.io/setups_run", "The number of times the main setup loop has run")
mSetupsRun = statsutils.MakeSumCounter("gloo.solo.io/setups_run", "The number of times the main setup loop has run")
)

// tell us how to setup
Expand Down Expand Up @@ -52,7 +52,7 @@ func (s *SetupSyncer) Sync(ctx context.Context, snap *v1.SetupSnapshot) error {

contextutils.LoggerFrom(ctx).Debugw("received settings snapshot", zap.Any("settings", settings))

utils.MeasureOne(
statsutils.MeasureOne(
ctx,
mSetupsRun,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"

errors "github.com/rotisserie/eris"
"github.com/solo-io/gloo/pkg/utils"
"github.com/solo-io/gloo/pkg/utils/statsutils"
gwv1 "github.com/solo-io/gloo/projects/gateway/pkg/api/v1"
gloov1 "github.com/solo-io/gloo/projects/gloo/pkg/api/v1"
"github.com/solo-io/go-utils/contextutils"
Expand Down Expand Up @@ -135,7 +135,7 @@ func (m *ConfigStatusMetrics) SetResourceValid(ctx context.Context, resource res
if err != nil {
log.Errorf("Error setting labels on %s: %s", Names[gvk], err.Error())
}
utils.MeasureZero(ctx, m.metrics[gvk].gauge, mutators...)
statsutils.MeasureZero(ctx, m.metrics[gvk].gauge, mutators...)
}
}

Expand All @@ -152,7 +152,7 @@ func (m *ConfigStatusMetrics) SetResourceInvalid(ctx context.Context, resource r
if err != nil {
log.Errorf("Error setting labels on %s: %s", Names[gvk], err.Error())
}
utils.MeasureOne(ctx, m.metrics[gvk].gauge, mutators...)
statsutils.MeasureOne(ctx, m.metrics[gvk].gauge, mutators...)
}
}

Expand Down Expand Up @@ -224,7 +224,7 @@ func newResourceMetric(gvk schema.GroupVersionKind, labelToPath map[string]strin
i++
}
return &resourceMetric{
gauge: utils.MakeGauge(Names[gvk], descriptions[gvk], tagKeys...),
gauge: statsutils.MakeGauge(Names[gvk], descriptions[gvk], tagKeys...),
labelToPath: labelToPath,
}, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/solo-io/gloo/pkg/utils/statsutils/metrics"
gwv1 "github.com/solo-io/gloo/projects/gateway/pkg/api/v1"
"github.com/solo-io/gloo/projects/gateway/pkg/utils/metrics"
gloov1 "github.com/solo-io/gloo/projects/gloo/pkg/api/v1"
"github.com/solo-io/gloo/test/helpers"
"github.com/solo-io/solo-kit/pkg/api/v1/resources"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package utils
package statsutils

import (
"context"
Expand Down
51 changes: 51 additions & 0 deletions pkg/utils/statsutils/stopwatch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package statsutils

import (
"context"
"time"

"github.com/prometheus/client_golang/prometheus"
"github.com/solo-io/go-utils/contextutils"
)

type StopWatch interface {
Start(ctx context.Context)
Stop(ctx context.Context)
}

type stopwatch struct {
hist prometheus.Observer
inProgress prometheus.Gauge
currentTime *time.Time
}

func NewStopWatch(hist prometheus.Observer) StopWatch {
return &stopwatch{hist: hist}
}

// Start begins the counter for the recorded value.
func (s *stopwatch) Start(ctx context.Context) {
if s.currentTime != nil {
contextutils.LoggerFrom(ctx).DPanic("Start() should not be called twice without calling Stop()")
return
}
now := time.Now()
s.currentTime = &now
if s.inProgress != nil {
s.inProgress.Inc()
}
}

// Stop ends the counter for the recorded value. This method should be called directly after
// Start(), in a defer statement.
func (s *stopwatch) Stop(ctx context.Context) {
if s.currentTime == nil {
contextutils.LoggerFrom(ctx).DPanic("Stop() should only be called after Start()")
return
}
s.hist.Observe(time.Since(*s.currentTime).Seconds())
s.currentTime = nil
if s.inProgress != nil {
s.inProgress.Dec()
}
}
56 changes: 56 additions & 0 deletions pkg/utils/statsutils/translator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package statsutils

import (
"github.com/prometheus/client_golang/prometheus"
"sigs.k8s.io/controller-runtime/pkg/metrics"
)

var (
translationRecorder *prometheus.HistogramVec
statusSyncerRecorder *prometheus.HistogramVec
)

func init() {
translationRecorder = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "translation_time_sec",
Namespace: "gloo_edge",
Help: "how long the translator takes in seconds",
Buckets: []float64{0.25, 0.5, 0.75, 1, 2.5, 5, 10, 20, 30, 60},
},
[]string{"translator_name"},
)

statusSyncerRecorder = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "status_syncer_time_sec",
Namespace: "gloo_edge",
Help: "how long the status syncer takes in seconds",
Buckets: []float64{0.25, 0.5, 0.75, 1, 2.5, 5, 10, 20, 30, 60},
},
[]string{"status_syncer_name"},
)

metrics.Registry.MustRegister(translationRecorder)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure this will work; I don't think /metrics currently serves the controller-runtime metrics

metrics.Registry.MustRegister(statusSyncerRecorder)
}

type StopWatchFactory interface {
// NewTranslatorStopWatch returns a stop watch for the given translator
NewTranslatorStopWatch(translatorName string) StopWatch

// NewStatusSyncerStopWatch returns a stop watch for the given status syncer
NewStatusSyncerStopWatch(statusSyncerName string) StopWatch
}

func NewStatusSyncerStopWatch(statusSyncerName string) StopWatch {
return &stopwatch{
hist: statusSyncerRecorder.WithLabelValues(statusSyncerName),
}
}

func NewTranslatorStopWatch(translatorName string) StopWatch {
return &stopwatch{
hist: translationRecorder.WithLabelValues(translatorName),
}
}
8 changes: 4 additions & 4 deletions projects/accesslogger/pkg/runner/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
envoy_data_accesslog_v3 "github.com/envoyproxy/go-control-plane/envoy/data/accesslog/v3"
pb "github.com/envoyproxy/go-control-plane/envoy/service/accesslog/v3"
_struct "github.com/golang/protobuf/ptypes/struct"
"github.com/solo-io/gloo/pkg/utils"
"github.com/solo-io/gloo/pkg/utils/statsutils"
"github.com/solo-io/gloo/projects/accesslogger/pkg/loggingservice"
"github.com/solo-io/gloo/projects/gloo/pkg/plugins/transformation"
"github.com/solo-io/go-utils/contextutils"
Expand Down Expand Up @@ -105,7 +105,7 @@ func Run() {
// https://docs.solo.io/gloo-edge/latest/guides/security/auth/jwt/access_control/#appendix---use-a-remote-json-web-key-set-jwks-server
issuer := getClaimFromJwtInDynamicMetadata("iss", meta)

utils.MeasureOne(
statsutils.MeasureOne(
ctx,
mAccessLogsRequests,
tag.Insert(responseCodeKey, v.GetResponse().GetResponseCode().String()),
Expand All @@ -121,15 +121,15 @@ func Run() {
// otherwise, you want this
// upstreamRespTimeNs := firstToFirstNs(v)

utils.Measure(
statsutils.Measure(
ctx,
mAccessLogsDownstreamRespTime,
downstreamRespTimeNs,
tag.Insert(responseCodeKey, v.GetResponse().GetResponseCode().String()),
tag.Insert(clusterKey, v.GetCommonProperties().GetUpstreamCluster()),
tag.Insert(requestMethodKey, v.GetRequest().GetRequestMethod().String()))

utils.Measure(
statsutils.Measure(
ctx,
mAccessLogsUpstreamRespTime,
upstreamRespTimeNs,
Expand Down
16 changes: 8 additions & 8 deletions projects/gateway/pkg/services/k8sadmission/cert_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"time"
"unsafe"

"github.com/solo-io/gloo/pkg/utils"
"github.com/solo-io/gloo/pkg/utils/statsutils"
"go.opencensus.io/tag"
)

Expand All @@ -25,8 +25,8 @@ type certificateProvider struct {
}

func NewCertificateProvider(certPath, keyPath string, logger *log.Logger, ctx context.Context, interval time.Duration) (*certificateProvider, error) {
mReloadSuccess := utils.MakeSumCounter("validation.gateway.solo.io/certificate_reload_success", "Number of successful certificate reloads")
mReloadFailed := utils.MakeSumCounter("validation.gateway.solo.io/certificate_reload_failed", "Number of failed certificate reloads")
mReloadSuccess := statsutils.MakeSumCounter("validation.gateway.solo.io/certificate_reload_success", "Number of successful certificate reloads")
mReloadFailed := statsutils.MakeSumCounter("validation.gateway.solo.io/certificate_reload_failed", "Number of failed certificate reloads")
tagKey, err := tag.NewKey("error")
if err != nil {
return nil, err
Expand All @@ -43,7 +43,7 @@ func NewCertificateProvider(certPath, keyPath string, logger *log.Logger, ctx co
if err != nil {
return nil, err
}
utils.MeasureOne(ctx, mReloadSuccess)
statsutils.MeasureOne(ctx, mReloadSuccess)
result := &certificateProvider{
ctx: ctx,
logger: logger,
Expand All @@ -68,13 +68,13 @@ func NewCertificateProvider(certPath, keyPath string, logger *log.Logger, ctx co
certFileInfo, err := os.Stat(certPath)
if err != nil {
result.logger.Printf("Error while checking if validating admission webhook certificate file changed %s", err)
utils.MeasureOne(ctx, mReloadFailed, tag.Insert(tagKey, fmt.Sprintf("%s", err)))
statsutils.MeasureOne(ctx, mReloadFailed, tag.Insert(tagKey, fmt.Sprintf("%s", err)))
continue
}
keyFileInfo, err := os.Stat(keyPath)
if err != nil {
result.logger.Printf("Error while checking if validating admission webhook private key file changed %s", err)
utils.MeasureOne(ctx, mReloadFailed, tag.Insert(tagKey, fmt.Sprintf("%s", err)))
statsutils.MeasureOne(ctx, mReloadFailed, tag.Insert(tagKey, fmt.Sprintf("%s", err)))
continue
}
km := keyFileInfo.ModTime()
Expand All @@ -85,10 +85,10 @@ func NewCertificateProvider(certPath, keyPath string, logger *log.Logger, ctx co
result.logger.Println("Reloaded validating admission webhook certificate")
result.keyMtime = km
result.certMtime = cm
utils.MeasureOne(ctx, mReloadSuccess)
statsutils.MeasureOne(ctx, mReloadSuccess)
} else {
result.logger.Printf("Error while reloading validating admission webhook certificate %s, will keep using the old certificate", err)
utils.MeasureOne(ctx, mReloadFailed, tag.Insert(tagKey, fmt.Sprintf("%s", err)))
statsutils.MeasureOne(ctx, mReloadFailed, tag.Insert(tagKey, fmt.Sprintf("%s", err)))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

"github.com/hashicorp/go-multierror"
"github.com/solo-io/gloo/pkg/utils/statsutils"

"github.com/ghodss/yaml"

Expand Down Expand Up @@ -54,8 +55,8 @@ var (
resourceTypeKey, _ = tag.NewKey("resource_type")
resourceRefKey, _ = tag.NewKey("resource_ref")

mGatewayResourcesAccepted = utils.MakeSumCounter("validation.gateway.solo.io/resources_accepted", "The number of resources accepted")
mGatewayResourcesRejected = utils.MakeSumCounter("validation.gateway.solo.io/resources_rejected", "The number of resources rejected")
mGatewayResourcesAccepted = statsutils.MakeSumCounter("validation.gateway.solo.io/resources_accepted", "The number of resources accepted")
mGatewayResourcesRejected = statsutils.MakeSumCounter("validation.gateway.solo.io/resources_rejected", "The number of resources rejected")

unmarshalErrMsg = "could not unmarshal raw object"
UnmarshalErr = errors.New(unmarshalErrMsg)
Expand All @@ -75,7 +76,7 @@ const (
)

func incrementMetric(ctx context.Context, resource string, ref *core.ResourceRef, m *stats.Int64Measure) {
utils.MeasureOne(
statsutils.MeasureOne(
ctx,
m,
tag.Insert(resourceTypeKey, resource),
Expand Down
2 changes: 1 addition & 1 deletion projects/gateway/pkg/syncer/translator_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"time"

"github.com/solo-io/gloo/pkg/bootstrap/leaderelector"
"github.com/solo-io/gloo/pkg/utils/statsutils/metrics"

"github.com/solo-io/gloo/projects/gateway/pkg/utils/metrics"
gloo_translator "github.com/solo-io/gloo/projects/gloo/pkg/translator"
"github.com/solo-io/solo-kit/pkg/api/v1/clients"
"github.com/solo-io/solo-kit/pkg/errors"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ import (
"time"

"github.com/solo-io/gloo/pkg/bootstrap/leaderelector/singlereplica"
"github.com/solo-io/gloo/pkg/utils/statsutils/metrics"

"github.com/solo-io/gloo/pkg/utils/statusutils"
"github.com/solo-io/gloo/projects/gateway/pkg/utils/metrics"

"github.com/solo-io/solo-kit/pkg/api/v1/resources"

. "github.com/onsi/ginkgo/v2"
Expand Down
5 changes: 2 additions & 3 deletions projects/gateway/pkg/syncer/translator_syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ import (
"sync"

"github.com/solo-io/gloo/pkg/bootstrap/leaderelector/singlereplica"

"github.com/solo-io/gloo/pkg/utils/statusutils"
"github.com/solo-io/gloo/projects/gateway/pkg/utils/metrics"
"github.com/solo-io/gloo/pkg/utils/statsutils/metrics"

"github.com/golang/mock/gomock"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/solo-io/gloo/pkg/utils/settingsutil"
"github.com/solo-io/gloo/pkg/utils/statusutils"
gatewayv1 "github.com/solo-io/gloo/projects/gateway/pkg/api/v1"
"github.com/solo-io/gloo/projects/gateway/pkg/reconciler"
gatewaymocks "github.com/solo-io/gloo/projects/gateway/pkg/translator/mocks"
Expand Down
2 changes: 1 addition & 1 deletion projects/gateway/pkg/translator/opts.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package translator

import (
"github.com/solo-io/gloo/projects/gateway/pkg/utils/metrics"
"github.com/solo-io/gloo/pkg/utils/statsutils/metrics"
"github.com/solo-io/solo-kit/pkg/api/v1/clients"
"github.com/solo-io/solo-kit/pkg/api/v1/clients/factory"
)
Expand Down
2 changes: 1 addition & 1 deletion projects/gateway/pkg/validation/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import (
"reflect"
"sync"

utils2 "github.com/solo-io/gloo/pkg/utils/statsutils"
gloov1snap "github.com/solo-io/gloo/projects/gloo/pkg/api/v1/gloosnapshot"
"github.com/solo-io/go-utils/hashutils"

"github.com/hashicorp/go-multierror"
errors "github.com/rotisserie/eris"
utils2 "github.com/solo-io/gloo/pkg/utils"
sologatewayv1 "github.com/solo-io/gloo/projects/gateway/pkg/api/v1"
"github.com/solo-io/gloo/projects/gateway/pkg/translator"
"github.com/solo-io/gloo/projects/gateway/pkg/utils"
Expand Down
6 changes: 3 additions & 3 deletions projects/gateway/pkg/validation/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/hashicorp/go-multierror"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/solo-io/gloo/pkg/utils"
"github.com/solo-io/gloo/pkg/utils/statsutils"
v1 "github.com/solo-io/gloo/projects/gateway/pkg/api/v1"
"github.com/solo-io/gloo/projects/gateway/pkg/translator"
"github.com/solo-io/gloo/projects/gloo/pkg/api/grpc/validation"
Expand Down Expand Up @@ -61,7 +61,7 @@ var _ = Describe("Validator", func() {
ExtensionValidator: extensionValidator,
AllowWarnings: false,
})
mValidConfig = utils.MakeGauge("validation.gateway.solo.io/valid_config", "A boolean indicating whether gloo config is valid")
mValidConfig = statsutils.MakeGauge("validation.gateway.solo.io/valid_config", "A boolean indicating whether gloo config is valid")
})

It("returns error before sync called", func() {
Expand Down Expand Up @@ -747,7 +747,7 @@ var _ = Describe("Validator", func() {
Context("valid config gauge", func() {
BeforeEach(func() {
// reset the value before each test
utils.Measure(context.TODO(), mValidConfig, -1)
statsutils.Measure(context.TODO(), mValidConfig, -1)
})
It("returns 1 when there are no validation errors", func() {
v.glooValidator = ValidateAccept
Expand Down
Loading
Loading