Categorygithub.com/SkyAPM/go2sky
modulepackage
1.5.0
Repository: https://github.com/skyapm/go2sky.git
Documentation: pkg.go.dev

# README

GO2Sky

Build Coverage GoDoc

GO2Sky is an instrument SDK library, written in Go, by following Apache SkyWalking tracing and metrics formats.

Installation

$ go get -u github.com/SkyAPM/go2sky

The API of this project is still evolving. The use of vendoring tool is recommended.

Quickstart

By completing this quickstart, you will learn how to trace local methods. For more details, please view the example.

Configuration

GO2Sky can export traces to Apache SkyWalking OAP server or local logger. In the following example, we configure GO2Sky to export to OAP server, which is listening on oap-skywalking port 11800, and all the spans from this program will be associated with a service name example. reporter.GRPCReporter can also adjust the behavior through reporter.GRPCReporterOption, view all.

r, err := reporter.NewGRPCReporter("oap-skywalking:11800")
if err != nil {
    log.Fatalf("new reporter error %v \n", err)
}
defer r.Close()
tracer, err := go2sky.NewTracer("example", go2sky.WithReporter(r))

In some scenarios, we may need a filter to filter segments that do not need to be submitted, for example, to reduce the load of gRPC reporting, or only track the request of error.

r, err := reporter.NewGRPCReporter("oap-skywalking:11800", reporter.WithReportStrategy(func(s *v3.SegmentObject) bool {
	var isReport bool
	for _, span := s.GetSpans() {
		if span.GetIsError() {
			isReport = true
			break
		}
	}
	
	return isReport
}))

You can also create tracer with sampling rate. It supports decimals between 0-1 (two decimal places), representing the sampling percentage of trace.

tracer, err := go2sky.NewTracer("example", go2sky.WithReporter(r), go2sky.WithSampler(0.5))

Also could customize correlation context config.

tracer, err := go2sky.NewTracer("example", go2sky.WithReporter(r), go2sky.WithSampler(0.5), go2sky.WithCorrelation(3, 128))

Create span

To create a span in a trace, we used the Tracer to start a new span. We indicate this as the root span because of passing context.Background(). We must also be sure to end this span, which will be show in End span.

span, ctx, err := tracer.CreateLocalSpan(context.Background())

Create a sub span

A sub span created as the children of root span links to its parent with Context.

subSpan, newCtx, err := tracer.CreateLocalSpan(ctx)

Get correlation

Get custom data from tracing context.

value := go2sky.GetCorrelation(ctx, key)

Put correlation

Put custom data to tracing context.

success := go2sky.PutCorrelation(ctx, key, value)

End span

We must end the spans so they becomes available for sending to the backend by a reporter.

subSpan.End()
span.End()

Global Tracer

Set and get global Tracer

// new tracer
tr, err := go2sky.NewTracer("example")

// registers `tracer` as the global Tracer
go2sky.SetGlobalTracer(tr)

// returns the registered global Tracer
// if none is registered then an instance of `nil` is returned
tracer := go2sky.GetGlobalTracer()

Get Active Span

Get the activeSpan in the Context.

go2sky.ActiveSpan(ctx)

With Span

Save the activeSpan to Context

go2sky.WithSpan(ctx, activeSpan)

Get Global Service Name

Get the ServiceName of the activeSpan in the Context.

go2sky.ServiceName(ctx)

Get Global Service Instance Name

Get the ServiceInstanceName of the activeSpan in the Context.

go2sky.ServiceInstanceName(ctx)

Get Global TraceID

Get the TraceID of the activeSpan in the Context.

go2sky.TraceID(ctx)

Get Global TraceSegmentID

Get the TraceSegmentID of the activeSpan in the Context.

go2sky.TraceSegmentID(ctx)

Get Global SpanID

Get the SpanID of the activeSpan in the Context.

go2sky.SpanID(ctx)

Periodically Report

Go2sky agent reports the segments periodically. It would not wait for all finished segments reported when the service exits.

Advanced Concepts

We cover some advanced topics about GO2Sky.

Context propagation

Trace links spans belong to it by using context propagation which varies between different scenario.

In process

We use context package to link spans. The root span usually pick context.Background(), and sub spans will inject the context generated by its parent.

//Create a new context
entrySpan, entryCtx, err := tracer.CreateEntrySpan(context.Background(), ...)

// Some operation
...

// Link two spans by injecting entrySpan context into exitSpan
exitSpan, err := tracer.CreateExitSpan(entryCtx, ...)

Crossing process

We use Entry span to extract context from downstream service, and use Exit span to inject context to upstream service.

Entry and Exit spans make sense to OAP analysis which generates topology map and service metrics.

//Extract context from HTTP request header `sw8`
span, ctx, err := tracer.CreateEntrySpan(r.Context(), "/api/login", func(key string) (string, error) {
		return r.Header.Get(key), nil
})

// Some operation
...

// Inject context into HTTP request header `sw8`
span, err := tracer.CreateExitSpan(req.Context(), "/service/validate", "tomcat-service:8080", func(key, value string) error {
		req.Header.Set(key, value)
		return nil
})

Tag

