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

export edition and version as build_info #330

Merged
merged 1 commit into from
Mar 13, 2019
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
4 changes: 2 additions & 2 deletions cmd/acra-connector/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ func registerMetrics() {
registerLock.Do(func() {
prometheus.MustRegister(connectionCounter)
prometheus.MustRegister(connectionProcessingTimeHistogram)
cmd.RegisterVersionMetrics(ServiceName)
version, err := utils.GetParsedVersion()
if err != nil {
panic(err)
}
cmd.ExportVersionMetric(version)
cmd.RegisterVersionMetrics(ServiceName, version)
cmd.RegisterBuildInfoMetrics(ServiceName, utils.CommunityEdition)
})
}
4 changes: 2 additions & 2 deletions cmd/acra-server/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ func registerMetrics() {
prometheus.MustRegister(connectionProcessingTimeHistogram)
base.RegisterAcraStructProcessingMetrics()
base.RegisterDbProcessingMetrics()
cmd.RegisterVersionMetrics(ServiceName)
version, err := utils.GetParsedVersion()
if err != nil {
panic(err)
}
cmd.ExportVersionMetric(version)
cmd.RegisterVersionMetrics(ServiceName, version)
cmd.RegisterBuildInfoMetrics(ServiceName, utils.CommunityEdition)
})
}
4 changes: 2 additions & 2 deletions cmd/acra-translator/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ func registerMetrics() {
prometheus.MustRegister(connectionProcessingTimeHistogram)
prometheus.MustRegister(common.RequestProcessingTimeHistogram)
base.RegisterAcraStructProcessingMetrics()
cmd.RegisterVersionMetrics(ServiceName)
version, err := utils.GetParsedVersion()
if err != nil {
panic(err)
}
cmd.ExportVersionMetric(version)
cmd.RegisterVersionMetrics(ServiceName, version)
cmd.RegisterBuildInfoMetrics(ServiceName, utils.CommunityEdition)
})
}
50 changes: 44 additions & 6 deletions cmd/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,26 @@ func RunPrometheusHTTPHandler(connectionString string) (net.Listener, *http.Serv
return listener, server, nil
}

var registerLock = sync.Once{}

// serviceNameToLabelFormat convert service name to lower case and remove all '_'
// ex. Acra-Server will be changed to acraserver
func serviceNameToLabelFormat(serviceName string) string {
const replaceAll = -1
return strings.ToLower(strings.Replace(serviceName, "-", "", replaceAll))
}

// ExportVersionMetric set values for version metrics
func ExportVersionMetric(version *utils.Version) {
// editionToLabel convert edition value to string to use as label
func editionToLabel(edition utils.ProductEdition) string {
switch edition {
case utils.CommunityEdition:
return "ce"
case utils.EnterpriseEdition:
return "ee"
}
panic(fmt.Sprintf("undefined edition: %v", edition))
}

// exportVersionMetric set values for version metrics
func exportVersionMetric(version *utils.Version) {
version, err := utils.GetParsedVersion()
if err != nil {
panic(err)
Expand All @@ -83,10 +92,18 @@ var (
majorVersionGauge *prometheus.GaugeVec
minorVersionGauge *prometheus.GaugeVec
patchVersionGauge *prometheus.GaugeVec
buildInfoCounter *prometheus.CounterVec
)

const (
BuildInfoEditionLabel = "edition"
BuildInfoVersionLabel = "version"
)

var registerVersionMetricsLock = sync.Once{}

// RegisterVersionMetrics set and register metrics with current version value
func RegisterVersionMetrics(serviceName string) {
func RegisterVersionMetrics(serviceName string, version *utils.Version) {
labelServiceName := serviceNameToLabelFormat(serviceName)
majorVersionGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Expand All @@ -106,9 +123,30 @@ func RegisterVersionMetrics(serviceName string) {
Help: "Patch number of version",
}, []string{})

registerLock.Do(func() {
registerVersionMetricsLock.Do(func() {
prometheus.MustRegister(majorVersionGauge)
prometheus.MustRegister(minorVersionGauge)
prometheus.MustRegister(patchVersionGauge)
exportVersionMetric(version)
})
}

var registerBuildInfoLock = sync.Once{}

// RegisterBuildInfoMetrics set and register metrics with build info
func RegisterBuildInfoMetrics(serviceName string, edition utils.ProductEdition) {
buildInfoCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: fmt.Sprintf("%s_build_info", serviceNameToLabelFormat(serviceName)),
}, []string{BuildInfoEditionLabel, BuildInfoVersionLabel})

registerBuildInfoLock.Do(func() {
prometheus.MustRegister(buildInfoCounter)
version, err := utils.GetParsedVersion()
if err != nil {
panic(err)
}
// increment on start only once
buildInfoCounter.With(prometheus.Labels{BuildInfoEditionLabel: editionToLabel(edition), BuildInfoVersionLabel: version.String()}).Inc()
})
}
19 changes: 17 additions & 2 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3485,8 +3485,17 @@ def skip(need_skip):
if skip(family.name):
continue
for sample in family.samples:
self.assertGreaterEqual(sample.value, labels[sample.name]['min_value'],
'{} - {}'.format(sample.name, sample.value))
try:
self.assertGreaterEqual(sample.value, labels[sample.name]['min_value'],
'{} - {}'.format(sample.name, sample.value))
except KeyError:
# python prometheus client append _total for sample names if they have type <counter> and
# have not _total suffix
if not sample.name.endswith('_total'):
raise
name = sample.name[:-len('_total')]
self.assertGreaterEqual(sample.value, labels[name]['min_value'],
'{} - {}'.format(name, sample.value))

def testAcraServer(self):
# run some queries to set some values for counters
Expand All @@ -3512,6 +3521,8 @@ def testAcraServer(self):
'acraserver_version_major': {'min_value': 0},
'acraserver_version_minor': {'min_value': 0},
'acraserver_version_patch': {'min_value': 0},

'acraserver_build_info': {'min_value': 1},
}
self.checkMetrics('http://127.0.0.1:{}/metrics'.format(
self.ACRASERVER_PROMETHEUS_PORT), labels)
Expand All @@ -3529,6 +3540,8 @@ def testAcraConnector(self):
'acraconnector_version_major': {'min_value': 0},
'acraconnector_version_minor': {'min_value': 0},
'acraconnector_version_patch': {'min_value': 0},

'acraconnector_build_info': {'min_value': 1},
}
self.checkMetrics('http://127.0.0.1:{}/metrics'.format(
self.get_connector_prometheus_port(self.CONNECTOR_PORT_1)), labels)
Expand All @@ -3550,6 +3563,8 @@ def testAcraTranslator(self):
'acratranslator_version_patch': {'min_value': 0},

'acra_acrastruct_decryptions_total': {'min_value': 1},

'acratranslator_build_info': {'min_value': 1},
}
translator_port = 3456
metrics_port = translator_port+1
Expand Down
10 changes: 10 additions & 0 deletions utils/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ type Version struct {
Patch string
}

// ProductEdition type for edition values
type ProductEdition int

const (
CommunityEdition = iota
EnterpriseEdition
)

var Edition ProductEdition = CommunityEdition

// ComparisonStatus result of comparison versions
type ComparisonStatus int

Expand Down