Instrumenting Gin with OpenTelemetry

Installation

To install otelgin:

go get go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin

Usage

You can instrument Gin router by installing OpenTelemetry middleware:

router := gin.Default()
router.Use(otelgin.Middleware("service-name"))

Instrumenting templates rendering

To instrument templates rendering, use otelgin.HTML helper:

import "go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin"

func exampleHandler(c *gin.Context) {
	otelgin.HTML(c, http.StatusOK, indexTmpl, gin.H{
		"foo": "bar",
	})
}

What's next?

Next, instrument more operations, for example, database queries, errors, and logs. You can also learn about OpenTelemetry API to create your own instrumentations.