-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enhance: customizable config, code cleanup, middleware
- Loading branch information
Showing
19 changed files
with
401 additions
and
111 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
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
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
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
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
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
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
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,30 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package middleware | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
tracingMiddleware "github.com/go-vela/server/router/middleware/tracing" | ||
"github.com/go-vela/server/tracing" | ||
"go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin" | ||
) | ||
|
||
// TracingClient is a middleware function that attaches the tracing config | ||
// to the context of every http.Request. | ||
func TracingClient(tc *tracing.Client) gin.HandlerFunc { | ||
return func(c *gin.Context) { | ||
tracingMiddleware.ToContext(c, tc) | ||
|
||
c.Next() | ||
} | ||
} | ||
|
||
// TracingInstrumentation is a middleware function that attaches the tracing config | ||
// to the context of every http.Request. | ||
func TracingInstrumentation(tc *tracing.Client) gin.HandlerFunc { | ||
if tc.EnableTracing { | ||
return otelgin.Middleware(tc.ServiceName, otelgin.WithTracerProvider(tc.TracerProvider)) | ||
} | ||
|
||
return func(c *gin.Context) {} | ||
Check failure on line 29 in router/middleware/tracing.go
|
||
} |
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,37 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package tracing | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/go-vela/server/tracing" | ||
) | ||
|
||
const key = "tracing" | ||
|
||
// Setter defines a context that enables setting values. | ||
type Setter interface { | ||
Set(string, interface{}) | ||
} | ||
|
||
// FromContext returns the associated value with this context. | ||
func FromContext(c context.Context) *tracing.Client { | ||
value := c.Value(key) | ||
if value == nil { | ||
return nil | ||
} | ||
|
||
tc, ok := value.(*tracing.Client) | ||
if !ok { | ||
return nil | ||
} | ||
|
||
return tc | ||
} | ||
|
||
// ToContext adds the value to this context if it supports | ||
// the Setter interface. | ||
func ToContext(c Setter, tc *tracing.Client) { | ||
c.Set(key, tc) | ||
} |
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,95 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package tracing | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/gin-gonic/gin" | ||
Check failure on line 8 in router/middleware/tracing/context_test.go
|
||
"github.com/go-vela/server/tracing" | ||
) | ||
|
||
func TestTracing_FromContext(t *testing.T) { | ||
// setup types | ||
serviceName := "vela-test" | ||
want := &tracing.Client{ | ||
Config: tracing.Config{ | ||
ServiceName: serviceName, | ||
}, | ||
} | ||
|
||
// setup context | ||
gin.SetMode(gin.TestMode) | ||
context, _ := gin.CreateTestContext(nil) | ||
context.Set(key, want) | ||
|
||
// run test | ||
got := FromContext(context) | ||
|
||
if got != want { | ||
t.Errorf("FromContext is %v, want %v", got, want) | ||
} | ||
} | ||
|
||
func TestTracing_FromContext_Bad(t *testing.T) { | ||
// setup context | ||
gin.SetMode(gin.TestMode) | ||
context, _ := gin.CreateTestContext(nil) | ||
context.Set(key, nil) | ||
|
||
// run test | ||
got := FromContext(context) | ||
|
||
if got != nil { | ||
t.Errorf("FromContext is %v, want nil", got) | ||
} | ||
} | ||
|
||
func TestTracing_FromContext_WrongType(t *testing.T) { | ||
// setup context | ||
gin.SetMode(gin.TestMode) | ||
context, _ := gin.CreateTestContext(nil) | ||
context.Set(key, 1) | ||
|
||
// run test | ||
got := FromContext(context) | ||
|
||
if got != nil { | ||
t.Errorf("FromContext is %v, want nil", got) | ||
} | ||
} | ||
|
||
func TestTracing_FromContext_Empty(t *testing.T) { | ||
// setup context | ||
gin.SetMode(gin.TestMode) | ||
context, _ := gin.CreateTestContext(nil) | ||
|
||
// run test | ||
got := FromContext(context) | ||
|
||
if got != nil { | ||
t.Errorf("FromContext is %v, want nil", got) | ||
} | ||
} | ||
|
||
func TestTracing_ToContext(t *testing.T) { | ||
// setup types | ||
serviceName := "vela-test" | ||
want := &tracing.Client{ | ||
Config: tracing.Config{ | ||
ServiceName: serviceName, | ||
}, | ||
} | ||
|
||
// setup context | ||
gin.SetMode(gin.TestMode) | ||
context, _ := gin.CreateTestContext(nil) | ||
ToContext(context, want) | ||
|
||
// run test | ||
got := context.Value(key) | ||
|
||
if got != want { | ||
t.Errorf("ToContext is %v, want %v", got, want) | ||
} | ||
} |
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 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// Package tracing provides the ability for inserting | ||
// or extracting the Vela OTEL tracing client | ||
// from the middleware chain for the API. | ||
// | ||
// Usage: | ||
// | ||
// import "github.com/go-vela/server/router/middleware/tracing" | ||
package tracing |
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,28 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package tracing | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"github.com/sirupsen/logrus" | ||
|
||
"github.com/go-vela/server/tracing" | ||
) | ||
|
||
// Retrieve gets the value in the given context. | ||
func Retrieve(c *gin.Context) *tracing.Client { | ||
return FromContext(c) | ||
} | ||
|
||
// Establish sets the value in the given context. | ||
func Establish() gin.HandlerFunc { | ||
return func(c *gin.Context) { | ||
l := c.MustGet("logger").(*logrus.Entry) | ||
tc := Retrieve(c) | ||
|
||
l.Debugf("reading tracing client from context") | ||
|
||
ToContext(c, tc) | ||
c.Next() | ||
} | ||
} |
Oops, something went wrong.