All Projects → Nordstrom → ctrace-go

Nordstrom / ctrace-go

Licence: Apache-2.0 License
Canonical OpenTracing for GoLang

Programming Languages

go
31211 projects - #10 most used programming language
Makefile
30231 projects

Projects that are alternatives of or similar to ctrace-go

Jaeger Ui
Web UI for Jaeger
Stars: ✭ 639 (+5225%)
Mutual labels:  tracing, trace, opentracing
zipkin-ruby-opentracing
OpenTracing Tracer implementation for Zipkin in Ruby
Stars: ✭ 15 (+25%)
Mutual labels:  tracing, trace, opentracing
Zipkin Go Opentracing
OpenTracing Bridge for Zipkin Go
Stars: ✭ 472 (+3833.33%)
Mutual labels:  tracing, trace, opentracing
Molten
php probe for zipkin and opentracing
Stars: ✭ 740 (+6066.67%)
Mutual labels:  tracing, trace, opentracing
zipkin-cpp-opentracing
OpenTracing Tracer implementation for Zipkin in C++
Stars: ✭ 46 (+283.33%)
Mutual labels:  tracing, trace, opentracing
printer
A fancy logger yet lightweight, and configurable. 🖨
Stars: ✭ 65 (+441.67%)
Mutual labels:  log, logger, trace
Gf
GoFrame is a modular, powerful, high-performance and enterprise-class application development framework of Golang.
Stars: ✭ 6,501 (+54075%)
Mutual labels:  logger, tracing, opentracing
go-sensor
🚀 Go Distributed Tracing & Metrics Sensor for Instana
Stars: ✭ 90 (+650%)
Mutual labels:  tracing, trace, opentracing
missionlog
🚀 lightweight logging • supports level based filtering and tagging • weighs in at around 500 bytes
Stars: ✭ 19 (+58.33%)
Mutual labels:  log, logger
easeagent
An agent component for the Java system
Stars: ✭ 437 (+3541.67%)
Mutual labels:  tracing, opentracing
java-web-servlet-filter
OpenTracing Java Web Servlet Filter Instrumentation
Stars: ✭ 20 (+66.67%)
Mutual labels:  tracing, opentracing
feign-opentracing
OpenTracing Feign integration
Stars: ✭ 20 (+66.67%)
Mutual labels:  tracing, opentracing
nginx-opentracing
Instrument nginx for OpenTracing.
Stars: ✭ 21 (+75%)
Mutual labels:  tracing, opentracing
axios-opentracing
Axios interceptor which traces your requests 👀
Stars: ✭ 15 (+25%)
Mutual labels:  tracing, opentracing
mongoose-morgan
An npm package for saving morgan log inside MongoDB
Stars: ✭ 14 (+16.67%)
Mutual labels:  log, logger
ptkdev-logger
🦒 Beautiful Logger for Node.js: the best alternative to the console.log statement
Stars: ✭ 117 (+875%)
Mutual labels:  log, logger
Multiplatform-Log
Kotlin Multi Platform Logger, for android an ios : Logcat & print
Stars: ✭ 49 (+308.33%)
Mutual labels:  log, logger
PoShLog
🔩 PoShLog is PowerShell cross-platform logging module. It allows you to log structured event data into console, file and much more places easily. It's built upon great C# logging library Serilog - https://serilog.net/
Stars: ✭ 108 (+800%)
Mutual labels:  log, logger
ruby-sensor
💎 Ruby Distributed Tracing & Metrics Sensor for Instana
Stars: ✭ 23 (+91.67%)
Mutual labels:  tracing, opentracing
spdlog setup
spdlog setup initialization via file configuration for convenience.
Stars: ✭ 68 (+466.67%)
Mutual labels:  log, logger

ctrace-go

GoDoc Build Status Report Card Coverage Status OpenTracing 1.0 Enabled

Canonical OpenTracing for Go

Why

OpenTracing is a young specification and for most (if not all) SDK implementations, output format and wire protocol are specific to the backend platform implementation. ctrace attempts to decouple the format and wire protocol from the backend tracer implementation.

What

ctrace specifies a canonical format for trace logs. By default the logs are output to stdout but you can configure them to go to any WritableStream.

Required Reading

To fully understand this platform API, it's helpful to be familiar with the OpenTracing project project, terminology, and ctrace specification more specifically.

Install

Install via glide as follows:

$ glide get github.com/Nordstrom/ctrace-go

Usage

Add instrumentation to the operations you want to track. Most of this is done by middleware.

Initialization

First import ctrace.

import (
	ctrace "github.com/Nordstrom/ctrace-go"
)

