Skip to content

Commit abfd550

Browse files
authored
Distinguish between gin.Context and context.Context in example (#322)
1 parent b7beb0f commit abfd550

File tree

1 file changed

+5
-4
lines changed
  • instrumentation/github.com/gin-gonic/gin/example

1 file changed

+5
-4
lines changed

instrumentation/github.com/gin-gonic/gin/example/server.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package main
1616

1717
import (
18-
"context"
1918
"html/template"
2019
"log"
2120
"net/http"
@@ -42,7 +41,7 @@ func main() {
4241
r.SetHTMLTemplate(tmpl)
4342
r.GET("/users/:id", func(c *gin.Context) {
4443
id := c.Param("id")
45-
name := getUser(c.Request.Context(), id)
44+
name := getUser(c, id)
4645
gintrace.HTML(c, http.StatusOK, tmplName, gin.H{
4746
"name": name,
4847
"id": id,
@@ -69,8 +68,10 @@ func initTracer() {
6968
otelglobal.SetTraceProvider(tp)
7069
}
7170

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

0 commit comments

Comments
 (0)