copy code and remove dependency on fiber-otel

This commit is contained in:
Natalia Goc 2024-05-21 07:54:22 +02:00
parent 7dd3dc70d5
commit d7b45a1439
3 changed files with 104 additions and 14 deletions

View File

@ -1,5 +1,5 @@
GET http://127.0.0.1:3344/ GET http://127.0.0.1:3344/
HTTP 500 HTTP 500
GET http://127.0.0.1:3344/just/some/more/complex/path/:with/params GET http://127.0.0.1:3344/just/some/more/complex/path/jjj/params
HTTP 500 HTTP 500

View File

@ -6,7 +6,6 @@ import (
"git.ma-al.com/maal-libraries/observer/pkg/exporters" "git.ma-al.com/maal-libraries/observer/pkg/exporters"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
fiberOpentelemetry "github.com/psmarcin/fiber-opentelemetry/pkg/fiber-otel"
"go.opentelemetry.io/otel" "go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk/resource" "go.opentelemetry.io/otel/sdk/resource"
@ -25,6 +24,7 @@ type Config struct {
Version string Version string
// Name of an organization providing the service // Name of an organization providing the service
ServiceProvider string ServiceProvider string
Exporters []exporters.TraceExporter Exporters []exporters.TraceExporter
} }
@ -40,22 +40,23 @@ func newResource(config Config) *resource.Resource {
// NOTE: You can use [trace.WithAttributes] as a parameter to opts argument // NOTE: You can use [trace.WithAttributes] as a parameter to opts argument
func Start(ctx context.Context, spanName string, opts ...trace.SpanStartOption) (context.Context, trace.Span) { func Start(ctx context.Context, spanName string, opts ...trace.SpanStartOption) (context.Context, trace.Span) {
return fiberOpentelemetry.Tracer.Start(ctx, spanName, opts...) return Tracer.Start(ctx, spanName, opts...)
} }
// NOTE: You can use [trace.WithAttributes] as a parameter to opts argument // NOTE: You can use [trace.WithAttributes] as a parameter to opts argument
// Returns [c.UserContext] as [context.Context] // Returns [c.UserContext] as [context.Context]
func FStart(c *fiber.Ctx, opts ...trace.SpanStartOption) (context.Context, trace.Span) { func FStart(c *fiber.Ctx, opts ...trace.SpanStartOption) (context.Context, trace.Span) {
span := trace.SpanFromContext(fiberOpentelemetry.FromCtx(c)) return Tracer.Start(c.UserContext(), c.Method()+" "+c.Route().Path, opts...)
c.SetUserContext(trace.ContextWithSpan(c.UserContext(), span)) }
return fiberOpentelemetry.Tracer.Start(c.UserContext(), c.Method()+" "+c.Route().Path, opts...)
// Just like [FStart] but makes it possible to assign custom span name.
func FStartRenamed(c *fiber.Ctx, spanName string, opts ...trace.SpanStartOption) (context.Context, trace.Span) {
return Tracer.Start(c.UserContext(), spanName, opts...)
} }
// Retrieve span using [fiber.Ctx] // Retrieve span using [fiber.Ctx]
func SpanFromContext(c *fiber.Ctx) trace.Span { func SpanFromContext(c *fiber.Ctx) trace.Span {
ctx := fiberOpentelemetry.FromCtx(c) return trace.SpanFromContext(c.UserContext())
c.SetUserContext(trace.ContextWithSpan(c.UserContext(), trace.SpanFromContext(ctx)))
return trace.SpanFromContext(ctx)
} }
func NewMiddleware(config Config) func(*fiber.Ctx) error { func NewMiddleware(config Config) func(*fiber.Ctx) error {
@ -78,10 +79,9 @@ func NewMiddleware(config Config) func(*fiber.Ctx) error {
tracer := TP.Tracer("_maal-fiber-otel_") tracer := TP.Tracer("_maal-fiber-otel_")
return fiberOpentelemetry.New( return new(
fiberOpentelemetry.Config{ middlewareConfig{
Tracer: tracer, Tracer: tracer,
SpanName: "{{ .Method }} {{ .Route.Path }}",
TracerStartAttributes: []trace.SpanStartOption{ TracerStartAttributes: []trace.SpanStartOption{
trace.WithSpanKind(trace.SpanKindServer), trace.WithSpanKind(trace.SpanKindServer),
trace.WithNewRoot(), trace.WithNewRoot(),

View File

@ -0,0 +1,90 @@
package fiber_tracing
// This was copied from "github.com/psmarcin/fiber-opentelemetry/pkg/fiber-otel"
// and slighltly modified but this piece of code is yet to be fully integrated
// into the package.
import (
"github.com/gofiber/fiber/v2"
"go.opentelemetry.io/otel"
semconv "go.opentelemetry.io/otel/semconv/v1.25.0"
"go.opentelemetry.io/otel/trace"
)
var Tracer = otel.Tracer("fiber_tracing_middleware")
// Config defines the config for middleware.
type middlewareConfig struct {
Tracer trace.Tracer
TracerStartAttributes []trace.SpanStartOption
}
// ConfigDefault is the default config
var configDefault = middlewareConfig{
Tracer: Tracer,
TracerStartAttributes: []trace.SpanStartOption{
trace.WithNewRoot(),
},
}
// Helper function to set default values
func configDefaults(config ...middlewareConfig) middlewareConfig {
// Return default config if nothing provided
if len(config) < 1 {
return configDefault
}
// Override default config
cfg := config[0]
if len(cfg.TracerStartAttributes) == 0 {
cfg.TracerStartAttributes = configDefault.TracerStartAttributes
}
return cfg
}
func new(config ...middlewareConfig) fiber.Handler {
// Set default config
cfg := configDefaults(config...)
// Return new handler
return func(c *fiber.Ctx) error {
spanStartAttributes := trace.WithAttributes(
semconv.HTTPMethod(c.Method()),
semconv.HTTPTarget(string(c.Request().RequestURI())),
semconv.HTTPRoute(c.Route().Path),
semconv.HTTPURL(c.OriginalURL()),
semconv.HostIP(c.IP()),
semconv.HTTPUserAgent(string(c.Request().Header.UserAgent())),
semconv.HTTPRequestContentLength(c.Request().Header.ContentLength()),
semconv.HTTPScheme(c.Protocol()),
semconv.NetTransportTCP,
)
spanKind := trace.WithSpanKind(trace.SpanKindServer)
opts := []trace.SpanStartOption{
spanStartAttributes,
spanKind,
}
opts = append(opts, cfg.TracerStartAttributes...)
otelCtx, span := Tracer.Start(
c.UserContext(),
c.Method()+" "+c.Route().Path,
opts...,
)
c.SetUserContext(otelCtx)
defer span.End()
err := c.Next()
statusCode := c.Response().StatusCode()
attrs := semconv.HTTPResponseStatusCode(statusCode)
span.SetAttributes(attrs)
return err
}
}