-
Notifications
You must be signed in to change notification settings - Fork 71
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 #42 from slok/slok/std-provider
Add handler provider for multiple frameworks and support chi, alice and gorilla frameworks
- Loading branch information
Showing
13 changed files
with
440 additions
and
56 deletions.
There are no files selected for viewing
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,64 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
"time" | ||
|
||
"github.com/justinas/alice" | ||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
metrics "github.com/slok/go-http-metrics/metrics/prometheus" | ||
"github.com/slok/go-http-metrics/middleware" | ||
"github.com/slok/go-http-metrics/middleware/std" | ||
) | ||
|
||
const ( | ||
srvAddr = ":8080" | ||
metricsAddr = ":8081" | ||
) | ||
|
||
func main() { | ||
// Create our middleware. | ||
mdlw := middleware.New(middleware.Config{ | ||
Recorder: metrics.NewRecorder(metrics.Config{}), | ||
}) | ||
|
||
// Create our server. | ||
mux := http.NewServeMux() | ||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | ||
time.Sleep(200 * time.Millisecond) | ||
w.WriteHeader(http.StatusOK) | ||
}) | ||
mux.HandleFunc("/test1", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusCreated) }) | ||
mux.HandleFunc("/test1/test2", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusAccepted) }) | ||
mux.HandleFunc("/test1/test4", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNonAuthoritativeInfo) }) | ||
mux.HandleFunc("/test2", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNoContent) }) | ||
mux.HandleFunc("/test3", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusResetContent) }) | ||
|
||
// Wrap with middleware. | ||
h := alice.New(std.HandlerProvider("", mdlw)).Then(mux) | ||
|
||
// Serve our handler. | ||
go func() { | ||
log.Printf("server listening at %s", srvAddr) | ||
if err := http.ListenAndServe(srvAddr, h); err != nil { | ||
log.Panicf("error while serving: %s", err) | ||
} | ||
}() | ||
|
||
// Serve our metrics. | ||
go func() { | ||
log.Printf("metrics listening at %s", metricsAddr) | ||
if err := http.ListenAndServe(metricsAddr, promhttp.Handler()); err != nil { | ||
log.Panicf("error while serving metrics: %s", err) | ||
} | ||
}() | ||
|
||
// Wait until some signal is captured. | ||
sigC := make(chan os.Signal, 1) | ||
signal.Notify(sigC, syscall.SIGTERM, syscall.SIGINT) | ||
<-sigC | ||
} |
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,64 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
"time" | ||
|
||
"github.com/go-chi/chi" | ||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
metrics "github.com/slok/go-http-metrics/metrics/prometheus" | ||
"github.com/slok/go-http-metrics/middleware" | ||
"github.com/slok/go-http-metrics/middleware/std" | ||
) | ||
|
||
const ( | ||
srvAddr = ":8080" | ||
metricsAddr = ":8081" | ||
) | ||
|
||
func main() { | ||
// Create our middleware. | ||
mdlw := middleware.New(middleware.Config{ | ||
Recorder: metrics.NewRecorder(metrics.Config{}), | ||
}) | ||
|
||
// Create our router with the metrics middleware. | ||
r := chi.NewRouter() | ||
r.Use(std.HandlerProvider("", mdlw)) | ||
|
||
// Add paths. | ||
r.Get("/", func(w http.ResponseWriter, r *http.Request) { | ||
time.Sleep(200 * time.Millisecond) | ||
w.WriteHeader(http.StatusOK) | ||
}) | ||
r.Get("/test1", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusCreated) }) | ||
r.Get("/test1/test2", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusAccepted) }) | ||
r.Get("/test1/test4", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNonAuthoritativeInfo) }) | ||
r.Get("/test2", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNoContent) }) | ||
r.Get("/test3", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusResetContent) }) | ||
|
||
// Serve our handler. | ||
go func() { | ||
log.Printf("server listening at %s", srvAddr) | ||
if err := http.ListenAndServe(srvAddr, r); err != nil { | ||
log.Panicf("error while serving: %s", err) | ||
} | ||
}() | ||
|
||
// Serve our metrics. | ||
go func() { | ||
log.Printf("metrics listening at %s", metricsAddr) | ||
if err := http.ListenAndServe(metricsAddr, promhttp.Handler()); err != nil { | ||
log.Panicf("error while serving metrics: %s", err) | ||
} | ||
}() | ||
|
||
// Wait until some signal is captured. | ||
sigC := make(chan os.Signal, 1) | ||
signal.Notify(sigC, syscall.SIGTERM, syscall.SIGINT) | ||
<-sigC | ||
} |
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,64 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
"time" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
metrics "github.com/slok/go-http-metrics/metrics/prometheus" | ||
"github.com/slok/go-http-metrics/middleware" | ||
"github.com/slok/go-http-metrics/middleware/std" | ||
) | ||
|
||
const ( | ||
srvAddr = ":8080" | ||
metricsAddr = ":8081" | ||
) | ||
|
||
func main() { | ||
// Create our middleware. | ||
mdlw := middleware.New(middleware.Config{ | ||
Recorder: metrics.NewRecorder(metrics.Config{}), | ||
}) | ||
|
||
// Create our router with the metrics middleware. | ||
r := mux.NewRouter() | ||
r.Use(std.HandlerProvider("", mdlw)) | ||
|
||
// Add paths. | ||
r.Methods("GET").Path("/").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
time.Sleep(200 * time.Millisecond) | ||
w.WriteHeader(http.StatusOK) | ||
}) | ||
r.Methods("GET").Path("/test1").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusCreated) }) | ||
r.Methods("GET").Path("/test1/test2").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusAccepted) }) | ||
r.Methods("GET").Path("/test1/test4").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNonAuthoritativeInfo) }) | ||
r.Methods("GET").Path("/test2").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNoContent) }) | ||
r.Methods("GET").Path("/test3").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusResetContent) }) | ||
|
||
// Serve our handler. | ||
go func() { | ||
log.Printf("server listening at %s", srvAddr) | ||
if err := http.ListenAndServe(srvAddr, r); err != nil { | ||
log.Panicf("error while serving: %s", err) | ||
} | ||
}() | ||
|
||
// Serve our metrics. | ||
go func() { | ||
log.Printf("metrics listening at %s", metricsAddr) | ||
if err := http.ListenAndServe(metricsAddr, promhttp.Handler()); err != nil { | ||
log.Panicf("error while serving metrics: %s", err) | ||
} | ||
}() | ||
|
||
// Wait until some signal is captured. | ||
sigC := make(chan os.Signal, 1) | ||
signal.Notify(sigC, syscall.SIGTERM, syscall.SIGINT) | ||
<-sigC | ||
} |
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 |
---|---|---|
@@ -1,16 +1,19 @@ | ||
module github.com/slok/go-http-metrics | ||
|
||
require ( | ||
contrib.go.opencensus.io/exporter/prometheus v0.1.0 | ||
github.com/emicklei/go-restful v2.12.0+incompatible | ||
contrib.go.opencensus.io/exporter/prometheus v0.2.0 | ||
github.com/emicklei/go-restful v2.14.2+incompatible | ||
github.com/gin-gonic/gin v1.6.3 | ||
github.com/go-chi/chi v4.1.2+incompatible | ||
github.com/gorilla/mux v1.8.0 | ||
github.com/julienschmidt/httprouter v1.3.0 | ||
github.com/labstack/echo/v4 v4.1.16 | ||
github.com/prometheus/client_golang v1.6.0 | ||
github.com/stretchr/testify v1.6.0 | ||
github.com/justinas/alice v1.2.0 | ||
github.com/labstack/echo/v4 v4.1.17 | ||
github.com/prometheus/client_golang v1.7.1 | ||
github.com/stretchr/testify v1.6.1 | ||
github.com/urfave/negroni v1.0.0 | ||
go.opencensus.io v0.22.3 | ||
go.opencensus.io v0.22.4 | ||
goji.io v2.0.2+incompatible | ||
) | ||
|
||
go 1.14 | ||
go 1.15 |
Oops, something went wrong.