Created
April 10, 2023 17:58
-
-
Save carlosm27/d46f82807c13f37382668ff73377c86c to your computer and use it in GitHub Desktop.
Complete main.go file with prometheus handler
This file contains hidden or 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
| package main | |
| import ( | |
| "net/http" | |
| "github.com/gin-gonic/gin" | |
| "github.com/prometheus/client_golang/prometheus" | |
| "github.com/prometheus/client_golang/prometheus/promhttp" | |
| ) | |
| func main() { | |
| prometheus.MustRegister(pingCounter) | |
| r := gin.Default() | |
| r.GET("/", func(c *gin.Context) { | |
| c.JSON(http.StatusOK, gin.H{ | |
| "message": "Hello World", | |
| }) | |
| }) | |
| r.GET("/ping", ping) | |
| r.GET("/metrics", gin.WrapH(promhttp.Handler())) | |
| r.Run() | |
| } | |
| var pingCounter = prometheus.NewCounter( | |
| prometheus.CounterOpts{ | |
| Name: "ping_request_count", | |
| Help: "No of request handled by Ping handler", | |
| }, | |
| ) | |
| func ping(c *gin.Context) { | |
| pingCounter.Inc() | |
| c.JSON(http.StatusOK, gin.H{ | |
| "message": "pong", | |
| }) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment