Skip to content

Commit

Permalink
Merge pull request #8 from isd-sgcu/gear-be/fdr-66-be-setup-opentelem…
Browse files Browse the repository at this point in the history
…etry-jeager

Gear be/fdr 66 be setup opentelemetry jeager
  • Loading branch information
macgeargear authored Jul 12, 2024
2 parents a85e8c8 + 2b34e80 commit f59c78f
Show file tree
Hide file tree
Showing 12 changed files with 307 additions and 97 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ COPY --from=builder /app/server ./

ENV GO_ENV production

EXPOSE 3000
EXPOSE 3004

CMD ["./server"]
22 changes: 19 additions & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import (
"github.com/isd-sgcu/rpkm67-checkin/database"
"github.com/isd-sgcu/rpkm67-checkin/internal/checkin"
"github.com/isd-sgcu/rpkm67-checkin/logger"
"github.com/isd-sgcu/rpkm67-checkin/tracer"
checkinProto "github.com/isd-sgcu/rpkm67-go-proto/rpkm67/checkin/checkin/v1"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/health"
Expand All @@ -30,20 +32,34 @@ func main() {

logger := logger.New(conf)

tp, err := tracer.New(conf)
if err != nil {
panic(fmt.Sprintf("Failed to create tracer: %v", err))
}
defer func() {
if err := tp.Shutdown(context.Background()); err != nil {
panic(fmt.Sprintf("Failed to shutdown tracer: %v", err))
}
}()

tracer := tp.Tracer("rpkm67-checkin")

db, err := database.InitDatabase(&conf.Db, conf.App.IsDevelopment())
if err != nil {
panic(fmt.Sprintf("Failed to connect to database: %v", err))
}

checkinRepo := checkin.NewRepository(db)
checkinSvc := checkin.NewService(checkinRepo, logger.Named("checkinSvc"))
checkinRepo := checkin.NewRepository(db, tracer)
checkinSvc := checkin.NewService(checkinRepo, logger.Named("checkinSvc"), tracer)

listener, err := net.Listen("tcp", fmt.Sprintf(":%v", conf.App.Port))
if err != nil {
panic(fmt.Sprintf("Failed to listen: %v", err))
}

grpcServer := grpc.NewServer()
grpcServer := grpc.NewServer(
grpc.StatsHandler(otelgrpc.NewServerHandler()),
)
grpc_health_v1.RegisterHealthServer(grpcServer, health.NewServer())
checkinProto.RegisterCheckInServiceServer(grpcServer, checkinSvc)

Expand Down
28 changes: 20 additions & 8 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,23 @@ import (
)

type AppConfig struct {
Port string
Env string
Port string
Env string
ServiceName string
}

type DbConfig struct {
Url string
}

type TracerConfig struct {
Endpoint string
}

type Config struct {
App AppConfig
Db DbConfig
App AppConfig
Db DbConfig
Tracer TracerConfig
}

func LoadConfig() (*Config, error) {
Expand All @@ -29,17 +35,23 @@ func LoadConfig() (*Config, error) {
}

appConfig := AppConfig{
Port: os.Getenv("APP_PORT"),
Env: os.Getenv("APP_ENV"),
Port: os.Getenv("APP_PORT"),
Env: os.Getenv("APP_ENV"),
ServiceName: os.Getenv("APP_SERVICE_NAME"),
}

dbConfig := DbConfig{
Url: os.Getenv("DB_URL"),
}

tracerConfig := TracerConfig{
Endpoint: os.Getenv("TRACER_ENDPOINT"),
}

return &Config{
App: appConfig,
Db: dbConfig,
App: appConfig,
Db: dbConfig,
Tracer: tracerConfig,
}, nil
}

Expand Down
28 changes: 26 additions & 2 deletions docker-compose.qa.template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ services:
- ./microservices/auth:/app/config/staffs
ports:
- "3002:3002"

backend:
image: ghcr.io/isd-sgcu/rpkm67-backend:latest
container_name: backend
Expand Down Expand Up @@ -128,7 +128,7 @@ services:
container_name: prometheus
restart: unless-stopped
networks:
- rpkm67
- rpkm67
volumes:
- ./microservices/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
ports:
Expand All @@ -147,6 +147,30 @@ services:
ports:
- "3006:3000"

otel-collector:
image: otel/opentelemetry-collector-contrib:0.104.0
command: ["--config=/etc/opentelemetry.yml"]
volumes:
- ./microservices/opentelemetry/opentelemetry.yml:/etc/opentelemetry.yml
ports:
- 4317:4317 # OTLP gRPC receiver
- 4318:4318 # OTLP HTTP receiver
networks:
- rpkm67
depends_on:
- jaeger

jaeger:
image: jaegertracing/all-in-one:1.58
environment:
- COLLECTOR_OTLP_ENABLED=true
ports:
- 9091:16686 # Jaeger UI
- 14250:14250 # Jaeger gRPC collector
- 14268:14268 # Jaeger HTTP collector
networks:
- rpkm67

networks:
rpkm67:
name: rpkm67
16 changes: 14 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,34 @@ require (
github.com/isd-sgcu/rpkm67-go-proto v0.4.6
github.com/isd-sgcu/rpkm67-model v0.0.4
github.com/stretchr/testify v1.9.0
go.opentelemetry.io/otel v1.28.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.28.0
go.opentelemetry.io/otel/sdk v1.28.0
google.golang.org/grpc v1.65.0
gorm.io/gorm v1.25.10
)

require (
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.5.5 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
go.opentelemetry.io/otel/metric v1.28.0 // indirect
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240624140628-dc46fd24d27d // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240701130421-f6361c86f094 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Expand All @@ -36,6 +45,9 @@ require (
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/joho/godotenv v1.5.1
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.53.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.28.0 // indirect
go.opentelemetry.io/otel/trace v1.28.0
go.uber.org/zap v1.27.0
gorm.io/driver/postgres v1.5.9
)
37 changes: 31 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
github.com/bxcodec/faker/v4 v4.0.0-beta.3 h1:gqYNBvN72QtzKkYohNDKQlm+pg+uwBDVMN28nWHS18k=
github.com/bxcodec/faker/v4 v4.0.0-beta.3/go.mod h1:m6+Ch1Lj3fqW/unZmvkXIdxWS5+XQWPWxcbbQW2X+Ho=
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/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
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/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/isd-sgcu/rpkm67-go-proto v0.4.4 h1:ScXOEVS+n5airgje1c3kjtkEzENXEfxwZMb+d5xFfRA=
github.com/isd-sgcu/rpkm67-go-proto v0.4.4/go.mod h1:w+UCeQnJ3wBuJ7Tyf8LiBiPZVb1KlecjMNCB7kBeL7M=
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/isd-sgcu/rpkm67-go-proto v0.4.6 h1:yUBzUY3ftBfnI2x/MT+MsynBOlV5pOR143c9OTrVGuU=
github.com/isd-sgcu/rpkm67-go-proto v0.4.6/go.mod h1:w+UCeQnJ3wBuJ7Tyf8LiBiPZVb1KlecjMNCB7kBeL7M=
github.com/isd-sgcu/rpkm67-model v0.0.4 h1:tk6z6pXnhWBoG2SaSIoyLxNnwRaXwdbSIEEa/cSi8EY=
Expand All @@ -29,8 +36,8 @@ github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand All @@ -43,6 +50,22 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.53.0 h1:9G6E0TXzGFVfTnawRzrPl83iHOAV7L8NJiR8RSGYV1g=
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.53.0/go.mod h1:azvtTADFQJA8mX80jIH/akaE7h+dbm/sVuaHqN13w74=
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/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/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/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
Expand Down Expand Up @@ -81,8 +104,10 @@ golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240624140628-dc46fd24d27d h1:k3zyW3BYYR30e8v3x0bTDdE9vpYFjZHK+HcyqkrppWk=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240624140628-dc46fd24d27d/go.mod h1:Ue6ibwXGpU+dqIcODieyLOcgj7z8+IcskoNIgZxtrFY=
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-20240701130421-f6361c86f094 h1:BwIjyKYGsK9dMCBOorzRri8MQwmi7mT9rGHsCEinZkA=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094/go.mod h1:Ue6ibwXGpU+dqIcODieyLOcgj7z8+IcskoNIgZxtrFY=
google.golang.org/grpc v1.65.0 h1:bs/cUb4lp1G5iImFFd3u5ixQzweKizoZJAwBNLR42lc=
google.golang.org/grpc v1.65.0/go.mod h1:WgYC2ypjlB0EiQi6wdKixMqukr6lBc0Vo+oOgjrM5ZQ=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
Expand Down
31 changes: 22 additions & 9 deletions internal/checkin/checkin.repository.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,45 @@
package checkin

import (
"context"

"github.com/isd-sgcu/rpkm67-model/model"
"go.opentelemetry.io/otel/trace"
"gorm.io/gorm"
)

type Repository interface {
Create(checkIn *model.CheckIn) error
FindByEmail(email string, checkIns *[]*model.CheckIn) error
FindByUserId(userId string, checkIns *[]*model.CheckIn) error
Create(ctx context.Context, checkIn *model.CheckIn) error
FindByEmail(ctx context.Context, email string, checkIns *[]*model.CheckIn) error
FindByUserId(ctx context.Context, userId string, checkIns *[]*model.CheckIn) error
}

type repositoryImpl struct {
Db *gorm.DB
Db *gorm.DB
tracer trace.Tracer
}

func NewRepository(db *gorm.DB) Repository {
return &repositoryImpl{Db: db}
func NewRepository(db *gorm.DB, tracer trace.Tracer) Repository {
return &repositoryImpl{Db: db, tracer: tracer}
}

func (r *repositoryImpl) Create(checkIn *model.CheckIn) error {
func (r *repositoryImpl) Create(ctx context.Context, checkIn *model.CheckIn) error {
_, span := r.tracer.Start(ctx, "repository.checkin.Create")
defer span.End()

return r.Db.Create(checkIn).Error
}

func (r *repositoryImpl) FindByEmail(email string, checkIns *[]*model.CheckIn) error {
func (r *repositoryImpl) FindByEmail(ctx context.Context, email string, checkIns *[]*model.CheckIn) error {
_, span := r.tracer.Start(ctx, "repository.checkin.FindByEmail")
defer span.End()

return r.Db.Where("email = ?", email).Find(&checkIns).Error
}

func (r *repositoryImpl) FindByUserId(userId string, checkIns *[]*model.CheckIn) error {
func (r *repositoryImpl) FindByUserId(ctx context.Context, userId string, checkIns *[]*model.CheckIn) error {
_, span := r.tracer.Start(ctx, "repository.checkin.FindByUserId")
defer span.End()

return r.Db.Where("user_id = ?", userId).Find(&checkIns).Error
}
Loading

0 comments on commit f59c78f

Please sign in to comment.