Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add kernel info to EngineVersion metric #2889

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(
ZYecho marked this conversation as resolved.
Show resolved Hide resolved
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we should check the length here.

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 ""
}