Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support julienschmidt/httprouter #332

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions _integration-tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ require (
github.com/hashicorp/vault/api v1.15.0
github.com/jackc/pgx/v5 v5.7.1
github.com/jinzhu/gorm v1.9.16
github.com/julienschmidt/httprouter v1.3.0
github.com/labstack/echo/v4 v4.12.0
github.com/mattn/go-sqlite3 v1.14.22
github.com/redis/go-redis/v9 v9.7.0
Expand Down
1 change: 1 addition & 0 deletions _integration-tests/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1429,6 +1429,7 @@ github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHm
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/juju/qthttptest v0.1.1/go.mod h1:aTlAv8TYaflIiTDIQYzxnl1QdPjAg8Q8qJMErpKy6A4=
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/jung-kurt/gofpdf v1.0.0/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes=
github.com/jung-kurt/gofpdf v1.0.3-0.20190309125859-24315acbbda5/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes=
Expand Down
19 changes: 19 additions & 0 deletions _integration-tests/tests/julienschmidt_httprouter/gen_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2023-present Datadog, Inc.

//go:build integration

package julienschmidt_httprouter

import (
"context"
"fmt"
"io"
"net/http"
"testing"
"time"

"github.com/julienschmidt/httprouter"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"datadoghq.dev/orchestrion/_integration-tests/utils"
"datadoghq.dev/orchestrion/_integration-tests/validator/trace"
)

type TestCase struct {
*http.Server
}

func (tc *TestCase) Setup(t *testing.T) {
router := httprouter.New()
router.GET("/ping", func(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, err := io.WriteString(w, `{"message": "pong"}`)
assert.NoError(t, err)
})
tc.Server = &http.Server{
rarguelloF marked this conversation as resolved.
Show resolved Hide resolved
Addr: "127.0.0.1:" + utils.GetFreePort(t),
Handler: router,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
}
go func() { assert.ErrorIs(t, tc.Server.ListenAndServe(), http.ErrServerClosed) }()
}

func (tc *TestCase) Run(t *testing.T) {
resp, err := http.Get(fmt.Sprintf("http://%s/ping", tc.Server.Addr))
require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode)
}

func (tc *TestCase) Teardown(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
require.NoError(t, tc.Server.Shutdown(ctx))
}

func (*TestCase) ExpectedTraces() trace.Traces {
return trace.Traces{
{
Tags: map[string]any{
"name": "http.request",
"resource": "GET /ping",
"type": "http",
"service": "julienschmidt_httprouter.test",
},
Meta: map[string]string{
"component": "net/http",
"span.kind": "client",
},
Children: trace.Traces{
{
Tags: map[string]any{
"name": "http.request",
"resource": "GET /ping",
"type": "web",
"service": "julienschmidt_httprouter.test",
},
Meta: map[string]string{
"component": "net/http",
"span.kind": "server",
},
Children: trace.Traces{
{
Tags: map[string]any{
"name": "http.request",
"resource": "GET /ping",
"type": "web",
"service": "http.router",
},
Meta: map[string]string{
"component": "julienschmidt/httprouter",
"span.kind": "server",
},
},
},
},
},
},
}
}
48 changes: 42 additions & 6 deletions _integration-tests/tests/mux/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,12 @@ type TestCase struct {
}

func (tc *TestCase) Setup(t *testing.T) {
mux := mux.NewRouter()
router := mux.NewRouter()
tc.Server = &http.Server{
Addr: "127.0.0.1:" + utils.GetFreePort(t),
Handler: mux,
Handler: router,
}

mux.HandleFunc("/ping", func(w http.ResponseWriter, _ *http.Request) {
router.HandleFunc("/ping", func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, err := io.WriteString(w, `{"message": "pong"}`)
Expand Down Expand Up @@ -65,19 +64,56 @@ func (tc *TestCase) ExpectedTraces() trace.Traces {
"name": "http.request",
"resource": "GET /ping",
"type": "http",
"service": "mux.test",
},
Meta: map[string]string{
"http.url": url,
"http.url": url,
"component": "net/http",
"span.kind": "client",
},
Children: trace.Traces{
{
Tags: map[string]any{
"name": "http.request",
"resource": "GET /ping",
"type": "web",
"service": "mux.test",
},
Meta: map[string]string{
"http.url": url,
"http.url": url,
"component": "net/http",
"span.kind": "server",
},
Children: trace.Traces{
{
Tags: map[string]any{
"name": "http.request",
"resource": "GET /ping",
"type": "web",
"service": "mux.router",
},
Meta: map[string]string{
"http.url": url,
"component": "gorilla/mux",
"span.kind": "server",
},
Children: trace.Traces{
{
// FIXME: this span shouldn't exist
Tags: map[string]any{
"name": "http.request",
"resource": "GET /ping",
"type": "web",
"service": "mux.router",
},
Meta: map[string]string{
"http.url": url,
"component": "net/http",
"span.kind": "server",
},
},
},
},
},
},
},
Expand Down
32 changes: 31 additions & 1 deletion internal/injector/builtin/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions internal/injector/builtin/generated_deps.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Unless explicitly stated otherwise all files in this repository are licensed
# under the Apache License Version 2.0.
# This product includes software developed at Datadog (https://www.datadoghq.com/).
# Copyright 2023-present Datadog, Inc.
---
# yaml-language-server: $schema=../../../../../docs/static/schema.json
meta:
name: github.com/julienschmidt/httprouter
description: A high performance HTTP request router that scales well.
icon: at-symbol

aspects:
- id: Add fields to httprouter.Router
join-point:
struct-definition: github.com/julienschmidt/httprouter.Router
advice:
- inject-declarations:
imports:
tracing: "gopkg.in/DataDog/dd-trace-go.v1/contrib/julienschmidt/httprouter/internal/tracing"
lang: go1.18
template: |-
type __dd_wRouter struct {
*Router
}

func __dd_wrapRouter(r *Router) tracing.Router {
return &__dd_wRouter{r}
}

func (w __dd_wRouter) Lookup(method string, path string) (any, []tracing.Param, bool) {
h, params, ok := w.Router.Lookup(method, path)
return h, __dd_wrapParams(params), ok
}

type __dd_wParam struct {
Param
}

func __dd_wrapParams(params Params) []tracing.Param {
wParams := make([]tracing.Param, len(params))
for i, p := range params {
wParams[i] = __dd_wParam{p}
}
return wParams
}

func (w __dd_wParam) GetKey() string {
return w.Key
}

func (w __dd_wParam) GetValue() string {
return w.Value
}

func __dd_init(r *Router) {
if r.__dd_config != nil {
return
}
r.__dd_config = tracing.NewConfig()
return
}

- add-struct-field:
name: __dd_config
type: "*gopkg.in/DataDog/dd-trace-go.v1/contrib/julienschmidt/httprouter/internal/tracing.Config"

- id: Trace httprouter.Router#ServeHTTP
join-point:
function-body:
function:
- receiver: "*github.com/julienschmidt/httprouter.Router"
- name: ServeHTTP
advice:
- prepend-statements:
imports:
tracing: "gopkg.in/DataDog/dd-trace-go.v1/contrib/julienschmidt/httprouter/internal/tracing"
template: |-
{{- $r := .Function.Receiver -}}
{{- $w := .Function.Argument 0 -}}
{{- $req := .Function.Argument 1 -}}
__dd_init({{ $r }})

tw, treq, afterHandle, handled := tracing.BeforeHandle({{ $r }}.__dd_config, {{ $r }}, __dd_wrapRouter, {{ $w }}, {{ $req }})
{{ $w }} = tw
{{ $req }} = treq
defer afterHandle()
if handled {
return
}