Skip to content

Commit

Permalink
stats: micro-optimize prometheus (counter, gauge) increment
Browse files Browse the repository at this point in the history
* related commit: e6814a2

Signed-off-by: Alex Aizman <[email protected]>
  • Loading branch information
alex-aizman committed Jan 29, 2025
1 parent 465fc10 commit 6514aa1
Show file tree
Hide file tree
Showing 13 changed files with 104 additions and 70 deletions.
2 changes: 1 addition & 1 deletion ais/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -1674,8 +1674,8 @@ func (p *proxy) listObjects(w http.ResponseWriter, r *http.Request, bck *meta.Bc
}

vlabs := map[string]string{stats.VarlabBucket: bck.Cname("")}
p.statsT.IncWith(stats.ListCount, vlabs)
p.statsT.AddWith(
cos.NamedVal64{Name: stats.ListCount, Value: 1, VarLabs: vlabs},
cos.NamedVal64{Name: stats.ListLatency, Value: mono.SinceNano(beg), VarLabs: vlabs},
)

Expand Down
2 changes: 1 addition & 1 deletion ais/prxs3.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,8 @@ func (p *proxy) lsAllPagesS3(bck *meta.Bck, amsg *apc.ActMsg, lsmsg *apc.LsoMsg,
}

vlabs := map[string]string{stats.VarlabBucket: bck.Cname("")}
p.statsT.IncWith(stats.ListCount, vlabs)
p.statsT.AddWith(
cos.NamedVal64{Name: stats.ListCount, Value: 1, VarLabs: vlabs},
cos.NamedVal64{Name: stats.ListLatency, Value: mono.SinceNano(beg), VarLabs: vlabs},
)
if pageNum == 1 {
Expand Down
36 changes: 9 additions & 27 deletions ais/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -775,17 +775,13 @@ func (t *target) getObject(w http.ResponseWriter, r *http.Request, dpq *dpq, bck
if ecode, err := goi.getObject(); err != nil {
vlabs := map[string]string{stats.VarlabBucket: bck.Cname("")}
if goi.isIOErr {
t.statsT.AddWith(
cos.NamedVal64{Name: stats.ErrGetCount, Value: 1, VarLabs: vlabs},
cos.NamedVal64{Name: stats.IOErrGetCount, Value: 1, VarLabs: vlabs},
)
t.statsT.IncWith(stats.ErrGetCount, vlabs)
t.statsT.IncWith(stats.IOErrGetCount, vlabs)
if cmn.Rom.FastV(4, cos.SmoduleAIS) {
nlog.Warningln("io-error [", err, "]", goi.lom.String())
}
} else {
t.statsT.AddWith(
cos.NamedVal64{Name: stats.ErrGetCount, Value: 1, VarLabs: vlabs},
)
t.statsT.IncWith(stats.ErrGetCount, vlabs)
}

// handle right here, return nil
Expand Down Expand Up @@ -896,9 +892,7 @@ func (t *target) httpobjput(w http.ResponseWriter, r *http.Request, apireq *apiR
return
}
vlabs := map[string]string{stats.VarlabBucket: lom.Bck().Cname("")}
t.statsT.AddWith(
cos.NamedVal64{Name: stats.ErrAppendCount, Value: 1, VarLabs: vlabs},
)
t.statsT.IncWith(stats.ErrAppendCount, vlabs)
default:
poi := allocPOI()
{
Expand Down Expand Up @@ -990,9 +984,7 @@ func (t *target) httpobjpost(w http.ResponseWriter, r *http.Request, apireq *api
lom = nil
} else {
vlabs := map[string]string{stats.VarlabBucket: lom.Bck().Cname("")}
t.statsT.AddWith(
cos.NamedVal64{Name: stats.ErrRenameCount, Value: 1, VarLabs: vlabs},
)
t.statsT.IncWith(stats.ErrRenameCount, vlabs)
}
case apc.ActBlobDl:
// TODO: add stats.GetBlobCount and *ErrCount
Expand Down Expand Up @@ -1304,27 +1296,17 @@ func (t *target) DeleteObject(lom *core.LOM, evict bool) (code int, err error) {
vlabs := map[string]string{stats.VarlabBucket: lom.Bck().Cname("")}
switch {
case err == nil:
t.statsT.AddWith(
cos.NamedVal64{Name: stats.DeleteCount, Value: 1, VarLabs: vlabs},
)
t.statsT.IncWith(stats.DeleteCount, vlabs)
case cos.IsNotExist(err, code) || cmn.IsErrObjNought(err):
if !evict {
t.statsT.AddWith(
cos.NamedVal64{Name: stats.ErrDeleteCount, Value: 1, VarLabs: vlabs},
)
t.statsT.IncWith(stats.ErrDeleteCount, vlabs)
}
default:
// not to confuse with `stats.RemoteDeletedDelCount` that counts against
// QparamLatestVer, 'versioning.validate_warm_get' and friends
t.statsT.IncWith(stats.ErrDeleteCount, vlabs)
if !isback {
t.statsT.AddWith(
cos.NamedVal64{Name: stats.ErrDeleteCount, Value: 1, VarLabs: vlabs},
cos.NamedVal64{Name: stats.IOErrDeleteCount, Value: 1, VarLabs: vlabs},
)
} else {
t.statsT.AddWith(
cos.NamedVal64{Name: stats.ErrDeleteCount, Value: 1, VarLabs: vlabs},
)
t.statsT.IncWith(stats.IOErrDeleteCount, vlabs)
}
}
return code, err
Expand Down
2 changes: 1 addition & 1 deletion ais/tgtbck.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ func (t *target) httpbckget(w http.ResponseWriter, r *http.Request, dpq *dpq) {

delta := mono.SinceNano(begin)
vlabs := map[string]string{stats.VarlabBucket: bck.Cname("")}
t.statsT.IncWith(stats.ListCount, vlabs)
t.statsT.AddWith(
cos.NamedVal64{Name: stats.ListCount, Value: 1, VarLabs: vlabs},
cos.NamedVal64{Name: stats.ListLatency, Value: delta, VarLabs: vlabs},
)
case apc.ActSummaryBck:
Expand Down
4 changes: 2 additions & 2 deletions ais/tgtimpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ func (t *target) GetCold(ctx context.Context, lom *core.LOM, owt cmn.OWT) (ecode

func (t *target) coldstats(backend core.Backend, lom *core.LOM, started int64) {
vlabs := map[string]string{stats.VarlabBucket: lom.Bck().Cname("")}
t.statsT.IncWith(backend.MetricName(stats.GetCount), vlabs)
t.statsT.AddWith(
cos.NamedVal64{Name: backend.MetricName(stats.GetCount), Value: 1, VarLabs: vlabs},
cos.NamedVal64{Name: backend.MetricName(stats.GetLatencyTotal), Value: mono.SinceNano(started), VarLabs: vlabs},
cos.NamedVal64{Name: backend.MetricName(stats.GetSize), Value: lom.Lsize(), VarLabs: vlabs},
)
Expand All @@ -215,8 +215,8 @@ func (t *target) HeadCold(lom *core.LOM, origReq *http.Request) (oa *cmn.ObjAttr
if err != nil {
t.statsT.IncWith(stats.ErrHeadCount, vlabs)
} else {
t.statsT.IncWith(backend.MetricName(stats.HeadCount), vlabs)
t.statsT.AddWith(
cos.NamedVal64{Name: backend.MetricName(stats.HeadCount), Value: 1, VarLabs: vlabs},
cos.NamedVal64{Name: backend.MetricName(stats.HeadLatencyTotal), Value: mono.SinceNano(now), VarLabs: vlabs},
)
}
Expand Down
32 changes: 12 additions & 20 deletions ais/tgtobj.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,19 +235,13 @@ func (poi *putOI) putObject() (ecode int, err error) {
rerr:
if poi.owt == cmn.OwtPut && poi.restful && !poi.t2t {
vlabs := poi._vlabs()
if err != cmn.ErrSkip && !poi.remoteErr && err != io.ErrUnexpectedEOF &&
!cos.IsRetriableConnErr(err) && !cos.IsErrMv(err) {
poi.t.statsT.AddWith(
cos.NamedVal64{Name: stats.ErrPutCount, Value: 1, VarLabs: vlabs},
cos.NamedVal64{Name: stats.IOErrPutCount, Value: 1, VarLabs: vlabs},
)
poi.t.statsT.IncWith(stats.ErrPutCount, vlabs)

if err != cmn.ErrSkip && !poi.remoteErr && err != io.ErrUnexpectedEOF && !cos.IsRetriableConnErr(err) && !cos.IsErrMv(err) {
poi.t.statsT.IncWith(stats.IOErrPutCount, vlabs)
if cmn.Rom.FastV(4, cos.SmoduleAIS) {
nlog.Warningln("io-error [", err, "]", poi.loghdr())
}
} else {
poi.t.statsT.AddWith(
cos.NamedVal64{Name: stats.ErrPutCount, Value: 1, VarLabs: vlabs},
)
}
}
return ecode, err
Expand All @@ -269,8 +263,8 @@ func (poi *putOI) stats() {
delta = mono.SinceNano(poi.ltime)
vlabs = poi._vlabs()
)
poi.t.statsT.IncWith(stats.PutCount, vlabs)
poi.t.statsT.AddWith(
cos.NamedVal64{Name: stats.PutCount, Value: 1, VarLabs: vlabs},
cos.NamedVal64{Name: stats.PutSize, Value: size, VarLabs: vlabs},
cos.NamedVal64{Name: stats.PutThroughput, Value: size, VarLabs: vlabs},
cos.NamedVal64{Name: stats.PutLatency, Value: delta, VarLabs: vlabs},
Expand All @@ -279,8 +273,8 @@ func (poi *putOI) stats() {
if poi.rltime > 0 {
debug.Assert(bck.IsRemote())
backend := poi.t.Backend(bck)
poi.t.statsT.IncWith(backend.MetricName(stats.PutCount), vlabs)
poi.t.statsT.AddWith(
cos.NamedVal64{Name: backend.MetricName(stats.PutCount), Value: 1, VarLabs: vlabs},
cos.NamedVal64{Name: backend.MetricName(stats.PutLatencyTotal), Value: poi.rltime, VarLabs: vlabs},
cos.NamedVal64{Name: backend.MetricName(stats.PutE2ELatencyTotal), Value: delta, VarLabs: vlabs},
cos.NamedVal64{Name: backend.MetricName(stats.PutSize), Value: size, VarLabs: vlabs},
Expand Down Expand Up @@ -507,9 +501,7 @@ func (poi *putOI) write() (buf []byte, slab *memsys.Slab, lmfh cos.LomWriter, er
cksums.compt.Finalize()
if !cksums.compt.Equal(cksums.expct) {
err = cos.NewErrDataCksum(cksums.expct, &cksums.compt.Cksum, poi.lom.Cname())
poi.t.statsT.AddWith(
cos.NamedVal64{Name: stats.ErrPutCksumCount, Value: 1, VarLabs: poi._vlabs()},
)
poi.t.statsT.IncWith(stats.ErrPutCksumCount, poi._vlabs())
return
}
}
Expand Down Expand Up @@ -1215,32 +1207,32 @@ func (goi *getOI) transmit(r io.Reader, buf []byte, fqn string) error {
func (goi *getOI) stats(written int64) {
vlabs := map[string]string{stats.VarlabBucket: goi.lom.Bck().Cname("")}
delta := mono.SinceNano(goi.ltime)
goi.t.statsT.IncWith(stats.GetCount, vlabs)
goi.t.statsT.AddWith(
cos.NamedVal64{Name: stats.GetCount, Value: 1, VarLabs: vlabs},
cos.NamedVal64{Name: stats.GetSize, Value: written, VarLabs: vlabs},
cos.NamedVal64{Name: stats.GetThroughput, Value: written, VarLabs: vlabs}, // vis-à-vis user (as written m.b. range)
cos.NamedVal64{Name: stats.GetLatency, Value: delta, VarLabs: vlabs}, // see also: per-backend *LatencyTotal below
cos.NamedVal64{Name: stats.GetLatencyTotal, Value: delta, VarLabs: vlabs}, // ditto
)
if goi.verchanged {
goi.t.statsT.IncWith(stats.VerChangeCount, vlabs)
goi.t.statsT.AddWith(
cos.NamedVal64{Name: stats.VerChangeCount, Value: 1, VarLabs: vlabs},
cos.NamedVal64{Name: stats.VerChangeSize, Value: goi.lom.Lsize(), VarLabs: vlabs},
)
}

if goi.rltime > 0 {
bck := goi.lom.Bck()
backend := goi.t.Backend(bck)
goi.t.statsT.IncWith(backend.MetricName(stats.GetCount), vlabs)
goi.t.statsT.AddWith(
cos.NamedVal64{Name: backend.MetricName(stats.GetCount), Value: 1, VarLabs: vlabs},
cos.NamedVal64{Name: backend.MetricName(stats.GetE2ELatencyTotal), Value: delta, VarLabs: vlabs},
cos.NamedVal64{Name: backend.MetricName(stats.GetLatencyTotal), Value: goi.rltime, VarLabs: vlabs},
cos.NamedVal64{Name: backend.MetricName(stats.GetSize), Value: written, VarLabs: vlabs},
)
if goi.verchanged {
goi.t.statsT.IncWith(backend.MetricName(stats.VerChangeCount), vlabs)
goi.t.statsT.AddWith(
cos.NamedVal64{Name: backend.MetricName(stats.VerChangeCount), Value: 1, VarLabs: vlabs},
cos.NamedVal64{Name: backend.MetricName(stats.VerChangeSize), Value: goi.lom.Lsize(), VarLabs: vlabs},
)
}
Expand Down Expand Up @@ -1360,8 +1352,8 @@ func (a *apndOI) apnd(buf []byte) (packedHdl string, ecode int, err error) {
// stats (TODO: add `stats.FlushCount` for symmetry)
lat := time.Now().UnixNano() - a.started
vlabs := map[string]string{stats.VarlabBucket: a.lom.Bck().Cname("")}
a.t.statsT.IncWith(stats.AppendCount, vlabs)
a.t.statsT.AddWith(
cos.NamedVal64{Name: stats.AppendCount, Value: 1, VarLabs: vlabs},
cos.NamedVal64{Name: stats.AppendLatency, Value: lat, VarLabs: vlabs},
)
if cmn.Rom.FastV(4, cos.SmoduleAIS) {
Expand Down
2 changes: 1 addition & 1 deletion ais/tgts3mpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,8 +549,8 @@ func (t *target) getMptPart(w http.ResponseWriter, r *http.Request, bck *meta.Bc
slab.Free(buf)

vlabs := map[string]string{stats.VarlabBucket: bck.Cname("")}
t.statsT.IncWith(stats.GetCount, vlabs)
t.statsT.AddWith(
cos.NamedVal64{Name: stats.GetCount, Value: 1, VarLabs: vlabs},
cos.NamedVal64{Name: stats.GetSize, Value: size, VarLabs: vlabs},
cos.NamedVal64{Name: stats.GetLatencyTotal, Value: mono.SinceNano(startTime), VarLabs: vlabs},
)
Expand Down
1 change: 1 addition & 0 deletions cmn/cos/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type (
SetClrFlag(name string, set, clr NodeStateFlags)
Get(name string) int64
AddWith(namedVal64 ...NamedVal64)
IncWith(name string, VarLabs map[string]string)
}
NamedVal64 struct {
Name string
Expand Down
2 changes: 1 addition & 1 deletion core/ldp.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (lom *LOM) CheckRemoteMD(locked, sync bool, origReq *http.Request) (res CRM
ecode, err = 0, errDel
} else {
vlabs := map[string]string{"bucket": lom.Bck().Cname("")} // TODO -- FIXME: cannot import stats
g.tstats.AddWith(cos.NamedVal64{Name: RemoteDeletedDelCount, Value: 1, VarLabs: vlabs})
g.tstats.IncWith(RemoteDeletedDelCount, vlabs)
}
debug.Assert(err != nil)
return CRMD{ErrCode: ecode, Err: err}
Expand Down
6 changes: 3 additions & 3 deletions stats/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ func (r *runner) regCommon(snode *meta.Snode) {
// as cos.StatsUpdater
//

func (r *runner) Inc(name string) { r.core.add(name, 1) }
func (r *runner) Inc(name string) { r.core.inc(name) }
func (r *runner) Add(name string, val int64) { r.core.add(name, val) }

// (prometheus with variable labels)
Expand All @@ -330,12 +330,12 @@ func (r *runner) AddWith(nvs ...cos.NamedVal64) {

// (ditto; for convenience)
func (r *runner) IncWith(name string, vlabs map[string]string) {
r.AddWith(cos.NamedVal64{Name: name, Value: 1, VarLabs: vlabs})
r.core.incWith(cos.NamedVal64{Name: name, Value: 1, VarLabs: vlabs})
}

// (ditto)
func (r *runner) IncBck(name string, bck *cmn.Bck) {
r.AddWith(cos.NamedVal64{Name: name, Value: 1, VarLabs: map[string]string{VarlabBucket: bck.Cname("")}})
r.IncWith(name, map[string]string{VarlabBucket: bck.Cname("")})
}

func (r *runner) SetFlag(name string, set cos.NodeStateFlags) {
Expand Down
3 changes: 3 additions & 0 deletions stats/common_statsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ func (s *coreStats) initStarted(snode *meta.Snode) {
// compare w/ prometheus
func (s *coreStats) addWith(nv cos.NamedVal64) { s.add(nv.Name, nv.Value) }

func (s *coreStats) inc(name string) { s.add(name, 1) } // (for the sake of Prometheus optimization)
func (s *coreStats) incWith(nv cos.NamedVal64) { s.add(nv.Name, 1) } // ditto

func (s *coreStats) add(name string, val int64) {
v, ok := s.Tracker[name]
debug.Assertf(ok, "invalid metric name %q", name)
Expand Down
Loading

0 comments on commit 6514aa1

Please sign in to comment.