• golang学习笔记(二):链路追踪


    自定义http连接的服务端

    package server
    
    import (
    	"github.com/gin-gonic/gin"
    	"go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin"
    	"net/http"
    )
    
    type MyServer struct {
    	Server *http.Server
    }
    
    func GetServer() *MyServer {
    	gin.SetMode(gin.ReleaseMode)
    	engine := gin.New()
    	s := &http.Server{
    		Handler: engine,
    	}
    	return &MyServer{
    		Server: s,
    	}
    }
    
    func (s *MyServer) GetEngine() *gin.Engine {
    	return s.Server.Handler.(*gin.Engine)
    }
    
    func (s *MyServer) Start(addr string) error {
    	e := s.GetEngine()
    	err2 := e.Run(addr)
    	if err2 != nil {
    		return err2
    	}
    	err := s.Server.ListenAndServe()
    	if err != nil {
    		return err
    	}
    	return nil
    }
    
    func (s *MyServer) EnableTracing() {
    	s.GetEngine().Use(otelgin.Middleware("go20230923"))
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    初始化链路追踪

    package tracing
    
    import (
    	"context"
    	"fmt"
    	"go.opentelemetry.io/contrib/propagators/b3"
    	"go.opentelemetry.io/otel"
    	"go.opentelemetry.io/otel/exporters/jaeger"
    	"go.opentelemetry.io/otel/exporters/stdout/stdouttrace"
    	"go.opentelemetry.io/otel/propagation"
    	"go.opentelemetry.io/otel/sdk/resource"
    	"go.opentelemetry.io/otel/sdk/trace"
    	semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
    )
    
    var tracer = otel.Tracer("go20230923")
    
    func InitStdoutProvider(serviceName string) (func(ctx context.Context) error, error) {
    	tracer = otel.Tracer(serviceName)
    	// stdout as exporter
    	exporter, err := stdouttrace.New(stdouttrace.WithPrettyPrint())
    	if err != nil {
    		return nil, err
    	}
    	tp := trace.NewTracerProvider(
    		trace.WithSampler(trace.AlwaysSample()),
    		trace.WithBatcher(exporter),
    	)
    	otel.SetTracerProvider(tp)
    
    	otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))
    	return tp.Shutdown, nil
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    注册路由

    package router
    
    import (
    	"fmt"
    	"github.com/gin-gonic/gin"
    	"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
    	"go.opentelemetry.io/otel/trace"
    	"go20230923/pkg/server"
    	"log"
    	"net/http"
    )
    
    func InitRouter(s *server.MyServer) {
    	engine := s.GetEngine()
    	group := engine.Group("/v1")
    	group.GET("/hello", Hello)
    	group.GET("/world", World)
    }
    
    func Hello(c *gin.Context) {
    	if trace.SpanFromContext(c.Request.Context()).SpanContext().IsValid() {
    		spanCtx := trace.SpanFromContext(c.Request.Context()).SpanContext()
    		traceId := spanCtx.TraceID()
    		spanId := spanCtx.SpanID()
    		fmt.Printf("traceId : %s\n", traceId)
    		fmt.Printf("spanId : %s\n", spanId)
    	}
    	fmt.Println("hello world")
    	request, err := http.NewRequestWithContext(c.Request.Context(), "GET", "http://localhost:8080/v1/world", nil)
    	if err != nil {
    		log.Fatal(err)
    	}
    	client := http.Client{Transport: otelhttp.NewTransport(http.DefaultTransport)}
    	client.Do(request)
    }
    
    func World(c *gin.Context) {
    	if trace.SpanFromContext(c.Request.Context()).SpanContext().IsValid() {
    		spanCtx := trace.SpanFromContext(c.Request.Context()).SpanContext()
    		traceId := spanCtx.TraceID()
    		spanId := spanCtx.SpanID()
    		fmt.Printf("traceId : %s\n", traceId)
    		fmt.Printf("spanId : %s\n", spanId)
    	}
    	fmt.Println("hello world")
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    启动入口

    package main
    
    import (
    	"fmt"
    	"go20230923/pkg/server"
    	"go20230923/pkg/tracing"
    	"go20230923/router"
    )
    
    func main() {
    	s := server.GetServer()
    	s.EnableTracing()
    	tracing.InitStdoutProvider("go20230923")
    	router.InitRouter(s)
    	err := s.Start(":8080")
    	if err != nil {
    		fmt.Println(err)
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    GET http://localhost:8080/v1/hello 输出:

    traceId : 302c3e67a16bd20ce35b9220a4297d08
    spanId : f80dd7189261ed84
    hello world
    traceId : 302c3e67a16bd20ce35b9220a4297d08
    spanId : 7826014d750cf1c5
    hello world
    {
    
            "Name": "/v1/world",
            "SpanContext": {
                    "TraceID": "302c3e67a16bd20ce35b9220a4297d08",
                    "SpanID": "7826014d750cf1c5",
                    "TraceFlags": "01",
                    "TraceState": "",
                    "Remote": false
            },
            "Parent": {
                    "TraceID": "302c3e67a16bd20ce35b9220a4297d08",
                    "SpanID": "aaa69798fdc0e90c",
                    "TraceFlags": "01",
                    "TraceState": "",
                    "Remote": true
            },
            "SpanKind": 2,
            "StartTime": "2023-09-29T11:32:41.5265746+08:00",
            "EndTime": "2023-09-29T11:32:41.5271363+08:00",
            "Attributes": [
                    {
                            "Key": "http.method",
                            "Value": {
                                    "Type": "STRING",
                                    "Value": "GET"
                            }
                    },
                    {
                            "Key": "http.scheme",
                            "Value": {
                                    "Type": "STRING",
                                    "Value": "http"
                            }
                    },
                    {
                            "Key": "http.flavor",
                            "Value": {
                                    "Type": "STRING",
                                    "Value": "1.1"
                            }
                    },
                    {
                            "Key": "net.host.name",
                            "Value": {
                                    "Type": "STRING",
                                    "Value": "go20230923"
                            }
                    },
                    {
                            "Key": "net.host.port",
                            "Value": {
                                    "Type": "INT64",
                                    "Value": 8080
                            }
                    },
                    {
                            "Key": "net.sock.peer.addr",
                            "Value": {
                                    "Type": "STRING",
                                    "Value": "::1"
                            }
                    },
                    {
                            "Key": "net.sock.peer.port",
                            "Value": {
                                    "Type": "INT64",
                                    "Value": 62471
                            }
                    },
                    {
                            "Key": "http.user_agent",
                            "Value": {
                                    "Type": "STRING",
                                    "Value": "Go-http-client/1.1"
                            }
                    },
                    {
                            "Key": "http.route",
                            "Value": {
                                    "Type": "STRING",
                                    "Value": "/v1/world"
                            }
                    },
                    {
                            "Key": "http.status_code",
                            "Value": {
                                    "Type": "INT64",
                                    "Value": 200
                            }
                    }
            ],
            "Events": null,
            "Links": null,
            "Status": {
                    "Code": "Unset",
                    "Description": ""
            },
            "DroppedAttributes": 0,
            "DroppedEvents": 0,
            "DroppedLinks": 0,
            "ChildSpanCount": 0,
            "Resource": [
                    {
                            "Key": "service.name",
                            "Value": {
                                    "Type": "STRING",
                                    "Value": "unknown_service:___go_build_go20230923.exe"
                            }
                    },
                    {
                            "Key": "telemetry.sdk.language",
                            "Value": {
                                    "Type": "STRING",
                                    "Value": "go"
                            }
                    },
                    {
                            "Key": "telemetry.sdk.name",
                            "Value": {
                                    "Type": "STRING",
                                    "Value": "opentelemetry"
                            }
                    },
                    {
                            "Key": "telemetry.sdk.version",
                            "Value": {
                                    "Type": "STRING",
                                    "Value": "1.18.0"
                            }
                    }
            ],
            "InstrumentationLibrary": {
                    "Name": "go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin",
                    "Version": "0.44.0",
                    "SchemaURL": ""
            }
    }
    {
            "Name": "/v1/hello",
            "SpanContext": {
                    "TraceID": "302c3e67a16bd20ce35b9220a4297d08",
                    "SpanID": "f80dd7189261ed84",
                    "TraceFlags": "01",
                    "TraceState": "",
                    "Remote": false
            },
            "Parent": {
                    "TraceID": "00000000000000000000000000000000",
                    "SpanID": "0000000000000000",
                    "TraceFlags": "00",
                    "TraceState": "",
                    "Remote": false
            },
            "SpanKind": 2,
            "StartTime": "2023-09-29T11:32:41.5252493+08:00",
            "EndTime": "2023-09-29T11:32:41.5271363+08:00",
            "Attributes": [
                    {
                            "Key": "http.method",
                            "Value": {
                                    "Type": "STRING",
                                    "Value": "GET"
                            }
                    },
                    {
                            "Key": "http.scheme",
                            "Value": {
                                    "Type": "STRING",
                                    "Value": "http"
                            }
                    },
                    {
                            "Key": "http.flavor",
                            "Value": {
                                    "Type": "STRING",
                                    "Value": "1.1"
                            }
                    },
                    {
                            "Key": "net.host.name",
                            "Value": {
                                    "Type": "STRING",
                                    "Value": "go20230923"
                            }
                    },
                    {
                            "Key": "net.host.port",
                            "Value": {
                                    "Type": "INT64",
                                    "Value": 8080
                            }
                    },
                    {
                            "Key": "net.sock.peer.addr",
                            "Value": {
                                    "Type": "STRING",
                                    "Value": "::1"
                            }
                    },
                    {
                            "Key": "net.sock.peer.port",
                            "Value": {
                                    "Type": "INT64",
                                    "Value": 64585
                            }
                    },
                    {
                            "Key": "http.user_agent",
                            "Value": {
                                    "Type": "STRING",
                                    "Value": "PostmanRuntime/7.28.0"
                            }
                    },
                    {
                            "Key": "http.route",
                            "Value": {
                                    "Type": "STRING",
                                    "Value": "/v1/hello"
                            }
                    },
                    {
                            "Key": "http.status_code",
                            "Value": {
                                    "Type": "INT64",
                                    "Value": 200
                            }
                    }
            ],
            "Events": null,
            "Links": null,
            "Status": {
                    "Code": "Unset",
                    "Description": ""
            },
            "DroppedAttributes": 0,
            "DroppedEvents": 0,
            "DroppedLinks": 0,
            "ChildSpanCount": 1,
            "Resource": [
                    {
                            "Key": "service.name",
                            "Value": {
                                    "Type": "STRING",
                                    "Value": "unknown_service:___go_build_go20230923.exe"
                            }
                    },
                    {
                            "Key": "telemetry.sdk.language",
                            "Value": {
                                    "Type": "STRING",
                                    "Value": "go"
                            }
                    },
                    {
                            "Key": "telemetry.sdk.name",
                            "Value": {
                                    "Type": "STRING",
                                    "Value": "opentelemetry"
                            }
                    },
                    {
                            "Key": "telemetry.sdk.version",
                            "Value": {
                                    "Type": "STRING",
                                    "Value": "1.18.0"
                            }
                    }
            ],
            "InstrumentationLibrary": {
                    "Name": "go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin",
                    "Version": "0.44.0",
                    "SchemaURL": ""
            }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    参考

    Go语言 如何获取有关 *gin.Context.Request.Context()的详细信息
    golang 使用 OpenTelemetry 实现跨服务全链路追踪
    github go-moda项目

  • 相关阅读:
    计算机网络基础之计算机网络组成与分类
    ROS2 从头开始:第 4 部分 - 使用 ROS2 组合构建强大的机器人系统
    Flink不止于计算,存算一体才是未来
    Maven依赖冲突
    cmd、conhost退居二线,Win 11将设置默认终端
    DBA常用命令和数据库设计三范式
    springboot过滤器拦截自定义异常
    操作系统4小时速成:文件管理,文件结构,属性,基本操作,逻辑有无结构,目录结构,文件系统
    DataPath实现渐变效果
    vue - vue基础/vue核心内容
  • 原文地址:https://blog.csdn.net/u012734723/article/details/132656149