skywalking go agent configuration

summary

skywalking is an open source APM system based on OpenTracing specification. It is specially designed for micro service architecture and cloud native architecture. It supports clients in multiple languages. It is simple and fast to deploy. At present, it is widely used in the industry. For specific skywalking installation and deployment, please refer to the previous blog post: SkyWalking.
Since some of our underlying services are implemented by golang, skywalking go agent needs to be integrated for apm analysis.

Environment construction

namelinkdescribe
go2skyhttps://github.com/SkyAPM/go2skygo agent
go2sky-pluginshttps://github.com/SkyAPM/go2sky-pluginstrace access plug-in

go framework: gin
Installation command:

go get -u github.com/SkyAPM/go2sky 
go get -u github.com/SkyAPM/go2sky-plugins/gin/v3

Code integration

Package import:

import (
	"github.com/SkyAPM/go2sky"
	"github.com/SkyAPM/go2sky/reporter"
	"github.com/gin-gonic/gin"
	v3 "github.com/SkyAPM/go2sky-plugins/gin/v3"
)

Used in gin:

	r := gin.New()
	//skywalking
	rp, err := reporter.NewGRPCReporter("192.168.99.12:11800", reporter.WithCheckInterval(time.Second))
	if err != nil{
		logging.Info("create gosky reporter failed!")
	}
	tracer, err := go2sky.NewTracer("test-demo", go2sky.WithReporter(rp))
	#If we use the middleware of go2sky plugins, we don't need to write the span ourselves. The plug-in helps us complete it
	r.Use(v3.Middleware(r, tracer))

	gin.SetMode(setting.ServerSetting.RunMode)

Processing of plug-in span:

func Middleware(engine *gin.Engine, tracer *go2sky.Tracer) gin.HandlerFunc {
	# You need to use the tracer created by the upper layer
	if engine == nil || tracer == nil {
		return func(c *gin.Context) {
			c.Next()
		}
	}

	return func(c *gin.Context) {
	# Create span. EntrySpan is used here
		span, ctx, err := tracer.CreateEntrySpan(c.Request.Context(), getOperationName(c), func(key string) (string, error) {
			return c.Request.Header.Get(key), nil
		})
		if err != nil {
			c.Next()
			return
		}
		span.SetComponent(componentIDGINHttpServer)
		span.Tag(go2sky.TagHTTPMethod, c.Request.Method)
		span.Tag(go2sky.TagURL, c.Request.Host+c.Request.URL.Path)
		span.SetSpanLayer(agentv3.SpanLayer_Http)

		c.Request = c.Request.WithContext(ctx)
		#Request to continue execution in gin
		c.Next()

		if len(c.Errors) > 0 {
			span.Error(time.Now(), c.Errors.String())
		}
		#End span
		span.Tag(go2sky.TagStatusCode, strconv.Itoa(c.Writer.Status()))
		span.End()
	}
}

Call effect


Tracking chart:

What is Opentracing

Opentracing is a standard of distributed link tracing and one of the projects under CNCF (cloud native Computing Foundation). Different from the general specification standards, opentracing is not a specification standard at the level of transmission protocol and message format, but an API standard at the level of language.

Trace

Trace is a description of the movement of things in the distributed system. A trace represents the execution process of a transaction, request or process in the distributed system. A trace in OpenTracing is considered as a directed acyclic graph (DAG graph) composed of multiple spans. As shown in the following figure, multiple spans form a trace:

Span

Span represents the logical units with start time and execution time in the system. Logical causality is established between spans through nesting or sequential arrangement.
Each Span encapsulates the following states:

  • Operation name
  • Start timestamp
  • Completion timestamp
  • A group of Span Tags with zero or more keys: values. Keys must be strings, and values can be strings, bools and numeric
  • A set of zero or more Span Logs. The log itself is a key:value pair matching the timestamp. The key must be a string, although the value can be of any type. Not all opentracing implementations must support each value type
  • A SpanContext
  • Reference zero or more causal Spans through SpanContext

Relationship between span s in a Trace:

quote

OpenTracing: https://opentracing.io/specification/

Added by Pezmc on Mon, 07 Feb 2022 11:44:07 +0200