Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MilosSimic/5749951bfcd20e555fb8f3c2c7da6bd0 to your computer and use it in GitHub Desktop.
Save MilosSimic/5749951bfcd20e555fb8f3c2c7da6bd0 to your computer and use it in GitHub Desktop.
Prometheus example
package main
import (
"log"
"net/http"
"time"
"github.com/alexellis/faas/gateway/metrics"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus"
io_prometheus_client "github.com/prometheus/client_model/go"
)
func main() {
gatewayFunctionInvocation := prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "gateway_function_invocation_total",
Help: "Individual function metrics",
},
[]string{"function_name", "http_code"},
)
r := mux.NewRouter()
metricsHandler := metrics.PrometheusHandler()
r.Handle("/metrics", metricsHandler)
r.PathPrefix("/").Handler(http.FileServer(http.Dir("./assets/"))).Methods("GET")
s := &http.Server{
Addr: ":8080",
ReadTimeout: 8 * time.Second,
WriteTimeout: 8 * time.Second,
MaxHeaderBytes: 1 << 20,
Handler: r,
}
serviceName := "main"
labels := prometheus.Labels{"http_code": "500", "function_name": serviceName}
gatewayFunctionInvocation.With(labels).Add(1)
labelsOK := prometheus.Labels{"http_code": "200", "function_name": serviceName}
gatewayFunctionInvocation.With(labelsOK).Add(1)
prometheus.Register(gatewayFunctionInvocation)
// ideally this would work giving 2, but technically matches nothing.
allCodesForService := prometheus.Labels{"http_code": "", "function_name": serviceName}
counter := gatewayFunctionInvocation.With(allCodesForService)
// Get the metric's value from ProtoBuf interface (idea via Julius Volz)
var protoMetric io_prometheus_client.Metric
counter.Write(&protoMetric)
invocations := protoMetric.GetCounter().GetValue()
log.Println(invocations)
log.Fatal(s.ListenAndServe())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment