Golang用skywalking全链路追踪

Golang用skywalking全链路追踪

1.docker安装

(1.)ElasticSearch (https://hub.docker.com/_/elasticsearch):

docker pull docker.elastic.co/elasticsearch/elasticsearch:7.9.3

docker run -d --name elasticsearch --rm -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:7.9.3

(2.)skywalking (https://hub.docker.com/r/apache/skywalking-oap-server)

docker pull apache/skywalking-oap-server

docker run --name skywalking-oap-server -d --rm -p 11800:11800 -p 12800:12800 --link elasticsearch:elasticsearch -e SW_STORAGE=elasticsearch7 -e SW_STORAGE_ES_CLUSTER_NODES=elasticsearch:9200 apache/skywalking-oap-server

如果使用的是 ES6,则设置 SW_STORAGE 的值为:elasticsearch,参考来源:https://github.com/apache/skywalking/blob/master/docs/en/setup/backend/backend-storage.md。

注意,这里的 11800 端口为上报端口,即调用 NewGRPCReporter 时指定的端口。

(3.)skywalking-ui (https://hub.docker.com/r/apache/skywalking-ui)

docker pull apache/skywalking-ui

docker run --name skywalking-ui -d --rm -p 8080:8080 --link skywalking-oap-server:skywalking-oap-server -e SW_OAP_ADDRESS=skywalking-oap-server:12800 apache/skywalking-ui

注意,这里的 8080 端口为可视化页面端口。
https://www.cnblogs.com/aquester/p/13953663.html

2.skywalking上报go数据

https://cloud.tencent.com/document/product/1311/51607
https://studygolang.com/articles/33441
https://blog.csdn.net/zhounixing/article/details/105815910

3.echo框架代码实现

// Use gRPC reporter for production
	re, err := reporter.NewGRPCReporter("127.0.0.1:11800")
	//re, err := reporter.NewLogReporter()
	if err != nil {
		log.Fatalf("new reporter error %v \n", err)
	}
	defer re.Close()

	tracer, err := go2sky.NewTracer("test", go2sky.WithReporter(re), go2sky.WithInstance("test_Instance"))
	if err != nil {
		log.Fatalf("create tracer error %v \n", err)
	}

	e.Use(func(handlerFunc echo.HandlerFunc) echo.HandlerFunc {
		return func(c echo.Context) error {
			if tracer == nil {
				return handlerFunc(c)
			}
			span, ctx, err := tracer.CreateEntrySpan(c.Request().Context(), getOperationName(c), func() (string, error) {
				return c.Request().Header.Get(propagation.Header), nil
			})

			if err != nil {
				return handlerFunc(c)
			}

			span.SetComponent(5004)
			span.Tag(go2sky.TagHTTPMethod, c.Request().Method)
			span.Tag(go2sky.TagURL, c.Request().Host+c.Request().URL.Path)
			span.SetSpanLayer(1)
			c.Request().WithContext(ctx)
			span.Tag(go2sky.TagStatusCode, strconv.Itoa(c.Response().Status))
			if c.Response().Status >= 400 {
				span.Error(time.Now(), string(c.Response().Body))
			}
			span.End()
			return handlerFunc(c)
		}
	})

相关链接

https://github.com/SkyAPM/go2sky
https://github.com/SkyAPM/go2sky-plugins
https://skywalking.apache.org/zh/2020-04-19-skywalking-quick-start/
https://www.cnblogs.com/yougewe/p/11973117.html
https://www.codenong.com/cs105815910/
https://github.com/opentracing-contrib/echo
https://insights.thoughtworks.cn/skywalking-plugin-guide/
https://blog.csdn.net/zhounixing/article/details/105815910
https://juejin.cn/post/6871928187123826702

上一篇:Apache Skywalking Rce复现


下一篇:sykwalking分布式微服务链路追踪