diff --git a/adapter/block/scan/wire.go b/adapter/block/scan/wire.go index 267df2e..a9a5147 100644 --- a/adapter/block/scan/wire.go +++ b/adapter/block/scan/wire.go @@ -5,16 +5,31 @@ package scan import ( + "errors" + "fmt" + "github.com/blackhorseya/ryze/adapter/block/wirex" "github.com/blackhorseya/ryze/app/infra/configx" + "github.com/blackhorseya/ryze/app/infra/otelx" "github.com/blackhorseya/ryze/app/infra/transports/httpx" "github.com/blackhorseya/ryze/pkg/adapterx" + "github.com/blackhorseya/ryze/pkg/contextx" "github.com/google/wire" "github.com/spf13/viper" ) func initApplication(config *configx.Configuration) (*configx.Application, error) { - return config.Services["block-scan"], nil + app, ok := config.Services["block-scan"] + if !ok { + return nil, errors.New("[block-scan] service not found") + } + + err := otelx.SetupOTelSDK(contextx.Background(), app) + if err != nil { + return nil, fmt.Errorf("failed to setup OpenTelemetry SDK: %w", err) + } + + return app, nil } func initServer(app *configx.Application) (*httpx.Server, error) { diff --git a/adapter/block/scan/wire_gen.go b/adapter/block/scan/wire_gen.go index ab6d01b..a53459c 100644 --- a/adapter/block/scan/wire_gen.go +++ b/adapter/block/scan/wire_gen.go @@ -7,13 +7,21 @@ package scan import ( + "errors" + "fmt" "github.com/blackhorseya/ryze/adapter/block/wirex" "github.com/blackhorseya/ryze/app/infra/configx" + "github.com/blackhorseya/ryze/app/infra/otelx" "github.com/blackhorseya/ryze/app/infra/transports/httpx" "github.com/blackhorseya/ryze/pkg/adapterx" + "github.com/blackhorseya/ryze/pkg/contextx" "github.com/spf13/viper" ) +import ( + _ "github.com/blackhorseya/ryze/api/block/scan" +) + // Injectors from wire.go: func New(v *viper.Viper) (adapterx.Restful, error) { @@ -40,7 +48,17 @@ func New(v *viper.Viper) (adapterx.Restful, error) { // wire.go: func initApplication(config *configx.Configuration) (*configx.Application, error) { - return config.Services["block-scan"], nil + app, ok := config.Services["block-scan"] + if !ok { + return nil, errors.New("[block-scan] service not found") + } + + err := otelx.SetupOTelSDK(contextx.Background(), app) + if err != nil { + return nil, fmt.Errorf("failed to setup OpenTelemetry SDK: %w", err) + } + + return app, nil } func initServer(app *configx.Application) (*httpx.Server, error) { diff --git a/app/infra/configx/app.go b/app/infra/configx/app.go index eed2260..6042d24 100644 --- a/app/infra/configx/app.go +++ b/app/infra/configx/app.go @@ -6,5 +6,11 @@ import ( // Application is the application configuration. type Application struct { + Name string `json:"name" yaml:"name"` + HTTP httpx.Options `json:"http" yaml:"http"` + + OTel struct { + Target string `json:"target" yaml:"target"` + } `json:"otel" yaml:"otel"` } diff --git a/app/infra/otelx/otelx.go b/app/infra/otelx/otelx.go new file mode 100644 index 0000000..46789e7 --- /dev/null +++ b/app/infra/otelx/otelx.go @@ -0,0 +1,150 @@ +package otelx + +import ( + "context" + "errors" + "fmt" + "time" + + "github.com/blackhorseya/ryze/app/infra/configx" + "github.com/blackhorseya/ryze/pkg/contextx" + "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc" + "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc" + "go.opentelemetry.io/otel/propagation" + sdkmetric "go.opentelemetry.io/otel/sdk/metric" + "go.opentelemetry.io/otel/sdk/resource" + sdktrace "go.opentelemetry.io/otel/sdk/trace" + semconv "go.opentelemetry.io/otel/semconv/v1.4.0" + "go.opentelemetry.io/otel/trace" + "go.uber.org/zap" + "google.golang.org/grpc" + "google.golang.org/grpc/credentials/insecure" +) + +var ( + // Tracer is the global tracer. + Tracer = otel.Tracer("") + + // Meter is the global meter. + Meter = otel.Meter("") +) + +// Shutdown is the function to shutdown the OpenTelemetry SDK. +var Shutdown = func(context.Context) error { + return nil +} + +// SetupOTelSDK sets up the OpenTelemetry SDK with the Jaeger exporter. +func SetupOTelSDK(ctx contextx.Contextx, app *configx.Application) (err error) { + if app.OTel.Target == "" { + ctx.Warn("OpenTelemetry is disabled") + return nil + } + + ctx.Info( + "setting up OpenTelemetry SDK", + zap.String("service_name", app.Name), + zap.String("otlp", app.OTel.Target), + ) + + var shutdownFuncs []func(context.Context) error + + Shutdown = func(ctx context.Context) error { + for _, fn := range shutdownFuncs { + err = errors.Join(err, fn(ctx)) + } + shutdownFuncs = nil + return err + } + + res, err := resource.New(ctx, resource.WithAttributes(semconv.ServiceNameKey.String(app.Name))) + if err != nil { + return fmt.Errorf("failed to create resource: %w", err) + } + + conn, err := initConn(app) + if err != nil { + return err + } + + tracerProvider, err := newTracer(ctx, res, conn, app) + if err != nil { + return err + } + shutdownFuncs = append(shutdownFuncs, tracerProvider.Shutdown) + + meterProvider, err := newMeter(ctx, res, conn, app) + if err != nil { + return err + } + shutdownFuncs = append(shutdownFuncs, meterProvider.Shutdown) + + return nil +} + +func initConn(app *configx.Application) (*grpc.ClientConn, error) { + conn, err := grpc.NewClient(app.OTel.Target, grpc.WithTransportCredentials(insecure.NewCredentials())) + if err != nil { + return nil, fmt.Errorf("failed to create gRPC client: %w", err) + } + + return conn, nil +} + +func newTracer( + ctx context.Context, + res *resource.Resource, + conn *grpc.ClientConn, + app *configx.Application, +) (*sdktrace.TracerProvider, error) { + exporter, err := otlptracegrpc.New(ctx, otlptracegrpc.WithGRPCConn(conn)) + if err != nil { + return nil, fmt.Errorf("failed to create the Jaeger exporter: %w", err) + } + + processor := sdktrace.NewBatchSpanProcessor(exporter) + provider := sdktrace.NewTracerProvider( + sdktrace.WithSampler(sdktrace.AlwaysSample()), + sdktrace.WithResource(res), + sdktrace.WithSpanProcessor(processor), + ) + otel.SetTracerProvider(provider) + + otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator( + propagation.TraceContext{}, + propagation.Baggage{}, + )) + + Tracer = provider.Tracer(app.Name) + + return provider, nil +} + +func newMeter( + ctx context.Context, + res *resource.Resource, + conn *grpc.ClientConn, + app *configx.Application, +) (p *sdkmetric.MeterProvider, err error) { + exporter, err := otlpmetricgrpc.New(ctx, otlpmetricgrpc.WithGRPCConn(conn)) + if err != nil { + return nil, fmt.Errorf("failed to create the OTLP exporter: %w", err) + } + + provider := sdkmetric.NewMeterProvider( + sdkmetric.WithReader(sdkmetric.NewPeriodicReader(exporter, sdkmetric.WithInterval(3*time.Second))), + sdkmetric.WithResource(res), + ) + otel.SetMeterProvider(provider) + + Meter = provider.Meter(app.Name) + + return provider, nil +} + +// Span is used to start a span. +func Span(parent contextx.Contextx, name string) (ctx contextx.Contextx, span trace.Span) { + next, span := Tracer.Start(parent, name) + return contextx.WithContext(next), span +} diff --git a/configs/example.yaml b/configs/example.yaml index 0ac9750..916de6b 100644 --- a/configs/example.yaml +++ b/configs/example.yaml @@ -2,6 +2,17 @@ log: level: debug format: console +services: + block-scan: + name: dev-block-scan + + http: + port: 1992 + mode: debug + + otel: + target: "localhost:4317" + networks: ton: name: ton diff --git a/go.mod b/go.mod index 98668f8..ed5bd14 100644 --- a/go.mod +++ b/go.mod @@ -14,6 +14,12 @@ require ( github.com/swaggo/swag v1.16.3 github.com/xssnick/tonutils-go v1.9.9 go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin v0.53.0 + go.opentelemetry.io/otel v1.28.0 + go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.28.0 + go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.28.0 + go.opentelemetry.io/otel/sdk v1.28.0 + go.opentelemetry.io/otel/sdk/metric v1.28.0 + go.opentelemetry.io/otel/trace v1.28.0 go.uber.org/mock v0.4.0 go.uber.org/zap v1.27.0 google.golang.org/grpc v1.65.0 @@ -24,6 +30,7 @@ require ( github.com/KyleBanks/depth v1.2.1 // indirect github.com/bytedance/sonic v1.11.9 // indirect github.com/bytedance/sonic/loader v0.1.1 // indirect + github.com/cenkalti/backoff/v4 v4.3.0 // indirect github.com/cloudwego/base64x v0.1.4 // indirect github.com/cloudwego/iasm v0.2.0 // indirect github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect @@ -40,6 +47,8 @@ require ( github.com/go-playground/universal-translator v0.18.1 // indirect github.com/go-playground/validator/v10 v10.22.0 // indirect github.com/goccy/go-json v0.10.3 // indirect + github.com/google/uuid v1.6.0 // indirect + github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 // indirect github.com/hashicorp/hcl v1.0.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/josharian/intern v1.0.0 // indirect @@ -66,9 +75,9 @@ require ( github.com/subosito/gotenv v1.6.0 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/ugorji/go/codec v1.2.12 // indirect - go.opentelemetry.io/otel v1.28.0 // indirect + go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.28.0 // indirect go.opentelemetry.io/otel/metric v1.28.0 // indirect - go.opentelemetry.io/otel/trace v1.28.0 // indirect + go.opentelemetry.io/proto/otlp v1.3.1 // indirect go.uber.org/multierr v1.11.0 // indirect golang.org/x/arch v0.8.0 // indirect golang.org/x/crypto v0.25.0 // indirect @@ -77,6 +86,7 @@ require ( golang.org/x/sys v0.22.0 // indirect golang.org/x/text v0.16.0 // indirect golang.org/x/tools v0.23.0 // indirect + google.golang.org/genproto/googleapis/api v0.0.0-20240701130421-f6361c86f094 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20240725223205-93522f1f2a9f // indirect gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum index ecb9c44..7659388 100644 --- a/go.sum +++ b/go.sum @@ -4,6 +4,8 @@ github.com/bytedance/sonic v1.11.9 h1:LFHENlIY/SLzDWverzdOvgMztTxcfcF+cqNsz9pK5z github.com/bytedance/sonic v1.11.9/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4= github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM= github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= +github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8= +github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE= github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y= github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w= github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg= @@ -55,8 +57,12 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/wire v0.6.0 h1:HBkoIh4BdSxoyo9PveV8giw7ZsaBOvzWKfcg/6MrVwI= github.com/google/wire v0.6.0/go.mod h1:F4QhpQ9EDIdJ1Mbop/NZBRB+5yrR6qg3BnctaoUk6NA= +github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 h1:bkypFPDjIYGfCYD5mRBvpqxfYX1YCS1PXdKYWi8FsN0= +github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0/go.mod h1:P+Lt/0by1T8bfcF3z737NnSbmxQAppXMRziHUxPOC8k= github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= @@ -95,8 +101,8 @@ github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= -github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= +github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8= +github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/sagikazarmark/locafero v0.6.0 h1:ON7AQg37yzcRPU69mt7gwhFEBwxI6P9T4Qu3N51bwOk= github.com/sagikazarmark/locafero v0.6.0/go.mod h1:77OmuIc6VTraTXKXIs/uvUxKGUXjE1GbemJYHqdNjX0= @@ -150,10 +156,22 @@ go.opentelemetry.io/contrib/propagators/b3 v1.28.0 h1:XR6CFQrQ/ttAYmTBX2loUEFGdk go.opentelemetry.io/contrib/propagators/b3 v1.28.0/go.mod h1:DWRkzJONLquRz7OJPh2rRbZ7MugQj62rk7g6HRnEqh0= go.opentelemetry.io/otel v1.28.0 h1:/SqNcYk+idO0CxKEUOtKQClMK/MimZihKYMruSMViUo= go.opentelemetry.io/otel v1.28.0/go.mod h1:q68ijF8Fc8CnMHKyzqL6akLO46ePnjkgfIMIjUIX9z4= +go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.28.0 h1:U2guen0GhqH8o/G2un8f/aG/y++OuW6MyCo6hT9prXk= +go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.28.0/go.mod h1:yeGZANgEcpdx/WK0IvvRFC+2oLiMS2u4L/0Rj2M2Qr0= +go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.28.0 h1:3Q/xZUyC1BBkualc9ROb4G8qkH90LXEIICcs5zv1OYY= +go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.28.0/go.mod h1:s75jGIWA9OfCMzF0xr+ZgfrB5FEbbV7UuYo32ahUiFI= +go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.28.0 h1:R3X6ZXmNPRR8ul6i3WgFURCHzaXjHdm0karRG/+dj3s= +go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.28.0/go.mod h1:QWFXnDavXWwMx2EEcZsf3yxgEKAqsxQ+Syjp+seyInw= go.opentelemetry.io/otel/metric v1.28.0 h1:f0HGvSl1KRAU1DLgLGFjrwVyismPlnuU6JD6bOeuA5Q= go.opentelemetry.io/otel/metric v1.28.0/go.mod h1:Fb1eVBFZmLVTMb6PPohq3TO9IIhUisDsbJoL/+uQW4s= +go.opentelemetry.io/otel/sdk v1.28.0 h1:b9d7hIry8yZsgtbmM0DKyPWMMUMlK9NEKuIG4aBqWyE= +go.opentelemetry.io/otel/sdk v1.28.0/go.mod h1:oYj7ClPUA7Iw3m+r7GeEjz0qckQRJK2B8zjcZEfu7Pg= +go.opentelemetry.io/otel/sdk/metric v1.28.0 h1:OkuaKgKrgAbYrrY0t92c+cC+2F6hsFNnCQArXCKlg08= +go.opentelemetry.io/otel/sdk/metric v1.28.0/go.mod h1:cWPjykihLAPvXKi4iZc1dpER3Jdq2Z0YLse3moQUCpg= go.opentelemetry.io/otel/trace v1.28.0 h1:GhQ9cUuQGmNDd5BTCP2dAvv75RdMxEfTmYejp+lkx9g= go.opentelemetry.io/otel/trace v1.28.0/go.mod h1:jPyXzNPg6da9+38HEwElrQiHlVMTnVfM3/yv2OlIHaI= +go.opentelemetry.io/proto/otlp v1.3.1 h1:TrMUixzpM0yuc/znrFTP9MMRh8trP93mkCiDVeXrui0= +go.opentelemetry.io/proto/otlp v1.3.1/go.mod h1:0X1WI4de4ZsLrrJNLAQbFeLCm3T7yBkR0XqQ7niQU+8= go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= go.uber.org/mock v0.4.0 h1:VcM4ZOtdbR4f6VXfiOpwpVJDL6lCReaZ6mw31wqh7KU= @@ -232,6 +250,8 @@ golang.org/x/tools v0.17.0/go.mod h1:xsh6VxdV005rRVaS6SSAf9oiAqljS7UZUacMZ8Bnsps golang.org/x/tools v0.23.0 h1:SGsXPZ+2l4JsgaCKkx+FQ9YZ5XEtA1GZYuoDjenLjvg= golang.org/x/tools v0.23.0/go.mod h1:pnu6ufv6vQkll6szChhK3C3L/ruaIv5eBeztNG8wtsI= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/genproto/googleapis/api v0.0.0-20240701130421-f6361c86f094 h1:0+ozOGcrp+Y8Aq8TLNN2Aliibms5LEzsq99ZZmAGYm0= +google.golang.org/genproto/googleapis/api v0.0.0-20240701130421-f6361c86f094/go.mod h1:fJ/e3If/Q67Mj99hin0hMhiNyCRmt6BQ2aWIJshUSJw= google.golang.org/genproto/googleapis/rpc v0.0.0-20240725223205-93522f1f2a9f h1:RARaIm8pxYuxyNPbBQf5igT7XdOyCNtat1qAT2ZxjU4= google.golang.org/genproto/googleapis/rpc v0.0.0-20240725223205-93522f1f2a9f/go.mod h1:Ue6ibwXGpU+dqIcODieyLOcgj7z8+IcskoNIgZxtrFY= google.golang.org/grpc v1.65.0 h1:bs/cUb4lp1G5iImFFd3u5ixQzweKizoZJAwBNLR42lc=