-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from Trendyol/dist-tracing
Add Opentelemetry and kafka support for otel
- Loading branch information
Showing
28 changed files
with
1,220 additions
and
29 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,75 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/Trendyol/chaki/modules/client" | ||
"github.com/Trendyol/chaki/modules/server/response" | ||
) | ||
|
||
type exampleClient struct { | ||
*client.Base | ||
} | ||
|
||
func newClient(f *client.Factory) *exampleClient { | ||
return &exampleClient{ | ||
Base: f.Get("example-client", client.DefaultErrDecoder), | ||
} | ||
} | ||
|
||
func (cl *exampleClient) SendHello(ctx context.Context) (string, error) { | ||
resp := &response.Response[string]{} | ||
if _, err := cl.Request(ctx).SetResult(resp).Get("/hello"); err != nil { | ||
return "", err | ||
} | ||
|
||
return resp.Data, nil | ||
} | ||
|
||
func (cl *exampleClient) sendGreetWithQuery(ctx context.Context, req GreetWithQueryRequest) (string, error) { | ||
resp := &response.Response[string]{} | ||
|
||
params := map[string]string{ | ||
"text": req.Text, | ||
"repeatTimes": fmt.Sprintf("%d", req.RepeatTimes), | ||
} | ||
|
||
if _, err := cl.Request(ctx). | ||
SetResult(resp). | ||
SetQueryParams(params). | ||
Get("/hello/query"); err != nil { | ||
return "", err | ||
} | ||
return resp.Data, nil | ||
} | ||
|
||
func (cl *exampleClient) sendGreetWithParam(ctx context.Context, req GreetWithParamRequest) (string, error) { | ||
resp := &response.Response[string]{} | ||
|
||
url := fmt.Sprintf("/hello/param/%s", req.Text) | ||
params := map[string]string{ | ||
"repeatTimes": fmt.Sprintf("%d", req.RepeatTimes), | ||
} | ||
|
||
if _, err := cl.Request(ctx). | ||
SetResult(resp). | ||
SetQueryParams(params). | ||
Get(url); err != nil { | ||
return "", err | ||
} | ||
|
||
return resp.Data, nil | ||
} | ||
|
||
func (cl *exampleClient) sendGreetWithBody(ctx context.Context, req GreetWithBodyRequest) (string, error) { | ||
resp := &response.Response[string]{} | ||
|
||
if _, err := cl.Request(ctx). | ||
SetResult(resp). | ||
SetBody(req). | ||
Post("/hello/body"); err != nil { | ||
return "", err | ||
} | ||
|
||
return resp.Data, nil | ||
} |
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,79 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/Trendyol/chaki" | ||
"github.com/Trendyol/chaki/logger" | ||
"github.com/Trendyol/chaki/modules/client" | ||
"github.com/Trendyol/chaki/modules/otel" | ||
otelclient "github.com/Trendyol/chaki/modules/otel/client" | ||
otelserver "github.com/Trendyol/chaki/modules/otel/server" | ||
|
||
"github.com/Trendyol/chaki/modules/server" | ||
"github.com/Trendyol/chaki/modules/server/controller" | ||
"github.com/Trendyol/chaki/modules/server/route" | ||
) | ||
|
||
func main() { | ||
app := chaki.New() | ||
|
||
app.Use( | ||
client.Module(), | ||
server.Module(), | ||
otel.Module(), | ||
otelclient.Module(), | ||
otelserver.Module(), | ||
) | ||
|
||
app.Provide( | ||
newClient, | ||
NewController, | ||
) | ||
|
||
if err := app.Start(); err != nil { | ||
logger.Fatal(err) | ||
} | ||
} | ||
|
||
type serverController struct { | ||
*controller.Base | ||
cl *exampleClient | ||
} | ||
|
||
func NewController(cl *exampleClient) controller.Controller { | ||
return &serverController{ | ||
Base: controller.New("server-controller").SetPrefix("/hello"), | ||
cl: cl, | ||
} | ||
} | ||
|
||
func (ct *serverController) hello(ctx context.Context, _ any) (string, error) { | ||
logger.From(ctx).Info("hello") | ||
resp, err := ct.cl.SendHello(ctx) | ||
if err != nil { | ||
return "", err | ||
} | ||
return resp, nil | ||
} | ||
|
||
func (ct *serverController) greetWithBody(ctx context.Context, req GreetWithBodyRequest) (string, error) { | ||
return ct.cl.sendGreetWithBody(ctx, req) | ||
} | ||
|
||
func (ct *serverController) greetWithQuery(ctx context.Context, req GreetWithQueryRequest) (string, error) { | ||
return ct.cl.sendGreetWithQuery(ctx, req) | ||
} | ||
|
||
func (ct *serverController) greetWithParam(ctx context.Context, req GreetWithParamRequest) (string, error) { | ||
return ct.cl.sendGreetWithParam(ctx, req) | ||
} | ||
|
||
func (ct *serverController) Routes() []route.Route { | ||
return []route.Route{ | ||
route.Get("", ct.hello), | ||
route.Get("/query", ct.greetWithQuery).Name("Greet with query"), | ||
route.Get("/param/:text", ct.greetWithParam).Name("Greet with param"), | ||
route.Post("/body", ct.greetWithBody).Name("Greet with body"), | ||
} | ||
} |
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,16 @@ | ||
package main | ||
|
||
type GreetWithBodyRequest struct { | ||
Text string `json:"text" validate:"required,min=3,max=100"` | ||
RepeatTimes int `json:"repeatTimes" validate:"required,gte=1,lte=5"` | ||
} | ||
|
||
type GreetWithQueryRequest struct { | ||
Text string `query:"text" validate:"required,min=3,max=100"` | ||
RepeatTimes int `query:"repeatTimes" validate:"required,gte=1,lte=5"` | ||
} | ||
|
||
type GreetWithParamRequest struct { | ||
Text string `param:"text" validate:"required,min=3,max=100"` | ||
RepeatTimes int `query:"repeatTimes" validate:"required,gte=1,lte=5"` | ||
} |
6 changes: 6 additions & 0 deletions
6
example/client-server-with-otel/client/resources/configs/application.yaml
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,6 @@ | ||
server: | ||
addr: ":8081" | ||
|
||
client: | ||
example-client: | ||
baseUrl: "http://localhost:8082" |
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,74 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/Trendyol/chaki" | ||
"github.com/Trendyol/chaki/logger" | ||
"github.com/Trendyol/chaki/modules/otel" | ||
otelclient "github.com/Trendyol/chaki/modules/otel/client" | ||
otelserver "github.com/Trendyol/chaki/modules/otel/server" | ||
"github.com/Trendyol/chaki/modules/server" | ||
"github.com/Trendyol/chaki/modules/server/controller" | ||
"github.com/Trendyol/chaki/modules/server/route" | ||
) | ||
|
||
func main() { | ||
app := chaki.New() | ||
|
||
app.Use( | ||
server.Module(), | ||
otel.Module(), | ||
otelclient.Module(), | ||
otelserver.Module(), | ||
) | ||
|
||
app.Provide( | ||
NewController, | ||
) | ||
|
||
if err := app.Start(); err != nil { | ||
logger.Fatal(err) | ||
} | ||
|
||
} | ||
|
||
type serverController struct { | ||
*controller.Base | ||
} | ||
|
||
func NewController() controller.Controller { | ||
return &serverController{ | ||
Base: controller.New("server-controller").SetPrefix("/hello"), | ||
} | ||
} | ||
|
||
func (ct *serverController) hello(ctx context.Context, _ any) (string, error) { | ||
logger.From(ctx).Info("hello from server") | ||
return "Hi From Server", nil | ||
} | ||
|
||
func (ct *serverController) Routes() []route.Route { | ||
return []route.Route{ | ||
route.Get("/", ct.hello), | ||
route.Get("/greet", ct.greetHandler).Name("Greet Route"), | ||
route.Get("/query", ct.greetWithQuery).Name("Greet with query"), | ||
route.Get("/param/:text", ct.greetWithParam).Name("Greet with param"), | ||
route.Post("/body", ct.greetWithBody).Name("Greet with body")} | ||
} | ||
|
||
func (ct *serverController) greetHandler(_ context.Context, _ struct{}) (string, error) { | ||
return "Greetings!", nil | ||
} | ||
|
||
func (ct *serverController) greetWithBody(_ context.Context, req GreetWithBodyRequest) (string, error) { | ||
return req.Text, nil | ||
} | ||
|
||
func (ct *serverController) greetWithQuery(_ context.Context, req GreetWithQueryRequest) (string, error) { | ||
return req.Text, nil | ||
} | ||
|
||
func (ct *serverController) greetWithParam(_ context.Context, req GreetWithParamRequest) (string, error) { | ||
return req.Text, nil | ||
} |
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,16 @@ | ||
package main | ||
|
||
type GreetWithBodyRequest struct { | ||
Text string `json:"text" validate:"required,min=3,max=100"` | ||
RepeatTimes int `json:"repeatTimes" validate:"required,gte=1,lte=5"` | ||
} | ||
|
||
type GreetWithQueryRequest struct { | ||
Text string `query:"text" validate:"required,min=3,max=100"` | ||
RepeatTimes int `query:"repeatTimes" validate:"required,gte=1,lte=5"` | ||
} | ||
|
||
type GreetWithParamRequest struct { | ||
Text string `param:"text" validate:"required,min=3,max=100"` | ||
RepeatTimes int `query:"repeatTimes" validate:"required,gte=1,lte=5"` | ||
} |
2 changes: 2 additions & 0 deletions
2
example/client-server-with-otel/server/resources/configs/application.yaml
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 @@ | ||
server: | ||
addr: ":8082" |
Empty file.
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,7 @@ | ||
kafka: | ||
consumer: | ||
example: | ||
brokers: "broker,broker" | ||
topic: test | ||
groupId: example.groupId | ||
|
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,40 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/Trendyol/chaki" | ||
"github.com/Trendyol/chaki/logger" | ||
"github.com/Trendyol/chaki/modules/kafka" | ||
"github.com/Trendyol/chaki/modules/kafka/consumer" | ||
"github.com/Trendyol/chaki/modules/otel" | ||
otelkafka "github.com/Trendyol/chaki/modules/otel/kafka" | ||
) | ||
|
||
func main() { | ||
app := chaki.New() | ||
|
||
app.WithOption( | ||
chaki.WithConfigPath("config.yaml"), | ||
) | ||
|
||
app.Use( | ||
otel.Module(), | ||
otelkafka.Module(), | ||
kafka.Module(), | ||
) | ||
|
||
app.Provide( | ||
newConsumer, | ||
) | ||
|
||
logger.Fatal(app.Start()) | ||
|
||
} | ||
|
||
func newConsumer() consumer.Consumer { | ||
return consumer.NewFn("example", func(msg *consumer.Message) error { | ||
// otel span defined in context | ||
// logger can log spanId and traceId | ||
logger.From(msg.Context).Info("message here") | ||
return nil | ||
}) | ||
} |
Empty file.
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,58 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/Trendyol/chaki" | ||
"github.com/Trendyol/chaki/logger" | ||
"github.com/Trendyol/chaki/modules/otel" | ||
otelserver "github.com/Trendyol/chaki/modules/otel/server" | ||
"github.com/Trendyol/chaki/modules/server" | ||
"github.com/Trendyol/chaki/modules/server/controller" | ||
"github.com/Trendyol/chaki/modules/server/middlewares" | ||
"github.com/Trendyol/chaki/modules/server/route" | ||
) | ||
|
||
func main() { | ||
app := chaki.New() | ||
|
||
app.WithOption( | ||
chaki.WithConfigPath("config.yaml"), | ||
) | ||
|
||
app.Use( | ||
otel.Module(), | ||
otelserver.Module(), | ||
server.Module(), | ||
) | ||
|
||
app.Provide( | ||
middlewares.ErrHandler, | ||
newHelloController, | ||
) | ||
|
||
if err := app.Start(); err != nil { | ||
logger.Fatal(err) | ||
} | ||
} | ||
|
||
type HelloController struct { | ||
*controller.Base | ||
} | ||
|
||
func newHelloController() controller.Controller { | ||
return &HelloController{ | ||
Base: controller.New("Hello Controller").SetPrefix("/hello"), | ||
} | ||
} | ||
|
||
func (c *HelloController) Routes() []route.Route { | ||
return []route.Route{ | ||
route.Get("/greet", c.greetHandler).Name("Greet Route"), | ||
} | ||
} | ||
|
||
func (c *HelloController) greetHandler(ctx context.Context, _ any) (string, error) { | ||
logger.From(ctx).Info("trace id and spand id will be logged wiht message") | ||
return "Greetings!", nil | ||
} |
Oops, something went wrong.