Skip to content

Commit

Permalink
feat: support github.com/twitchtv/twirp (#352)
Browse files Browse the repository at this point in the history
Adds support for `github.com/twitchtv/twirp`

---------

Co-authored-by: Romain Marcadier <[email protected]>
  • Loading branch information
rarguelloF and RomainMuller authored Oct 31, 2024
1 parent 35b17c0 commit ddeab4f
Show file tree
Hide file tree
Showing 14 changed files with 347 additions and 55 deletions.
1 change: 1 addition & 0 deletions _integration-tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ require (
github.com/testcontainers/testcontainers-go/modules/redis v0.34.0
github.com/testcontainers/testcontainers-go/modules/vault v0.34.0
github.com/tinylib/msgp v1.2.2
github.com/twitchtv/twirp v8.1.3+incompatible
github.com/vektah/gqlparser/v2 v2.5.18
github.com/xlab/treeprint v1.2.0
go.mongodb.org/mongo-driver v1.17.1
Expand Down
2 changes: 2 additions & 0 deletions _integration-tests/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1643,6 +1643,8 @@ github.com/tklauser/go-sysconf v0.3.14 h1:g5vzr9iPFFz24v2KZXs/pvpvh8/V9Fw6vQK5ZZ
github.com/tklauser/go-sysconf v0.3.14/go.mod h1:1ym4lWMLUOhuBOPGtRcJm7tEGX4SCYNEEEtghGG/8uY=
github.com/tklauser/numcpus v0.9.0 h1:lmyCHtANi8aRUgkckBgoDk1nHCux3n2cgkJLXdQGPDo=
github.com/tklauser/numcpus v0.9.0/go.mod h1:SN6Nq1O3VychhC1npsWostA+oW+VOQTxZrS604NSRyI=
github.com/twitchtv/twirp v8.1.3+incompatible h1:+F4TdErPgSUbMZMwp13Q/KgDVuI7HJXP61mNV3/7iuU=
github.com/twitchtv/twirp v8.1.3+incompatible/go.mod h1:RRJoFSAmTEh2weEqWtpPE3vFK5YBhA6bqp2l1kfCC5A=
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
Expand Down
19 changes: 19 additions & 0 deletions _integration-tests/tests/twirp/gen_test.go

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

119 changes: 119 additions & 0 deletions _integration-tests/tests/twirp/twirp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// 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 twirp

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

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/twitchtv/twirp"
"github.com/twitchtv/twirp/example"

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

type TestCase struct {
srv *http.Server
client example.Haberdasher
addr string
}

func (tc *TestCase) Setup(t *testing.T) {
lis, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)

tc.addr = "http://" + lis.Addr().String()
handler := example.NewHaberdasherServer(&randomHaberdasher{})
tc.srv = &http.Server{Handler: handler}

go func() {
assert.ErrorIs(t, tc.srv.Serve(lis), http.ErrServerClosed)
}()
tc.client = example.NewHaberdasherJSONClient(tc.addr, http.DefaultClient)
}

func (tc *TestCase) Run(t *testing.T) {
ctx := context.Background()
_, err := tc.client.MakeHat(ctx, &example.Size{Inches: 6})
require.NoError(t, err)
}

func (tc *TestCase) Teardown(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

require.NoError(t, tc.srv.Shutdown(ctx))
}

func (*TestCase) ExpectedTraces() trace.Traces {
return trace.Traces{
{
Tags: map[string]any{
"name": "http.request",
"service": "twirp.test",
"resource": "POST /twirp/twitch.twirp.example.Haberdasher/MakeHat",
"type": "http",
},
Meta: map[string]string{
"component": "net/http",
"span.kind": "client",
},
Children: trace.Traces{
{
Tags: map[string]any{
"name": "http.request",
"service": "twirp.test",
"resource": "POST /twirp/twitch.twirp.example.Haberdasher/MakeHat",
"type": "web",
},
Meta: map[string]string{
"component": "net/http",
"span.kind": "server",
},
Children: trace.Traces{
{
Tags: map[string]any{
"name": "twirp.Haberdasher",
"service": "twirp-server",
"resource": "MakeHat",
"type": "web",
},
Meta: map[string]string{
"component": "twitchtv/twirp",
"rpc.system": "twirp",
"rpc.service": "Haberdasher",
"rpc.method": "MakeHat",
"twirp.method": "MakeHat",
"twirp.package": "twitch.twirp.example",
"twirp.service": "Haberdasher",
},
},
},
},
},
},
}
}

type randomHaberdasher struct{}

func (*randomHaberdasher) MakeHat(_ context.Context, size *example.Size) (*example.Hat, error) {
if size.Inches <= 0 {
return nil, twirp.InvalidArgumentError("Inches", "I can't make a hat that small!")
}
return &example.Hat{
Size: size.Inches,
Color: "blue",
Name: "top hat",
}, nil
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ require (
github.com/tinylib/msgp v1.2.2 // indirect
github.com/tklauser/go-sysconf v0.3.14 // indirect
github.com/tklauser/numcpus v0.9.0 // indirect
github.com/twitchtv/twirp v8.1.3+incompatible // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,8 @@ github.com/tklauser/go-sysconf v0.3.14 h1:g5vzr9iPFFz24v2KZXs/pvpvh8/V9Fw6vQK5ZZ
github.com/tklauser/go-sysconf v0.3.14/go.mod h1:1ym4lWMLUOhuBOPGtRcJm7tEGX4SCYNEEEtghGG/8uY=
github.com/tklauser/numcpus v0.9.0 h1:lmyCHtANi8aRUgkckBgoDk1nHCux3n2cgkJLXdQGPDo=
github.com/tklauser/numcpus v0.9.0/go.mod h1:SN6Nq1O3VychhC1npsWostA+oW+VOQTxZrS604NSRyI=
github.com/twitchtv/twirp v8.1.3+incompatible h1:+F4TdErPgSUbMZMwp13Q/KgDVuI7HJXP61mNV3/7iuU=
github.com/twitchtv/twirp v8.1.3+incompatible/go.mod h1:RRJoFSAmTEh2weEqWtpPE3vFK5YBhA6bqp2l1kfCC5A=
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
Expand Down
123 changes: 69 additions & 54 deletions 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.

49 changes: 49 additions & 0 deletions internal/injector/builtin/testdata/server/twirp.go.snap

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
Expand Up @@ -3,7 +3,7 @@
# This product includes software developed at Datadog (https://www.datadoghq.com/).
# Copyright 2023-present Datadog, Inc.
---
# yaml-language-server: $schema=../../../../docs/static/schema.json
# yaml-language-server: $schema=../../../../../docs/static/schema.json
meta:
name: google.golang.org/grpc
description: The Go implementation of gRPC.
Expand Down
Loading

0 comments on commit ddeab4f

Please sign in to comment.