Skip to content

Commit

Permalink
feature: add kernel info to EngineVersion metric
Browse files Browse the repository at this point in the history
Signed-off-by: KevinBetterQ <1093850932@qq.com>
  • Loading branch information
KevinBetterQ committed Jun 5, 2019
1 parent 777292a commit 4052bf8
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 2 deletions.
2 changes: 1 addition & 1 deletion apis/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var (
ImageActionsTimer = metrics.NewLabelTimer(subsystemPouch, "image_actions", "The number of seconds it takes to process each image action", "action")

// EngineVersion records the version and commit information of the engine process.
EngineVersion = metrics.NewLabelGauge(subsystemPouch, "engine", "The version and commit information of the engine process", "commit")
EngineVersion = metrics.NewLabelGauge(subsystemPouch, "engine", "The version and commit information of the engine process", "commit", "version", "kernel")
)

var registerMetrics sync.Once
Expand Down
11 changes: 10 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/alibaba/pouch/daemon/config"
"github.com/alibaba/pouch/lxcfs"
"github.com/alibaba/pouch/pkg/debug"
"github.com/alibaba/pouch/pkg/kernel"
"github.com/alibaba/pouch/pkg/utils"
"github.com/alibaba/pouch/storage/quota"
"github.com/alibaba/pouch/version"
Expand Down Expand Up @@ -167,7 +168,15 @@ func runDaemon(cmd *cobra.Command) error {
fmt.Printf("pouchd version: %s, build: %s, build at: %s\n", version.Version, version.GitCommit, version.BuildTime)
return nil
}
metrics.EngineVersion.WithLabelValues(version.GitCommit).Set(1)

kernelVersion, err := kernel.GetKernelVersion()
if err != nil {
return fmt.Errorf("failed to get kernel version: %s", err)
}
metrics.EngineVersion.WithLabelValues(
version.GitCommit,
version.Version,
kernelVersion.String()).Set(1)
// initialize log.
initLog()

Expand Down
51 changes: 51 additions & 0 deletions test/api_system_metrics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package main

import (
"regexp"

"github.com/alibaba/pouch/pkg/kernel"
"github.com/alibaba/pouch/test/environment"
"github.com/alibaba/pouch/version"

"github.com/go-check/check"
)

// APIEngineVersionMetricsSuite is the test suite for EngineVersion metrics API.
type APIEngineVersionMetricsSuite struct{}

func init() {
check.Suite(&APIEngineVersionMetricsSuite{})
}

// SetUpSuite does common setup in the beginning of each suite.
func (suite *APIEngineVersionMetricsSuite) SetUpSuite(c *check.C) {
SkipIfFalse(c, environment.IsLinux)
}

// TestEngineVersionMetrics tests metrics of EngineVersion.
func (suite *APIEngineVersionMetricsSuite) TestEngineVersionMetrics(c *check.C) {
commitExpected := version.GitCommit
versionExpected := version.Version
kernelVersion, err := kernel.GetKernelVersion()
c.Assert(err, check.IsNil)
kernelExpected := kernelVersion.String()

key := "engine_daemon_engine_info{"
versionMetrics := GetMetricLine(c, key)

regularCommit := `^.*commit="(.*?)".*$`
regular := regexp.MustCompile(regularCommit)
params := regular.FindStringSubmatch(versionMetrics)
c.Assert(params[1], check.Equals, commitExpected)

regularVersion := `^.*version="(.*?)".*$`
regular = regexp.MustCompile(regularVersion)
params = regular.FindStringSubmatch(versionMetrics)
c.Assert(params[1], check.Equals, versionExpected)

regularKernel := `^.*kernel="(.*?)".*$`
regular = regexp.MustCompile(regularKernel)
params = regular.FindStringSubmatch(versionMetrics)
c.Assert(params[1], check.Equals, kernelExpected)

}
15 changes: 15 additions & 0 deletions test/util_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,3 +345,18 @@ func GetMetric(c *check.C, key string, keySuccess string) (int, int) {

return iCount, iCountSuccess
}

// GetMetricLine get metrics from prometheus server, return the single metric.
func GetMetricLine(c *check.C, key string) string {
resp, err := request.Get("/metrics")
c.Assert(err, check.IsNil)
defer resp.Body.Close()
scanner := bufio.NewScanner(resp.Body)
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, key) {
return line
}
}
return ""
}

0 comments on commit 4052bf8

Please sign in to comment.