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 4, 2019
1 parent 777292a commit 579a86e
Show file tree
Hide file tree
Showing 4 changed files with 78 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
52 changes: 52 additions & 0 deletions test/api_system_metrics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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{})
}

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

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

// TearDownSuite run after each suite to do cleanup work for the whole suite.
func (suite *APIEngineVersionMetricsSuite) TearDownSuite(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)
regularStr := `^.*commit="(.*)",version="(.*)",kernel="(.*)".*$`
regular := regexp.MustCompile(regularStr)
params := regular.FindStringSubmatch(versionMetrics)
c.Assert(params[1], check.Equals, commitExpected)
c.Assert(params[2], check.Equals, versionExpected)
c.Assert(params[3], 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 579a86e

Please sign in to comment.