-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from Financial-Times/deploy-on-push/k8s/promet…
…heus-metrics Prometheus metrics
- Loading branch information
Showing
9 changed files
with
254 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
helm/upp-aggregate-healthcheck/templates/service-monitor.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
apiVersion: monitoring.coreos.com/v1 | ||
kind: ServiceMonitor | ||
metadata: | ||
labels: | ||
component: {{ .Values.service.name }} | ||
prometheus: {{ .Values.metrics.prometheusInstance }} | ||
name: {{ .Values.service.name }} | ||
spec: | ||
endpoints: | ||
- interval: {{ .Values.metrics.interval }} | ||
path: {{ .Values.metrics.path }} | ||
jobLabel: component | ||
selector: | ||
matchLabels: | ||
app: {{ .Values.service.name }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package main | ||
|
||
import ( | ||
"strings" | ||
"time" | ||
|
||
prom "github.com/prometheus/client_golang/prometheus" | ||
) | ||
|
||
type prometheusFeeder struct { | ||
environment string | ||
ticker *time.Ticker | ||
controller controller | ||
} | ||
|
||
func newPrometheusFeeder(environment string, controller controller) *prometheusFeeder { | ||
ticker := time.NewTicker(60 * time.Second) | ||
return &prometheusFeeder{ | ||
environment: environment, | ||
ticker: ticker, | ||
controller: controller, | ||
} | ||
} | ||
|
||
func (p prometheusFeeder) feed() { | ||
ignitePilotLight(p.environment) | ||
serviceStatus := initServiceStatusMetrics() | ||
|
||
for range p.ticker.C { | ||
p.recordMetrics(serviceStatus) | ||
} | ||
} | ||
|
||
func (p prometheusFeeder) recordMetrics(serviceStatus *prom.GaugeVec) { | ||
for _, service := range p.controller.getMeasuredServices() { | ||
select { | ||
case checkResult := <-service.bufferedMetrics.buffer: | ||
name := strings.Replace(checkResult.Name, ".", "-", -1) | ||
checkStatus := inverseBoolToFloat64(checkResult.Ok) | ||
serviceStatus. | ||
With(prom.Labels{"environment": p.environment, "service": name}). | ||
Set(checkStatus) | ||
default: | ||
continue | ||
} | ||
} | ||
} | ||
|
||
func initServiceStatusMetrics() *prom.GaugeVec { | ||
serviceStatus := prom.NewGaugeVec( | ||
prom.GaugeOpts{ | ||
Namespace: "upp", | ||
Subsystem: "health", | ||
Name: "servicestatus", | ||
Help: "Status of the service: 0 - healthy; 1 - unhealthy", | ||
}, | ||
[]string{ | ||
"environment", | ||
"service", | ||
}) | ||
prom.MustRegister(serviceStatus) | ||
return serviceStatus | ||
} | ||
|
||
func ignitePilotLight(environment string) { | ||
pilotLight := prom.NewGaugeVec( | ||
prom.GaugeOpts{ | ||
Namespace: "upp", | ||
Subsystem: "health", | ||
Name: "pilotlight", | ||
Help: "Pilot light for the service monitoring UPP service health", | ||
}, | ||
[]string{ | ||
"environment", | ||
}) | ||
prom.MustRegister(pilotLight) | ||
pilotLight.With(prom.Labels{"environment": environment}).Set(1) | ||
} | ||
|
||
func inverseBoolToFloat64(b bool) float64 { | ||
if b { | ||
return 0 | ||
} | ||
return 1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
|
||
prom "github.com/prometheus/client_golang/prometheus" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
const ENV = "foobar" | ||
|
||
func TestNewPrometheusFeeder(t *testing.T) { | ||
feeder := newPrometheusFeeder(ENV, &healthCheckController{}) | ||
assert.NotNil(t, feeder) | ||
assert.Equal(t, ENV, feeder.environment, "Environment should be set correctly.") | ||
} | ||
|
||
func TestInitServiceStatusMetrics(t *testing.T) { | ||
gauge := initServiceStatusMetrics() | ||
assert.NotNil(t, gauge) | ||
duplicateGauge := prom.NewGaugeVec( | ||
prom.GaugeOpts{ | ||
Namespace: "upp", | ||
Subsystem: "health", | ||
Name: "servicestatus", | ||
Help: "Status of the service: 0 - healthy; 1 - unhealthy", | ||
}, | ||
[]string{ | ||
"environment", | ||
"service", | ||
}) | ||
|
||
err := prom.Register(duplicateGauge) | ||
assert.NotNil(t, err, "Gauge should've been registered already.") | ||
_, ok := err.(prom.AlreadyRegisteredError) | ||
assert.True(t, ok, "Expecting an 'AlreadyRegisteredError'.") | ||
} | ||
func TestIgnitePilotLight(t *testing.T) { | ||
ignitePilotLight(ENV) | ||
duplicateGauge := prom.NewGaugeVec( | ||
prom.GaugeOpts{ | ||
Namespace: "upp", | ||
Subsystem: "health", | ||
Name: "pilotlight", | ||
Help: "Pilot light for the service monitoring UPP service health", | ||
}, | ||
[]string{ | ||
"environment", | ||
}) | ||
|
||
err := prom.Register(duplicateGauge) | ||
assert.NotNil(t, err, "Gauge should've been registered already.") | ||
_, ok := err.(prom.AlreadyRegisteredError) | ||
assert.True(t, ok, "Expecting an 'AlreadyRegisteredError'.") | ||
} | ||
|
||
func TestInverseBoolToFloat64(t *testing.T) { | ||
one := inverseBoolToFloat64(false) | ||
assert.Equal(t, float64(1), one) | ||
|
||
zero := inverseBoolToFloat64(true) | ||
assert.Equal(t, float64(0), zero) | ||
} |