Skip to content

Commit

Permalink
Add rback setup to run in k8s
Browse files Browse the repository at this point in the history
  • Loading branch information
utkarshmani1997 committed Sep 14, 2017
1 parent cbf90e5 commit 1b4d042
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 24 deletions.
3 changes: 2 additions & 1 deletion prometheus-monitoring/deployment-prometheus.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ spec:
metadata:
labels:
app: prometheus-server
spec:
spec:
serviceAccountName: prometheus
containers:
- name: prometheus
image: prom/prometheus:latest
Expand Down
46 changes: 23 additions & 23 deletions prometheus-monitoring/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,54 +5,54 @@ import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"net/http"
"strconv"
"time"
)

var (
// How often our /hello request durations fall into one of the defined buckets.
// How often our /latest/volume request durations fall into one of the defined buckets.
// We can use default buckets or set ones we are interested in.
duration = prometheus.NewHistogram(prometheus.HistogramOpts{
Name: "hello_request_duration_seconds",
Help: "Histogram of the /hello request duration.",
Buckets: []float64{0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10},
})
volumeRequestDuration = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "volume_request_duration_seconds",
Help: "Histogram of the /latest/volumes request duration.",
Buckets: []float64{0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10},
},
[]string{"code", "method"},
)
// Counter vector to which we can attach labels. That creates many key-value
// label combinations. So in our case we count requests by status code separetly.
counter = prometheus.NewCounterVec(
volumeRequestCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "hello_requests_total",
Help: "Total number of /hello requests.",
Name: "volume_requests_total",
Help: "Total number of /latest/volumes requests.",
},
[]string{"status"},
[]string{"code", "method"},
)
)

// init registers Prometheus metrics.
func init() {
prometheus.MustRegister(duration)
prometheus.MustRegister(counter)
prometheus.MustRegister(volumeRequestDuration)
prometheus.MustRegister(volumeRequestCounter)
}

func handler(w http.ResponseWriter, r *http.Request) {
var status int

var code int
defer func(begun time.Time) {
duration.Observe(time.Since(begun).Seconds())
volumeRequestDuration.WithLabelValues(strconv.Itoa(code), r.Method).Observe(time.Since(begun).Seconds())

// hello_requests_total{status="200"} 2385
counter.With(prometheus.Labels{
"status": fmt.Sprint(status),
}).Inc()
// volume_requests_total{status="200"}
volumeRequestCounter.WithLabelValues(strconv.Itoa(code), r.Method).Inc()
}(time.Now())

status = 200
w.WriteHeader(status)
code = 200
w.WriteHeader(code)

fmt.Fprintf(w, "Hi there, the end point is : %s !", r.URL.Path[1:])
}

func main() {
http.HandleFunc("/", handler)
http.HandleFunc("/latest/volumes", handler)
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":8080", nil)
}
35 changes: 35 additions & 0 deletions prometheus-monitoring/rbac-setup.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRole
metadata:
name: prometheus
rules:
- apiGroups: [""]
resources:
- nodes
- nodes/proxy
- services
- endpoints
- pods
verbs: ["get", "list", "watch"]
- nonResourceURLs: ["/metrics"]
verbs: ["get"]
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: prometheus
namespace: default
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
name: prometheus
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: prometheus
subjects:
- kind: ServiceAccount
name: prometheus
namespace: default

0 comments on commit 1b4d042

Please sign in to comment.