Skip to content

Commit

Permalink
Merge pull request #10257 from gyuho/cluster-version
Browse files Browse the repository at this point in the history
etcdserver/*: add "etcd_cluster_version" metric
  • Loading branch information
xiang90 authored Nov 15, 2018
2 parents 322339b + 0226481 commit 024f3df
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 0 deletions.
2 changes: 2 additions & 0 deletions etcdserver/api/membership/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"go.etcd.io/etcd/version"

"github.com/coreos/go-semver/semver"
"github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -500,6 +501,7 @@ func (c *RaftCluster) SetVersion(ver *semver.Version, onSet func(*zap.Logger, *s
if c.be != nil {
mustSaveClusterVersionToBackend(c.be, ver)
}
ClusterVersionMetrics.With(prometheus.Labels{"cluster_version": ver.String()}).Set(1)
onSet(c.lg, ver)
}

Expand Down
31 changes: 31 additions & 0 deletions etcdserver/api/membership/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2018 The etcd Authors
//
// 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 membership

import "github.com/prometheus/client_golang/prometheus"

var (
ClusterVersionMetrics = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "etcd",
Subsystem: "cluster",
Name: "version",
Help: "Which version is running. 1 for 'cluster_version' label with current cluster version",
},
[]string{"cluster_version"})
)

func init() {
prometheus.MustRegister(ClusterVersionMetrics)
}
1 change: 1 addition & 0 deletions etcdserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,7 @@ func (s *EtcdServer) start() {
} else {
plog.Infof("starting server... [version: %v, cluster version: %v]", version.Version, version.Cluster(s.ClusterVersion().String()))
}
membership.ClusterVersionMetrics.With(prometheus.Labels{"cluster_version": s.ClusterVersion().String()}).Set(1)
} else {
if lg != nil {
lg.Info(
Expand Down
10 changes: 10 additions & 0 deletions tests/e2e/etcd_release_upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package e2e
import (
"fmt"
"os"
"strings"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -101,6 +102,15 @@ func TestReleaseUpgrade(t *testing.T) {
}
}
}

// expect upgraded cluster version
ver := version.Version
if strings.HasSuffix(ver, "+git") {
ver = strings.Replace(ver, "+git", "", 1)
}
if err := cURLGet(cx.epc, cURLReq{endpoint: "/metrics", expected: fmt.Sprintf(`etcd_cluster_version{cluster_version="%s"} 1`, ver), metricsURLScheme: cx.cfg.metricsURLScheme}); err != nil {
cx.t.Fatalf("failed get with curl (%v)", err)
}
}

func TestReleaseUpgradeWithRestart(t *testing.T) {
Expand Down
8 changes: 8 additions & 0 deletions tests/e2e/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package e2e

import (
"fmt"
"strings"
"testing"

"go.etcd.io/etcd/version"
Expand Down Expand Up @@ -45,6 +46,13 @@ func metricsTest(cx ctlCtx) {
if err := cURLGet(cx.epc, cURLReq{endpoint: "/metrics", expected: fmt.Sprintf(`etcd_server_version{server_version="%s"} 1`, version.Version), metricsURLScheme: cx.cfg.metricsURLScheme}); err != nil {
cx.t.Fatalf("failed get with curl (%v)", err)
}
ver := version.Version
if strings.HasSuffix(ver, "+git") {
ver = strings.Replace(ver, "+git", "", 1)
}
if err := cURLGet(cx.epc, cURLReq{endpoint: "/metrics", expected: fmt.Sprintf(`etcd_cluster_version{cluster_version="%s"} 1`, ver), metricsURLScheme: cx.cfg.metricsURLScheme}); err != nil {
cx.t.Fatalf("failed get with curl (%v)", err)
}
if err := cURLGet(cx.epc, cURLReq{endpoint: "/health", expected: `{"health":"true"}`, metricsURLScheme: cx.cfg.metricsURLScheme}); err != nil {
cx.t.Fatalf("failed get with curl (%v)", err)
}
Expand Down

0 comments on commit 024f3df

Please sign in to comment.