package
0.0.0-20220316145958-c72030a08b9b
Repository: https://github.com/wavefronthq/wavefront-go-middleware.git
Documentation: pkg.go.dev

# README

Middleware-echoweb

Instrumentation

  1. Create Cfg.yaml file
  2. Create Routes.yaml file
  3. Initailise Config with choice of sender, absolute path of CfgFile and RoutesFile along with EchoWeb Server pointer.
  4. Call init function and pass the Config struct intialised above.
  5. Copy tracer-config.yaml, tracer-routesRegistration.yaml file to container in Dockerfile

Example: tracer-config.yaml file:

cluster* : prod
shard* : 1
application : wavefrontHQ
service : wavefront-middleware
source : VMware
CustomApplicationTags* :
	staticTags:
		customApplicationTagKey : customApplicationTagValue
	jwtClaims**:
		- username
rateSampler* : 10
durationSampler* : 60

*optional parameter If no rate sampler is given it is taken as deterministic sampler ie all traces are sent to wavefront

**Middleware automatically parses jwt token to add the given claims as tags in the span

Example: tracer-routesRegistration.yaml

routesRegistration:
		/tracingapi/test.GET:
				routeSpecificTag: route-tag
				routeSpecifcMetaTags:
					routeSpecifcMetaTagKey: routeSpecifcMetaTagValue

Routes registration format: api-path.HTTP_METHOD

init function:

Using Direct Sender

func init() {
	cfgFile := "filePath of Cfg.yaml file"
	routesFile := "filePath of Routes.yaml file"

	config := new(Config)
	config.CfgFile = cfgFile
	config.RoutesFile= routesFile
	config.echoWeb = *echo.Echo
	config.DirectCfg = senders.DirectConfiguration
	
	//Initialising Global tracer
	err := InitTracer(config)

	if err != nil {
		/*
		Handle Error
		*/
	}
}

Using In-Direct Sender

func init() {
	cfgFile := "filePath of Cfg.yaml file"
	routesFile := "filePath of Routes.yaml file"

	config := new(Config)
	config.CfgFile = cfgFile
	config.RoutesFile= routesFile
	config.echoWeb = *echo.Echo
	config.DirectCfg = senders.ProxyConfiguration
	
	//Initialising Global tracer
	err := InitTracer(config)

	if err != nil {
		/*
		Handle Error
		*/
	}
}

Cross process context propagation

headers:= GetTracingHeadersToInject(c)

c - echo context

GetTracingHeadersToInject func returns headers which can be added to the call when making cross service calls. Context is propagated using headers which helps in stiching trace

Adding dynamic tags to Span

To add metadata derived during servicing the request

AddDynamicTags(c,tags)

c - echo context tags - key value pairs of strings

Contextual Logger

logger:= NewSpanLogger(c)

c - echo context Same usage as default go logger instance given by log lib in go. Ex: logger.Println("Logging") Logs are automatically injected span info including trace id, span id, and parent span id. Logs are sent to wavefront for each call

All the function exposed by standard Go Logger are exposed by custom logger with same usage

Other tracing methods exposed

For manual start of span

serverSpan,parentSpanId,err:= StartTraceSpan(c,operationName,tags)

c - echo context

For injecting headers in default http client request

httpRequest:= InjectTracerHTTP(tracer,span,httpReq)

Returns headers to be injected from span. To be used when manually starting span within the process

tags:= GetTracingHeadersToInjectFromSpan(tracer,span)

c - echo context

Returns headers to be injected from echo context. To be used when using middleware

GetTracingHeadersToInjectFromContext(c)

# Functions

AddDynamicTags injects the dyanmic tags to the span.
GetTracingHeadersToInjectFromContext returns the headerswhich Should be added to the request to inject fromthe echo contextTo be used when using middleware.
GetTracingHeadersToInjectFromSpan returns the headerswhich Should be added to the request to inject fromthe current span.To be used when manually starting span inside process.
InitTracer initialize tracer object, initialize the GlobalTracer with this newly created, tracer object.
InjectTracerHTTP injects the current span into the trace.By Adding the context of the current span to the headers of the Http request.
NewSpanLogger returns a new custom logger instance with TraceId and SpanId injected.It exposes the same functions as the standard Golang Logger, with added function of injecting span info info to the logs.And sending span logs to wavefront.
StartTraceSpan looks for existing context from the headers of the request.If context is found, it starts a child span to the parent span,Else it starts a root span.Tags are added to the respective spans created.If parent span exists parent span id is returned along with child span otherwise root span and "" is returned.
TracingHandler custom echoWeb middleware.Enables tracing for the routes defined in tracer-config.go.Injects respective tags for each route defined in the span of each trace.

# Variables

Tracer Global Tracer.

# Structs

Config stores the middleware config.
SpanLogger custom implementation of Go standard LoggerExposes the same functions as Standard Logger.
Writer custom writer.