Skip to content

Commit

Permalink
Expose prometheus metrics for BotKube
Browse files Browse the repository at this point in the history
  • Loading branch information
Surbhidongaonkar committed Nov 29, 2019
1 parent 638137a commit 5c5acb7
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 7 deletions.
13 changes: 13 additions & 0 deletions cmd/botkube/main.go
Expand Up @@ -2,15 +2,21 @@ package main

import (
"fmt"
"os"

"github.com/infracloudio/botkube/pkg/bot"
"github.com/infracloudio/botkube/pkg/config"
"github.com/infracloudio/botkube/pkg/controller"
log "github.com/infracloudio/botkube/pkg/logging"
"github.com/infracloudio/botkube/pkg/metrics"
"github.com/infracloudio/botkube/pkg/notify"
"github.com/infracloudio/botkube/pkg/utils"
)

const (
defaultMetricsPort = "2112"
)

func main() {
log.Logger.Info("Starting controller")
Config, err := config.New()
Expand All @@ -30,6 +36,13 @@ func main() {
go mb.Start()
}

// Prometheus metrics
metricsPort, exists := os.LookupEnv("METRICS_PORT")
if !exists {
metricsPort = defaultMetricsPort
}
go metrics.ServeMetrics(metricsPort)

// List notifiers
var notifiers []notify.Notifier
if Config.Communications.Slack.Enabled {
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Expand Up @@ -31,7 +31,7 @@ require (
github.com/onsi/ginkgo v1.10.2 // indirect
github.com/pborman/uuid v1.2.0 // indirect
github.com/pelletier/go-toml v1.5.0 // indirect
github.com/pkg/errors v0.8.1 // indirect
github.com/prometheus/client_golang v1.2.1
github.com/sirupsen/logrus v1.4.2
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.2.0 // indirect
Expand All @@ -42,7 +42,6 @@ require (
go.uber.org/zap v1.10.0 // indirect
golang.org/x/crypto v0.0.0-20191002192127-34f69633bfdc // indirect
golang.org/x/net v0.0.0-20191009170851-d66e71096ffb // indirect
golang.org/x/sys v0.0.0-20191009170203-06d7bd2c5f4f // indirect
golang.org/x/time v0.0.0-20190921001708-c4c64cad1fd0 // indirect
google.golang.org/appengine v1.6.5 // indirect
gopkg.in/asn1-ber.v1 v1.0.0-20181015200546-f715ec2f112d // indirect
Expand Down
51 changes: 46 additions & 5 deletions go.sum

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions helm/botkube/templates/deployment.yaml
Expand Up @@ -42,6 +42,8 @@ spec:
value: {{ .Values.logLevel | quote }}
- name: BOTKUBE_VERSION
value: {{ .Chart.Version }}
- name: METRICS_PORT
value: {{ .Values.service.targetPort | quote }}
{{- if .Values.extraEnv }}
{{- range $name, $value := .Values.extraEnv }}
- name: {{ $name }}
Expand Down
20 changes: 20 additions & 0 deletions helm/botkube/templates/service.yaml
@@ -0,0 +1,20 @@
{{- if .Values.serviceMonitor.enabled }}
apiVersion: v1
kind: Service
metadata:
name: {{ include "botkube.fullname" . }}
labels:
app.kubernetes.io/name: {{ include "botkube.name" . }}
helm.sh/chart: {{ include "botkube.chart" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
app: botkube
spec:
type: ClusterIP
ports:
- name: {{ .Values.service.name }}
port: {{ .Values.service.port }}
targetPort: {{ .Values.service.targetPort }}
selector:
app: botkube
{{- end }}
31 changes: 31 additions & 0 deletions helm/botkube/templates/servicemonitor.yaml
@@ -0,0 +1,31 @@
{{- if .Values.serviceMonitor.enabled }}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: {{ template "botkube.fullname" . }}
labels:
app.kubernetes.io/name: {{ include "botkube.name" . }}
helm.sh/chart: {{ include "botkube.chart" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- if .Values.serviceMonitor.labels }}
{{- toYaml .Values.serviceMonitor.labels | nindent 4 }}
{{- end }}

spec:
endpoints:
- interval: {{ .Values.serviceMonitor.interval }}
port: {{ .Values.serviceMonitor.port }}
path: {{ .Values.serviceMonitor.path }}
selector:
matchLabels:
app.kubernetes.io/name: {{ include "botkube.name" . }}
helm.sh/chart: {{ include "botkube.chart" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
component: controller
app: botkube
namespaceSelector:
matchNames:
- {{ .Release.Namespace }}
{{- end }}
15 changes: 15 additions & 0 deletions helm/botkube/values.yaml
Expand Up @@ -250,6 +250,21 @@ config:
# Set false to disable upgrade notification
upgradeNotifier: true

service:
name: metrics
port: 2112
targetPort: 2112

serviceMonitor:
## If true, a ServiceMonitor CR is created for a botkube
## https://github.com/coreos/prometheus-operator/blob/master/Documentation/api.md#servicemonitor
##
enabled: false
interval: 10s
path: /metrics
port: metrics
labels: {}

resources: {}
# We usually recommend not to specify default resources and to leave this as a conscious
# choice for the user. This also increases chances charts run on environments with little
Expand Down
12 changes: 12 additions & 0 deletions pkg/metrics/metrics.go
@@ -0,0 +1,12 @@
package metrics

import (
"github.com/prometheus/client_golang/prometheus/promhttp"
"net/http"
)

// ServeMetrics exposes metrics in Prometheus format
func ServeMetrics(metricsPort string) {
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":"+metricsPort, nil)
}

0 comments on commit 5c5acb7

Please sign in to comment.