mirror of
https://github.com/kataras/iris.git
synced 2025-12-21 20:07:04 +00:00
add experimental handlers examples
Former-commit-id: 8d8a0d15afce2554dc5926f6b9bd9c42cb95dad0
This commit is contained in:
39
_examples/experimental-handlers/prometheus/simple/main.go
Normal file
39
_examples/experimental-handlers/prometheus/simple/main.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
|
||||
prometheusMiddleware "github.com/iris-contrib/middleware/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
m := prometheusMiddleware.New("serviceName", 0.3, 1.2, 5.0)
|
||||
|
||||
app.Use(m.ServeHTTP)
|
||||
|
||||
app.OnErrorCode(iris.StatusNotFound, func(ctx iris.Context) {
|
||||
// error code handlers are not sharing the same middleware as other routes, so we have
|
||||
// to call them inside their body.
|
||||
m.ServeHTTP(ctx)
|
||||
|
||||
ctx.Writef("Not Found")
|
||||
})
|
||||
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
sleep := rand.Intn(4999) + 1
|
||||
time.Sleep(time.Duration(sleep) * time.Millisecond)
|
||||
ctx.Writef("Slept for %d milliseconds", sleep)
|
||||
})
|
||||
|
||||
app.Get("/metrics", iris.FromStd(promhttp.Handler()))
|
||||
|
||||
// http://localhost:8080/
|
||||
// http://localhost:8080/anotfound
|
||||
// http://localhost:8080/metrics
|
||||
app.Run(iris.Addr(":8080"))
|
||||
}
|
||||
Reference in New Issue
Block a user