{
  "slug": "Gin-framework-in-Golang-c453436c8c07",
  "title": "Gin framework in Golang",
  "subtitle": "https://gin-gonic.com/",
  "excerpt": "https://gin-gonic.com/",
  "date": "2023-11-07",
  "tags": [
    "Go",
    "Performance"
  ],
  "readingTime": "1 min",
  "url": "https://medium.com/@mobinshaterian/gin-framework-in-golang-c453436c8c07",
  "hero": "https://cdn-images-1.medium.com/max/800/1*NwnROdIFvkD0MdjEDQfoPg.png",
  "content": [
    {
      "type": "heading",
      "level": 2,
      "text": "Gin framework in Golang"
    },
    {
      "type": "paragraph",
      "html": "<a href=\"https://gin-gonic.com/\" target=\"_blank\" rel=\"noreferrer noopener\">https://gin-gonic.com/</a>"
    },
    {
      "type": "code",
      "lang": "go",
      "code": "I need to set gin mode to release mode. How should I do it?gin.SetMode(gin.ReleaseMode)"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Gin plus open telemetry"
    },
    {
      "type": "paragraph",
      "html": "<a href=\"https://stackoverflow.com/questions/72226307/how-to-get-in-opentelemetry-otelgin-middleware-the-traceid-or-full-context-to-pr\" target=\"_blank\" rel=\"noreferrer noopener\">If you are using otelgin middleware, the traceable context is propagated through </a><code><a href=\"https://stackoverflow.com/questions/72226307/how-to-get-in-opentelemetry-otelgin-middleware-the-traceid-or-full-context-to-pr\" target=\"_blank\" rel=\"noreferrer noopener\">ginContext.Request.Context()</a></code><a href=\"https://stackoverflow.com/questions/72226307/how-to-get-in-opentelemetry-otelgin-middleware-the-traceid-or-full-context-to-pr\" target=\"_blank\" rel=\"noreferrer noopener\">. So you can pass on this request context instead of ginContext to your gRPC client to propagate trace.</a>"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Prometheus with Gin"
    },
    {
      "type": "code",
      "lang": "go",
      "code": "func PrometheusMiddleware() gin.HandlerFunc {\n    return func(c *gin.Context) {\n        path := c.FullPath() timer :=\n        prometheus.NewTimer(MonitoringHttpDuration.WithLabelValues(path)) c.Next()\n        timer.ObserveDuration()\n    }\n}"
    },
    {
      "type": "code",
      "lang": "go",
      "code": "package main\nimport (\n\"github.com/gin-gonic/gin\"\n\"github.com/prometheus/client_golang/prometheus\"\n\"github.com/prometheus/client_golang/prometheus/promhttp\"\n)\nvar cpuTemp = prometheus.NewGauge(prometheus.GaugeOpts{\n    Name: \"cpu_temperature_celsius\",\n    Help: \"Current temperature of the CPU.\",\n}\n)\nfunc init() {\n    prometheus.MustRegister(cpuTemp)\n}\nfunc prometheusHandler() gin.HandlerFunc {\n    h := promhttp.Handler()\n    return func(c *gin.Context) {\n        h.ServeHTTP(c.Writer, c.Request)\n    }\n}\nfunc main() {\n    cpuTemp.Set(65.3)\n    r := gin.New()\n    r.GET(\"/\", func(c *gin.Context) {\n        c.JSON(200, \"Hello world!\")\n    })\n    r.GET(\"/metrics\", prometheusHandler())\n    r.Run()\n}"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Transform gin.Context to context.Context"
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*NwnROdIFvkD0MdjEDQfoPg.png",
      "alt": "Gin framework in Golang",
      "caption": "",
      "width": 761,
      "height": 476
    }
  ]
}
