Skip to content

Commit

Permalink
refactor(otel): Update otel submodules #25
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmetcanozcan committed Aug 27, 2024
1 parent 3a7621e commit c3caaab
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 38 deletions.
7 changes: 4 additions & 3 deletions example/client-server-with-otel/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ func main() {
app.Use(
client.Module(),
server.Module(),
otel.Module(),
otelclient.Module(),
otelserver.Module(),
otel.Module(
otelclient.WithClient(),
otelserver.WithServer(),
),
)

app.Provide(
Expand Down
7 changes: 4 additions & 3 deletions example/client-server-with-otel/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ func main() {

app.Use(
server.Module(),
otel.Module(),
otelclient.Module(),
otelserver.Module(),
otel.Module(
otelclient.WithClient(),
otelserver.WithServer(),
),
)

app.Provide(
Expand Down
5 changes: 3 additions & 2 deletions example/consumer-with-otel/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ func main() {
)

app.Use(
otel.Module(),
otelkafka.Module(),
otel.Module(
otelkafka.WithKafka(),
),
kafka.Module(),
)

Expand Down
5 changes: 3 additions & 2 deletions example/server-with-newrelic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ func main() {
)

app.Use(
otel.Module(),
otel.Module(
otelserver.WithServer(),
),
orm.Module(postgresdriver.New()),
otelserver.Module(),
server.Module(),
swagger.Module(),

Expand Down
5 changes: 3 additions & 2 deletions example/server-with-otel/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ func main() {
)

app.Use(
otel.Module(),
otelserver.Module(),
otel.Module(
otelserver.WithServer(),
),
server.Module(),
)

Expand Down
5 changes: 3 additions & 2 deletions example/server-with-postgres/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ func main() {
)

app.Use(
otel.Module(),
otel.Module(
otelserver.WithServer(),
),
orm.Module(postgresdriver.New()),
otelserver.Module(),
server.Module(),
swagger.Module(),
)
Expand Down
8 changes: 4 additions & 4 deletions modules/otel/client/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import (

"github.com/Trendyol/chaki/module"
clientcommon "github.com/Trendyol/chaki/modules/client/common"
otelmodule "github.com/Trendyol/chaki/modules/otel"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"
)

const ModuleName = "chaki-otel-client"

func Module() *module.Module {
return module.New(ModuleName).Provide(newClientRoundTripperWrapper)
func WithClient() otelmodule.Option {
m := module.NewSubModule().Provide(newClientRoundTripperWrapper)
return otelmodule.WithSubModule(m)
}

func newClientRoundTripperWrapper(tp trace.TracerProvider, tmp propagation.TextMapPropagator) clientcommon.RoundTripperWrapper {
Expand Down
11 changes: 4 additions & 7 deletions modules/otel/kafka/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,19 @@ import (
"github.com/Trendyol/chaki/module"
"github.com/Trendyol/chaki/modules/kafka/consumer"
"github.com/Trendyol/chaki/modules/kafka/producer"
otelmodule "github.com/Trendyol/chaki/modules/otel"
"go.opentelemetry.io/otel/trace"
)

const moduleName = "chaki-otel-kafka"

func Module() *module.Module {
m := module.New(moduleName)

m.Provide(
func WithKafka() otelmodule.Option {
m := module.NewSubModule().Provide(
newSpanBuilder,
newConsumerInterceptor,
newProducerInterceptor,
newBatchConsumerInterceptor,
)

return m
return otelmodule.WithSubModule(m)
}

func newConsumerInterceptor(sb *spanBuilder) consumer.Interceptor {
Expand Down
2 changes: 2 additions & 0 deletions modules/otel/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ func Module(opts ...Option) *module.Module {
startOtel,
)

m.Merge(o.subModules...)

return m
}

Expand Down
28 changes: 19 additions & 9 deletions modules/otel/option.go
Original file line number Diff line number Diff line change
@@ -1,39 +1,49 @@
package otel

import "context"
import (
"context"

"github.com/Trendyol/chaki/module"
)

type CloseFunc func(context.Context) error

type InitFunc func() CloseFunc

type options struct {
initFunc InitFunc
initFunc InitFunc
subModules []*module.SubModule
}

type Option interface {
Apply(*options) *options
Apply(*options)
}

type withOption func(*options) *options
type withOption func(*options)

func (wf withOption) Apply(opts *options) *options {
return wf(opts)
func (wf withOption) Apply(opts *options) {
wf(opts)
}

var _ Option = withOption(nil)

func WithInitFunc(f InitFunc) Option {
return withOption(func(o *options) *options {
return withOption(func(o *options) {
o.initFunc = f
return o
})
}

func WithSubModule(sub ...*module.SubModule) Option {
return withOption(func(o *options) {
o.subModules = append(o.subModules, sub...)
})
}

func applyOptions(opts ...Option) *options {
o := &options{}

for _, opt := range opts {
o = opt.Apply(o)
opt.Apply(o)
}

return o
Expand Down
8 changes: 4 additions & 4 deletions modules/otel/server/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ package otelserver

import (
"github.com/Trendyol/chaki/module"
otelmodule "github.com/Trendyol/chaki/modules/otel"
otelcommon "github.com/Trendyol/chaki/modules/otel/common"
servercommon "github.com/Trendyol/chaki/modules/server/common"
"github.com/gofiber/contrib/otelfiber/v2"
"github.com/gofiber/fiber/v2"
"go.opentelemetry.io/otel/trace"
)

const ModuleName = "chaki-otel-server"

func Module() *module.Module {
return module.New(ModuleName).Provide(newMiddlewareGroup)
func WithServer() otelmodule.Option {
m := module.NewSubModule().Provide(newMiddlewareGroup)
return otelmodule.WithSubModule(m)
}

func newMiddlewareGroup() servercommon.MiddlewareGroup {
Expand Down

0 comments on commit c3caaab

Please sign in to comment.