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

Add Opentelemetry and kafka support for otel #7

Merged
merged 7 commits into from
Aug 7, 2024
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
75 changes: 75 additions & 0 deletions example/client-server-with-otel/client/client.go
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
}
79 changes: 79 additions & 0 deletions example/client-server-with-otel/client/main.go
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"),
}
}
16 changes: 16 additions & 0 deletions example/client-server-with-otel/client/request.go
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"`
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
server:
addr: ":8081"

client:
example-client:
baseUrl: "http://localhost:8082"
74 changes: 74 additions & 0 deletions example/client-server-with-otel/server/main.go
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
}
16 changes: 16 additions & 0 deletions example/client-server-with-otel/server/request.go
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"`
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
server:
addr: ":8082"
Empty file.
7 changes: 7 additions & 0 deletions example/consumer-with-otel/config.yaml
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

40 changes: 40 additions & 0 deletions example/consumer-with-otel/main.go
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.
58 changes: 58 additions & 0 deletions example/server-with-otel/main.go
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
}
Loading