feat(pkger): extend metrics for export functionality

pull/18649/head^2
Johnny Steenbergen 2020-06-24 22:05:10 -07:00 committed by Johnny Steenbergen
parent cb8fe33da8
commit c67a6ee1ea
1 changed files with 80 additions and 2 deletions

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"net/url" "net/url"
"path" "path"
"strconv"
"strings" "strings"
"github.com/influxdata/influxdb/v2" "github.com/influxdata/influxdb/v2"
@ -25,7 +26,7 @@ var _ SVC = (*mwMetrics)(nil)
func MWMetrics(reg *prom.Registry) SVCMiddleware { func MWMetrics(reg *prom.Registry) SVCMiddleware {
return func(svc SVC) SVC { return func(svc SVC) SVC {
return &mwMetrics{ return &mwMetrics{
rec: metric.New(reg, "pkger", metric.WithVec(templateVec())), rec: metric.New(reg, "pkger", metric.WithVec(templateVec()), metric.WithVec(exportVec())),
next: svc, next: svc,
} }
} }
@ -62,8 +63,21 @@ func (s *mwMetrics) UpdateStack(ctx context.Context, upd StackUpdate) (Stack, er
func (s *mwMetrics) Export(ctx context.Context, opts ...ExportOptFn) (*Pkg, error) { func (s *mwMetrics) Export(ctx context.Context, opts ...ExportOptFn) (*Pkg, error) {
rec := s.rec.Record("create_pkg") rec := s.rec.Record("create_pkg")
opt, err := exportOptFromOptFns(opts)
if err != nil {
return nil, rec(err)
}
pkg, err := s.next.Export(ctx, opts...) pkg, err := s.next.Export(ctx, opts...)
return pkg, rec(err) if err != nil {
return nil, err
}
return pkg, rec(err, metric.RecordAdditional(map[string]interface{}{
"num_org_ids": len(opt.OrgIDs),
"summary": pkg.Summary(),
"by_stack": opt.StackID != 0,
}))
} }
func (s *mwMetrics) DryRun(ctx context.Context, orgID, userID influxdb.ID, opts ...ApplyOptFn) (ImpactSummary, error) { func (s *mwMetrics) DryRun(ctx context.Context, orgID, userID influxdb.ID, opts ...ApplyOptFn) (ImpactSummary, error) {
@ -86,6 +100,70 @@ func applyMetricAdditions(orgID, userID influxdb.ID, sources []string) func(*met
}) })
} }
func exportVec() metric.VecOpts {
const (
byStack = "by_stack"
numOrgIDs = "num_org_ids"
bkts = "buckets"
checks = "checks"
dashes = "dashboards"
endpoints = "endpoints"
labels = "labels"
labelMappings = "label_mappings"
rules = "rules"
tasks = "tasks"
telegrafConfigs = "telegraf_configs"
variables = "variables"
)
return metric.VecOpts{
Name: "template_export",
Help: "Metrics for resources being exported",
LabelNames: []string{
"method",
byStack,
numOrgIDs,
bkts,
checks,
dashes,
endpoints,
labels,
labelMappings,
rules,
tasks,
telegrafConfigs,
variables,
},
CounterFn: func(vec *prometheus.CounterVec, o metric.CollectFnOpts) {
if o.Err != nil {
return
}
orgID, _ := o.AdditionalProps[numOrgIDs].(int)
sum, _ := o.AdditionalProps["sum"].(Summary)
st, _ := o.AdditionalProps[byStack].(bool)
vec.
With(prometheus.Labels{
"method": o.Method,
byStack: strconv.FormatBool(st),
numOrgIDs: strconv.Itoa(orgID),
bkts: strconv.Itoa(len(sum.Buckets)),
checks: strconv.Itoa(len(sum.Checks)),
dashes: strconv.Itoa(len(sum.Dashboards)),
endpoints: strconv.Itoa(len(sum.NotificationEndpoints)),
labels: strconv.Itoa(len(sum.Labels)),
labelMappings: strconv.Itoa(len(sum.LabelMappings)),
rules: strconv.Itoa(len(sum.NotificationRules)),
tasks: strconv.Itoa(len(sum.Tasks)),
telegrafConfigs: strconv.Itoa(len(sum.TelegrafConfigs)),
variables: strconv.Itoa(len(sum.TelegrafConfigs)),
}).
Inc()
},
HistogramFn: nil,
}
}
func templateVec() metric.VecOpts { func templateVec() metric.VecOpts {
return metric.VecOpts{ return metric.VecOpts{
Name: "template_count", Name: "template_count",