Skip to content

Commit

Permalink
Describe trace context propagation in the README
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Slotin committed Mar 11, 2020
1 parent f806b74 commit 1765429
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,36 @@ The Instana Go sensor consists of two parts:

The Instana Go sensor offers a set of quick features to support tracing of the most common operations like handling HTTP requests and executing HTTP requests.

To create an instance of the Instana sensor just request a new instance using the `instana.NewSensor` factory method and providing the name of the application. It is recommended to use a single Instana only. The sensor implementation is fully thread-safe and can be shared by multiple threads.
To create an instance of the Instana sensor just request a new instance using the `instana.NewSensor` factory method and providing the name of the application. It is recommended to use a single instance only. The sensor implementation is fully thread-safe and can be shared by multiple threads.

```go
var sensor = instana.NewSensor("my-service")
```

A full example can be found under the examples folder in [example/webserver/instana/http.go](./example/webserver/instana/http.go).

### Trace Context Propagation

Instana Go sensor provides an API to propagate the trace context throughout the call chain:

```go
func MyFunc(ctx context.Context) {
var spanOpts []ot.StartSpanOption

// retrieve parent span from context and reference it in the new one
if parent, ok := instana.SpanFromContext(); ok {
spanOpts = append(spanOpts, ot.ChildOf(parent.Context()))
}

// start a new span
span := tracer.StartSpan("my-func", spanOpts...)
defer span.Finish()

// and use it as a new parent inside the context
SubCall(instana.ContextWithSpan(ctx, span))
}
```

### HTTP Server Handlers

With support to wrap a `http.HandlerFunc`, Instana quickly adds the possibility to trace requests and collect child spans, executed in the context of the request span.
Expand Down Expand Up @@ -60,7 +82,8 @@ func main() {
// Accessing the parent request inside a handler
func myHandler(w http.ResponseWriter, req *http.Request) {
ctx := req.Context()
parentSpan := ctx.Value("parentSpan").(ot.Span) // use this TracingHttpRequest
parent, _ := instana.SpanFromContext(ctx))

tracer := parent.Tracer()
spanCtx := parent.Context().(instana.SpanContext)
traceID := spanCtx.TraceID // use this with EumSnippet
Expand Down

0 comments on commit 1765429

Please sign in to comment.