Instrumenting Gin with OpenTelemetry

Prerequisites

  • Go 1.13+

Resources

Introduction

In this tutorial we will instrument a basicopen in new window Gin app using OpenTelemetry instrumentation.

Instrumenting Gin app

git clone https://github.com/uptrace/uptrace-go.git
cd example/gin
  • Step 2. Install uptrace-go and Gin instrumentation:
go get github.com/uptrace/uptrace-go
go get go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin
  • Step 3. Edit main.go to configure OpenTelemetry to export data to Uptrace:
import "github.com/uptrace/uptrace-go/uptrace"

uptrace.ConfigureOpentelemetry(
	// copy your project DSN here or use UPTRACE_DSN env var
	// uptrace.WithDSN("https://<key>@api.uptrace.dev/<project_id>"),

	uptrace.WithServiceName("myservice"),
	uptrace.WithServiceVersion("1.0.0"),
)
  • Step 4. Edit main.go to instrument the Gin app:
router := gin.Default()
router.Use(otelgin.Middleware("service-name"))
UPTRACE_DSN="https://<token>@api.uptrace.dev/<project_id>" go run main.go

Instrumenting templates rendering

To instrument html/template rendering, use otelgin.HTML helper:

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

What is next?

Next, instrumentopen in new window more operations (for example, database queries or error logs) or learn about OpenTelemetry API to create your own instrumentations.