We set tags into a span which is stored in the backend, but some tags have special purpose. OAP server may use them to aggregate metrics, generate topology map and etc.

They are defined as constant in root package with prefix Tag.

Log x Trace context

Inject trace context into the log text. SkyWalking LAL(log analysis language) engine could extract the context from the text and correlate trace and logs.

// Get trace context data
import go2skylog "github.com/SkyAPM/go2sky/log"
logContext = go2skylog.FromContext(ctx)

// Build context data string
// Inject context string into log
// Context format string: [$serviceName,$instanceName,$traceId,$traceSegmentId,$spanId]
contextString := logContext.String()

Plugins

Go to go2sky-plugins repo to see all the plugins, click here.

Supported Environment Variables

Below is the full list of supported environment variables you can set to customize the agent behavior, please read the descriptions for what they can achieve.

Environment VariableDescriptionDefault
SW_AGENT_NAMEThe name of the Go serviceunset
SW_AGENT_LAYERInstance belong layer name which define in the backendunset
SW_AGENT_INSTANCE_NAMEThe name of the Go service instanceRandomly generated
SW_AGENT_SAMPLESample rate, 1 setting it to 1 means full sampling1
SW_AGENT_COLLECTOR_BACKEND_SERVICESThe backend OAP server addressunset
SW_AGENT_AUTHENTICATIONThe authentication token to verify that the agent is trusted by the backend OAP, as for how to configure the backend, refer to the yaml.unset
SW_AGENT_COLLECTOR_HEARTBEAT_PERIODAgent heartbeat report period. Unit, second20
SW_AGENT_COLLECTOR_GET_AGENT_DYNAMIC_CONFIG_INTERVALSniffer get agent dynamic config interval. Unit, second20
SW_AGENT_COLLECTOR_MAX_SEND_QUEUE_SIZESend span queue buffer length30000
SW_AGENT_PROCESS_STATUS_HOOK_ENABLEEnable the Process Status Hook featurefalse
SW_AGENT_PROCESS_LABELSThe labels of the process, multiple labels split by ","unset

CDS - Configuration Discovery Service

Configuration Discovery Service provides the dynamic configuration for the agent, defined in gRPC and stored in the backend.

Available key(s) and value(s) in Golang Agent.

Golang agent supports the following dynamic configurations.

Config KeyValue DescriptionValue Format Example
agent.sample_rateThe percentage of trace when sampling. It's [0, 1], Same with WithSampler parameter.0.1

Process Status Hook

This feature is used in cooperation with the skywalking-rover project.

When go2sky keeps alive with the backend, it would write a metadata file to the local (temporary directory) at the same time, which describes the information of the current process. The rover side scans all processes, find out which process contains this metadata file. Finally, the rover could collect, profiling, with this process.

Metadata File

The metadata file use to save metadata with current process, it save in: {TMPDIR}/apache_skywalking/process/{pid}/metadata.properties.

Also, when the go2sky keep alive with backend, modify and open time of the metadata file would be updated.

KeyTypeDescription
layerstringthis process layer.
service_namestringthis process service name.
instance_namestringthis process instance name.
process_namestringthis process process name, it's same with the instance name.
propertiesjsonthe properties in instance, the process labels also in the properties value.
labelsstringthe process labels, multiple labels split by ",".
languagestringcurrent process language, which is golang.

Please read the official documentation of rover to get more information.

License

Apache License 2.0. See LICENSE file for details.

# Packages

Package log help to build trace context data into log plugins.
No description provided by the author
No description provided by the author
Package propagation holds the required function signatures for Injection and Extraction.
Package reporter holds reporters contain official reporter implementations.
No description provided by the author

# Functions

No description provided by the author
No description provided by the author
GetGlobalTracer returns the registered global Tracer.
No description provided by the author
NewConstSampler creates a ConstSampler.
No description provided by the author
No description provided by the author
NewTracer return a new go2sky Tracer.
No description provided by the author
No description provided by the author
No description provided by the author
SetGlobalTracer registers `tracer` as the global Tracer.
No description provided by the author
No description provided by the author
No description provided by the author
WithContext setup trace sc from propagation.
No description provided by the author
WithCustomSampler setup custom sampler.
WithInstance setup instance identify.
WithOperationName setup span OperationName of a span.
WithReporter setup report pipeline for tracer.
WithSampler setup sampler.
No description provided by the author
WithSpanType setup span type of a span.

# Constants

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
SpanTypeEntry is a entry span, eg http server.
SpanTypeExit is a exit span, eg http client.
SpanTypeLocal is a local span, eg local method invoke.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Structs

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
RandomSampler Use sync.Pool to implement concurrent-safe for randomizer.
SegmentContext is the context in a segment.
Tracer is go2sky tracer implementation.

# Interfaces

No description provided by the author
ReportedSpan is accessed by Reporter to load reported data.
Reporter is a data transit specification.
No description provided by the author
Span interface as commonv3 span specification.

# Type aliases

No description provided by the author
SpanOption allows for functional options to adjust behaviour of a Span to be created by CreateLocalSpan.
SpanType is used to identify entry, exit and local.
Tag are supported by sky-walking engine.
TracerOption allows for functional options to adjust behaviour of a Tracer to be created by NewTracer.