-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit ed88369
Showing
6 changed files
with
212 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Created by .ignore support plugin (hsz.mobi) | ||
/.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
## fiber_tracing is middleware for fiber framework | ||
|
||
fiber_tracing Middleware trace requests on [Fiber framework](https://gofiber.io/) with OpenTracing API. | ||
You can use every tracer that implement OpenTracing interface | ||
|
||
### Install | ||
``` | ||
go get -u github.com/gofiber/fiber | ||
go get -u github.com/shareed2k/fiber_tracing | ||
``` | ||
|
||
### Config | ||
| Property | Type | Description | Default | | ||
| :--- | :--- | :--- | :--- | | ||
| Tracer | `opentracing.Tracer` | initializes an opentracing tracer., possible values: `jaeger`, `lightstep`, `instana`, `basictracer-go`, ... | `"&opentracing.NoopTracer{}"` | | ||
| OperationName | `func(*fiber.Ctx) string` | Span operation name | `"HTTP " + ctx.Method() + " URL: " + ctx.Path()` | | ||
| Filter | `func(*fiber.Ctx) bool` | Defines a function to skip middleware. | `nil` | | ||
| Modify | `func(*fiber.Ctx, opentracing.Span)` | Defines a function to edit span like add tags or logs... | `span.SetTag("http.remote_addr", ctx.IP()) ...` | | ||
|
||
### Example | ||
```go | ||
package main | ||
|
||
import ( | ||
"github.com/gofiber/fiber" | ||
"github.com/shareed2k/fiber_tracing" | ||
"github.com/uber/jaeger-client-go/config" | ||
) | ||
|
||
func main() { | ||
app := fiber.New() | ||
|
||
defcfg := config.Configuration{ | ||
ServiceName: "fiber-tracer", | ||
Sampler: &config.SamplerConfig{ | ||
Type: "const", | ||
Param: 1, | ||
}, | ||
Reporter: &config.ReporterConfig{ | ||
LogSpans: true, | ||
BufferFlushInterval: 1 * time.Second, | ||
}, | ||
} | ||
cfg, err := defcfg.FromEnv() | ||
if err != nil { | ||
panic("Could not parse Jaeger env vars: " + err.Error()) | ||
} | ||
tracer, closer, err := cfg.NewTracer() | ||
if err != nil { | ||
panic("Could not initialize jaeger tracer: " + err.Error()) | ||
} | ||
|
||
defer closer.Close() | ||
|
||
app.Use(fiber_tracing.New(fiber_tracing.Config{ | ||
Tracer: tracer, | ||
})) | ||
|
||
app.Get("/", func(c *fiber.Ctx) { | ||
c.Send("Welcome!") | ||
}) | ||
|
||
app.Listen(3000) | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module github.com/shareed2k/fiber_tracing | ||
|
||
go 1.14 | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/gofiber/fiber v1.9.3 | ||
github.com/opentracing/opentracing-go v1.1.0 | ||
github.com/stretchr/testify v1.5.1 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/gofiber/fiber v1.9.3 h1:KOuTZABkLAOQkPfrXhnIaPi7QsMkFClBKK6bLL2f4ZM= | ||
github.com/gofiber/fiber v1.9.3/go.mod h1:o2YQgwJW8+Z16x8MTos4nYn8PD1RJpzu9fojiGqjSjI= | ||
github.com/gorilla/schema v1.1.0 h1:CamqUDOFUBqzrvxuz2vEwo8+SUdwsluFh7IlzJh30LY= | ||
github.com/gorilla/schema v1.1.0/go.mod h1:kgLaKoK1FELgZqMAVxx/5cbj0kT+57qxUrAlIO2eleU= | ||
github.com/klauspost/compress v1.10.4 h1:jFzIFaf586tquEB5EhzQG0HwGNSlgAJpG53G6Ss11wc= | ||
github.com/klauspost/compress v1.10.4/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs= | ||
github.com/opentracing/opentracing-go v1.1.0 h1:pWlfV3Bxv7k65HYwkikxat0+s3pV4bsqf19k25Ur8rU= | ||
github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= | ||
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= | ||
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= | ||
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= | ||
github.com/valyala/fasthttp v1.12.0 h1:TsB9qkSeiMXB40ELWWSRMjlsE+8IkqXHcs01y2d9aw0= | ||
github.com/valyala/fasthttp v1.12.0/go.mod h1:229t1eWu9UXTPmoUkbpN/fctKPBY4IJoFXQnxHGXy6E= | ||
github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a/go.mod h1:v3UYOV9WzVtRmSR+PDvWpU/qWl4Wa5LApYYX4ZtKbio= | ||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= | ||
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= | ||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= | ||
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= | ||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package fiber_tracing | ||
|
||
import ( | ||
"net/http" | ||
"unsafe" | ||
|
||
"github.com/gofiber/fiber" | ||
"github.com/opentracing/opentracing-go" | ||
"github.com/opentracing/opentracing-go/ext" | ||
) | ||
|
||
var getString = func(b []byte) string { | ||
return *(*string)(unsafe.Pointer(&b)) | ||
} | ||
|
||
// Config ... | ||
type Config struct { | ||
// Tracer | ||
// Default: NoopTracer | ||
Tracer opentracing.Tracer | ||
|
||
// OperationName | ||
// Default: func(ctx *fiber.Ctx) string { | ||
// return "HTTP " + ctx.Method() + " URL: " + ctx.Path() | ||
// } | ||
OperationName func(*fiber.Ctx) string | ||
|
||
// Filter defines a function to skip middleware. | ||
// Optional. Default: nil | ||
Filter func(*fiber.Ctx) bool | ||
|
||
// Modify | ||
Modify func(*fiber.Ctx, opentracing.Span) | ||
} | ||
|
||
func New(config ...Config) func(*fiber.Ctx) { | ||
// Init config | ||
var cfg Config | ||
if len(config) > 0 { | ||
cfg = config[0] | ||
} | ||
|
||
if cfg.Tracer == nil { | ||
cfg.Tracer = &opentracing.NoopTracer{} | ||
} | ||
|
||
if cfg.Modify == nil { | ||
cfg.Modify = func(ctx *fiber.Ctx, span opentracing.Span) { | ||
span.SetTag("http.remote_addr", ctx.IP()) | ||
span.SetTag("http.path", ctx.Path()) | ||
span.SetTag("http.host", ctx.Hostname()) | ||
span.SetTag("http.method", ctx.Method()) | ||
span.SetTag("http.url", ctx.OriginalURL()) | ||
} | ||
} | ||
|
||
if cfg.OperationName == nil { | ||
cfg.OperationName = func(ctx *fiber.Ctx) string { | ||
return "HTTP " + ctx.Method() + " URL: " + ctx.Path() | ||
} | ||
} | ||
|
||
return func(ctx *fiber.Ctx) { | ||
// Filter request to skip middleware | ||
if cfg.Filter != nil && cfg.Filter(ctx) { | ||
ctx.Next() | ||
|
||
return | ||
} | ||
|
||
var span opentracing.Span | ||
|
||
opname := cfg.OperationName(ctx) | ||
tr := cfg.Tracer | ||
hdr := make(http.Header) | ||
|
||
ctx.Fasthttp.Request.Header.VisitAll(func(k, v []byte) { | ||
hdr.Set(getString(k), getString(v)) | ||
}) | ||
|
||
if ctx, err := tr.Extract(opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(hdr)); err != nil { | ||
span = tr.StartSpan(opname) | ||
} else { | ||
span = tr.StartSpan(opname, ext.RPCServerOption(ctx)) | ||
} | ||
|
||
cfg.Modify(ctx, span) | ||
|
||
defer func() { | ||
status := ctx.Fasthttp.Response.StatusCode() | ||
|
||
ext.HTTPStatusCode.Set(span, uint16(status)) | ||
|
||
if status >= fiber.StatusInternalServerError { | ||
ext.Error.Set(span, true) | ||
} | ||
|
||
span.Finish() | ||
}() | ||
|
||
ctx.Next() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package fiber_tracing |