# Instrumenting net/http server with OpenTelemetry

# Prerequisites

  • Go 1.13+

# Introduction

In this tutorial we will instrument a basicopen in new window net/http server using OpenTelemetry instrumentation.

# Instrumenting net/http app

  • Step 1. Let's start by downloading the source code:
git clone https://github.com/uptrace/uptrace-go.git
cd example/net-http
1
2
  • Step 2. Install uptrace-go and net/http instrumentation:
go get github.com/uptrace/uptrace-go
go get go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp
1
2
  • Step 3. Edit main.go to configure OpenTelemetry to export data to Uptrace:
import "github.com/uptrace/uptrace-go/uptrace"

uptrace.ConfigureOpentelemetry(&uptrace.Config{
	// copy your project DSN here or use UPTRACE_DSN env var
	DSN: "",
})
1
2
3
4
5
6
  • Step 4. Edit main.go to instrument the net/http server:
func main() {
    http.Handle("/hello", otelhttp.NewHandler(
        http.HandlerFunc(helloHandler),
        "hello-handler",
    ))
}

func helloHandler(w http.ResponseWriter, req *http.Request) { ... }
1
2
3
4
5
6
7
8
UPTRACE_DSN="https://<token>@api.uptrace.dev/<project_id>" go run main.go
1

The full code is available at GitHubopen in new window.

# What is next?

By now, you should have a Django app with instrumented views. Next, instrumentopen in new window more operations (for example, database queries) or learn about OpenTelemetry API to create your own instrumentations.