Skip to content

Commit

Permalink
snap: add save latency metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
xiang90 authored and yichengq committed Feb 28, 2015
1 parent 95bba15 commit c3d3ad9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
30 changes: 30 additions & 0 deletions snap/metrics.go
@@ -0,0 +1,30 @@
// Copyright 2015 CoreOS, Inc.
//
// 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 snap

import "github.com/coreos/etcd/Godeps/_workspace/src/github.com/prometheus/client_golang/prometheus"

var (
// TODO: save_fsync latency?
// TODO: save_encoding latency?
saveDurations = prometheus.NewSummary(prometheus.SummaryOpts{
Name: "snapshot_save_total_durations_microseconds",
Help: "The total latency distributions of save called by snapshot.",
})
)

func init() {
prometheus.MustRegister(saveDurations)
}
9 changes: 8 additions & 1 deletion snap/snapshotter.go
Expand Up @@ -24,6 +24,7 @@ import (
"path"
"sort"
"strings"
"time"

"github.com/coreos/etcd/pkg/pbutil"
"github.com/coreos/etcd/raft"
Expand Down Expand Up @@ -60,6 +61,8 @@ func (s *Snapshotter) SaveSnap(snapshot raftpb.Snapshot) error {
}

func (s *Snapshotter) save(snapshot *raftpb.Snapshot) error {
start := time.Now()

fname := fmt.Sprintf("%016x-%016x%s", snapshot.Metadata.Term, snapshot.Metadata.Index, snapSuffix)
b := pbutil.MustMarshal(snapshot)
crc := crc32.Update(0, crcTable, b)
Expand All @@ -68,7 +71,11 @@ func (s *Snapshotter) save(snapshot *raftpb.Snapshot) error {
if err != nil {
return err
}
return ioutil.WriteFile(path.Join(s.dir, fname), d, 0666)
err = ioutil.WriteFile(path.Join(s.dir, fname), d, 0666)
if err != nil {
saveDurations.Observe(float64(time.Since(start).Nanoseconds() / int64(time.Microsecond)))
}
return err
}

func (s *Snapshotter) Load() (*raftpb.Snapshot, error) {
Expand Down

0 comments on commit c3d3ad9

Please sign in to comment.