Skip to content

Commit

Permalink
Distinguish between gin.Context and context.Context in example (#322)
Browse files Browse the repository at this point in the history
  • Loading branch information
evantorrie committed Sep 9, 2020
1 parent b7beb0f commit abfd550
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions instrumentation/github.com/gin-gonic/gin/example/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package main

import (
"context"
"html/template"
"log"
"net/http"
Expand All @@ -42,7 +41,7 @@ func main() {
r.SetHTMLTemplate(tmpl)
r.GET("/users/:id", func(c *gin.Context) {
id := c.Param("id")
name := getUser(c.Request.Context(), id)
name := getUser(c, id)
gintrace.HTML(c, http.StatusOK, tmplName, gin.H{
"name": name,
"id": id,
Expand All @@ -69,8 +68,10 @@ func initTracer() {
otelglobal.SetTraceProvider(tp)
}

func getUser(ctx context.Context, id string) string {
_, span := tracer.Start(ctx, "getUser", oteltrace.WithAttributes(label.String("id", id)))
func getUser(c *gin.Context, id string) string {
// Pass the built-in `context.Context` object from http.Request to OpenTelemetry APIs
// where required. It is available from gin.Context.Request.Context()
_, span := tracer.Start(c.Request.Context(), "getUser", oteltrace.WithAttributes(label.String("id", id)))
defer span.End()
if id == "123" {
return "gintrace tester"
Expand Down

0 comments on commit abfd550

Please sign in to comment.