Instrumentation

The primary way to use ctrace-go is to hook in one or more of the auto-instrumentation decorators. These are the decorators we currently support.

  • TracedHttpHandler - this wraps the default serve mux to trace Incoming HTTP Requests.
  • TracedHttpClientTransport - this wraps the http.Transport for tracing Outgoing HTTP Requests.
  • TracedAPIGwLambdaProxyHandler - this wraps AWS Gateway Lambda Proxy handler using AWS Lambda Go Shim to trace requests coming into an AWS Lambda from an API Gateway proxy event.

TracedHttpHandler

To automatically instrument incoming HTTP Requests use the TracedHttpHandler.

import (
	"net/http"

	ctrace "github.com/Nordstrom/ctrace-go"
)

func ok(w http.ResponseWriter, r *http.Request) {
	region := r.URL.Query().Get("region")
	msg := hello(r.Context(), region)
	w.Write([]byte(msg))
}

func main() {
	http.HandleFunc("/ok", ok)
	http.ListenAndServe(":8004", ctrace.TracedHTTPHandler(http.DefaultServeMux))
}

TracedHTTPClientTransport

To automatically instrument outgoing HTTP Requests use the TracedHttpClientTransport.

import (
	"context"
	"net/http"

	ctrace "github.com/Nordstrom/ctrace-go"
)

var httpClient = &http.Client{
	Transport: ctrace.TracedHTTPClientTransport(&http.Transport{}),
}

func send(ctx context.Context, method string, url string, body io.Reader) (*http.Response, error) {
	req, err := http.NewRequest(method, url, body)
	if err != nil {
		return nil, err
	}
	return httpClient.Do(req.WithContext(ctx))
}

TracedAPIGwLambdaProxyHandler

To automatically instrument incoming API Gateway Lambda proxy requests use TracedAPIGwLambdaProxyHandler.

import (
	"github.com/eawsy/aws-lambda-go-core/service/lambda/runtime"
	"github.com/eawsy/aws-lambda-go-event/service/lambda/runtime/event/apigatewayproxyevt"
	ctrace "github.com/Nordstrom/ctrace-go"
)

handler := func(
	ctx context.Context,
	evt *apigatewayproxyevt.Event,
	lambdaCtx *runtime.Context,
) (interface{}, error) {
  // ...
}

var TracedHandler = ctrace.TracedAPIGwLambdaProxyHandler(handler)

Log Events

To log events within the context of the current Span, use span.LogFields.

import (
	ctrace "github.com/Nordstrom/ctrace-go"
	log "github.com/Nordstrom/ctrace-go/log"
)

func hello(ctx context.Context, region string) string {
	msg := fmt.Sprintf("Hello %v!", region)
	ctrace.LogInfo(ctx, "generate-msg", log.Message(message))
	return msg
}

Advanced Usage

If middleware does not fully meet your needs, you can manually instrument spans operations of interest and adding log statements to capture useful data relevant to those operations.

func main() {
	ctrace.Init(TracerOptions{
		MultiEvent: true,
		Writer:     fileWriter,
	})
}

Creating a Span given an existing Go context.Context

If you use context.Context in your application, OpenTracing's Go library will happily rely on it for Span propagation. To start a new (blocking child) Span, you can use StartSpanFromContext.

func xyz(ctx context.Context, ...) {
    // ...
    span, ctx := opentracing.StartSpanFromContext(ctx, "operation_name")
    defer span.Finish()
    span.LogFields(
        log.String("event", "soft error"),
        log.String("type", "cache timeout"),
        log.Int("waited.millis", 1500))
    // ...
}

Starting an empty trace by creating a "root span"

It's always possible to create a "root" Span with no parent or other causal reference.

func xyz() {
   // ...
   sp := opentracing.StartSpan("operation_name")
   defer sp.Finish()
   // ...
}

Best Practices

The following are recommended practices for using opentracing and ctrace-go within a GoLang project.

Context Propagation

Require a context.Context argument as the first parameter of every(a) API call

func (h handler) HandleRequest(ctx context.Context, r MyRequest) error {
  // ...
}

Rule of thumb: because contexts are always request-scopeed, never hold a reference to them on a struct. Always pass as a function parameter.

a. Obviously not every function in your codebase. You'll get a feel for the balance when you start writing context-aware code.

Custom Context Types

At some point, you will be tempted to invent your own "custom" context type. For example to provide convenience methods, since Value() takes an interface{} key and returns an interface{}.

You will regret it. Use extractor functions instead.

Contributing

Please see the contributing guide for details on contributing to ctrace-go.

License

Apache License 2.0

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].