Skip to content

Commit

Permalink
Lambda middleware (#9)
Browse files Browse the repository at this point in the history
* lambda middleware wip

* major rework, with lambda support

* fix lint
  • Loading branch information
andyday committed Jun 18, 2017
1 parent 24d30da commit e860eb0
Show file tree
Hide file tree
Showing 46 changed files with 1,477 additions and 822 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ build
*.coverprofile

*.env

*.prof
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ HTTP_PROXY = ${http_proxy}
HTTPS_PROXY = ${https_proxy}

# Many Go tools take file globs or directories as arguments instead of packages.
PKG_FILES ?= *.go http ext log demo
PKG_FILES ?= *.go core ext log demo

# The linting tools evolve with each Go version, so run them only on the latest
# stable release.
Expand Down
25 changes: 16 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,29 @@ $ glide get github.com/Nordstrom/ctrace-go
## Usage
Add instrumentation to the operations you want to track. Most of this is done by middleware.

### Singleton Initialization
First initialize the Global Tracer Singleton as early as possible.
### Initialization
First import ctrace.

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

func main() {
opentracing.InitGlobalTracer(ctrace.New())
This initializes the global Tracer with default settings so you can call
```go
tracer := ctrace.Global()
```

// ...
If you need to initialize the global tracer with customizations you will need to
call the following early in the code. For example you can call it in main as follows.

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

Expand All @@ -45,9 +55,7 @@ To automatically instrument incoming HTTP Requests use the TracedHandler.
import (
"net/http"

ctrace "github.com/Nordstrom/ctrace-go"
chttp "github.com/Nordstrom/ctrace-go/http"
opentracing "github.com/opentracing/opentracing-go"
)

func ok(w http.ResponseWriter, r *http.Request) {
Expand All @@ -57,7 +65,6 @@ func ok(w http.ResponseWriter, r *http.Request) {
}

func main() {
opentracing.InitGlobalTracer(ctrace.New())
http.HandleFunc("/ok", ok)
http.ListenAndServe(":8004", chttp.TracedHandler(http.DefaultServeMux))
}
Expand Down
6 changes: 1 addition & 5 deletions acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ import (
opentracing "github.com/opentracing/opentracing-go"
)

func lines(buf bytes.Buffer) []string {
return strings.Split(buf.String(), "\n")
}

var _ = Describe("Acceptance", func() {

var (
Expand All @@ -27,7 +23,7 @@ var _ = Describe("Acceptance", func() {

BeforeEach(func() {
buf.Reset()
tracer = ctrace.NewWithOptions(ctrace.TracerOptions{Writer: &buf, MultiEvent: true})
tracer = ctrace.Init(ctrace.TracerOptions{Writer: &buf, MultiEvent: true})
})

Describe("Parent and Child with Standard Tags", func() {
Expand Down
11 changes: 6 additions & 5 deletions bench_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package ctrace
package ctrace_test

import (
"os"
"testing"

ctrace "github.com/Nordstrom/ctrace-go"
"github.com/Nordstrom/ctrace-go/ext"
opentracing "github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/log"
Expand All @@ -14,7 +15,7 @@ func BenchmarkParentChildLog(b *testing.B) {
if err != nil {
return
}
t := NewWithOptions(TracerOptions{Writer: f})
t := ctrace.Init(ctrace.TracerOptions{Writer: f})
b.ResetTimer()
for i := 0; i < b.N; i++ {
parent := t.StartSpan("parent",
Expand Down Expand Up @@ -48,7 +49,7 @@ func BenchmarkParent(b *testing.B) {
if err != nil {
return
}
t := NewWithOptions(TracerOptions{Writer: f})
t := ctrace.Init(ctrace.TracerOptions{Writer: f})
b.ResetTimer()
for i := 0; i < b.N; i++ {
parent := t.StartSpan("parent",
Expand All @@ -70,7 +71,7 @@ func BenchmarkChild(b *testing.B) {
if err != nil {
return
}
t := NewWithOptions(TracerOptions{Writer: f})
t := ctrace.Init(ctrace.TracerOptions{Writer: f})
b.ResetTimer()
parent := t.StartSpan("parent",
ext.SpanKindServer(),
Expand Down Expand Up @@ -104,7 +105,7 @@ func BenchmarkLog(b *testing.B) {
if err != nil {
return
}
t := NewWithOptions(TracerOptions{Writer: f})
t := ctrace.Init(ctrace.TracerOptions{Writer: f})
b.ResetTimer()
parent := t.StartSpan("parent",
ext.SpanKindServer(),
Expand Down
5 changes: 3 additions & 2 deletions concurrency_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package ctrace
package ctrace_test

import (
"bytes"
"sync"

ctrace "github.com/Nordstrom/ctrace-go"
. "github.com/onsi/ginkgo"
opentracing "github.com/opentracing/opentracing-go"
)
Expand All @@ -13,7 +14,7 @@ const op = "test"
var _ = Describe("Concurrency", func() {
It("usage", func() {
var buf bytes.Buffer
trc := NewWithOptions(TracerOptions{
trc := ctrace.Init(ctrace.TracerOptions{
Writer: &buf,
DebugAssertSingleGoroutine: true,
})
Expand Down
38 changes: 0 additions & 38 deletions context.go

This file was deleted.

6 changes: 3 additions & 3 deletions http/http_suite_test.go → core/core_suite_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package http_test
package core_test

import (
. "github.com/onsi/ginkgo"
Expand All @@ -7,7 +7,7 @@ import (
"testing"
)

func TestHttp(t *testing.T) {
func TestCore(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Http Suite")
RunSpecs(t, "Core Suite")
}
2 changes: 1 addition & 1 deletion debug.go → core/debug.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ctrace
package core

import (
"bytes"
Expand Down
2 changes: 1 addition & 1 deletion json_encoder.go → core/json_encoder.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ctrace
package core

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion json_encoder_test.go → core/json_encoder_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ctrace
package core

import (
. "github.com/onsi/ginkgo"
Expand Down
41 changes: 40 additions & 1 deletion propagation.go → core/propagation.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ctrace
package core

import (
"net/url"
Expand All @@ -8,6 +8,45 @@ import (
opentracing "github.com/opentracing/opentracing-go"
)

const (
// HTTPHeaders represents SpanContexts as HTTP header string pairs.
//
// Unlike TextMap, the HTTPHeaders format requires that the keys and values
// be valid as HTTP headers as-is (i.e., character casing may be unstable
// and special characters are disallowed in keys, values should be
// URL-escaped, etc).
//
// For Tracer.Inject(): the carrier must be a `TextMapWriter`.
//
// For Tracer.Extract(): the carrier must be a `TextMapReader`.
//
// See HTTPHeaderCarrier for an implementation of both TextMapWriter
// and TextMapReader that defers to an http.Header instance for storage.
// For example, Inject():
//
// carrier := ctrace.HTTPHeadersCarrier(httpReq.Header)
// err := span.Tracer().Inject(
// span, ctrace.HTTPHeaders, carrier)
//
// Or Extract():
//
// carrier := ctrace.HTTPHeadersCarrier(httpReq.Header)
// span, err := tracer.Extract(
// ctrace.HTTPHeaders, carrier)
//
HTTPHeaders = opentracing.HTTPHeaders

// TextMap represents SpanContexts as key:value string pairs.
//
// Unlike HTTPHeaders, the TextMap format does not restrict the key or
// value character sets in any way.
//
// For Tracer.Inject(): the carrier must be a `TextMapWriter`.
//
// For Tracer.Extract(): the carrier must be a `TextMapReader`.
TextMap = opentracing.TextMap
)

// Injector is responsible for injecting SpanContext instances in a manner suitable
// for propagation via a format-specific "carrier" object. Typically the
// injection will take place across an RPC boundary, but message queues and
Expand Down
2 changes: 1 addition & 1 deletion propagation_test.go → core/propagation_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ctrace_test
package core_test

// func TestSpanPropagator(t *testing.T) {
// const op = "test"
Expand Down

0 comments on commit e860eb0

Please sign in to comment.