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

fix: remove otel #11

Merged
merged 1 commit into from
Jul 15, 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
27 changes: 14 additions & 13 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ 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"
Expand All @@ -32,25 +31,27 @@ 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))
}
}()
// 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")
// 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, tracer)
checkinSvc := checkin.NewService(checkinRepo, logger.Named("checkinSvc"), tracer)
// checkinRepo := checkin.NewRepository(db, tracer)
// checkinSvc := checkin.NewService(checkinRepo, logger.Named("checkinSvc"), tracer)
checkinRepo := checkin.NewRepository(db)
checkinSvc := checkin.NewService(checkinRepo, logger.Named("checkinSvc"))

listener, err := net.Listen("tcp", fmt.Sprintf(":%v", conf.App.Port))
if err != nil {
Expand Down
21 changes: 10 additions & 11 deletions internal/checkin/checkin.repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"

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

Expand All @@ -15,31 +14,31 @@ type Repository interface {
}

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

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

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

return r.Db.Create(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()
// _, span := r.tracer.Start(ctx, "repository.checkin.FindByEmail")
// defer span.End()

return r.Db.Where("email = ?", email).Find(&checkIns).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()
// _, span := r.tracer.Start(ctx, "repository.checkin.FindByUserId")
// defer span.End()

return r.Db.Where("user_id = ?", userId).Find(&checkIns).Error
}
54 changes: 26 additions & 28 deletions internal/checkin/checkin.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"github.com/isd-sgcu/rpkm67-checkin/constant"
proto "github.com/isd-sgcu/rpkm67-go-proto/rpkm67/checkin/checkin/v1"
"github.com/isd-sgcu/rpkm67-model/model"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand All @@ -21,22 +19,22 @@ type Service interface {

type serviceImpl struct {
proto.UnimplementedCheckInServiceServer
repo Repository
log *zap.Logger
tracer trace.Tracer
repo Repository
log *zap.Logger
// tracer trace.Tracer
}

func NewService(repo Repository, log *zap.Logger, tracer trace.Tracer) Service {
return &serviceImpl{repo: repo, log: log, tracer: tracer}
func NewService(repo Repository, log *zap.Logger) Service {
return &serviceImpl{repo: repo, log: log}
}

func (s *serviceImpl) Create(ctx context.Context, req *proto.CreateCheckInRequest) (*proto.CreateCheckInResponse, error) {
span := trace.SpanFromContext(ctx)
span.SetAttributes(
attribute.String("req.user.id", req.UserId),
attribute.String("req.user.email", req.Email),
attribute.String("req.event", req.Event),
)
// span := trace.SpanFromContext(ctx)
// span.SetAttributes(
// attribute.String("req.user.id", req.UserId),
// attribute.String("req.user.email", req.Email),
// attribute.String("req.event", req.Event),
// )

checkin := &model.CheckIn{
Email: req.Email,
Expand All @@ -57,7 +55,7 @@ func (s *serviceImpl) Create(ctx context.Context, req *proto.CreateCheckInReques
return nil, status.Error(codes.AlreadyExists, constant.AlreadyCheckinErrorMessage)
}
}
span.AddEvent("Verify user checkin not duplicate")
// span.AddEvent("Verify user checkin not duplicate")

err = s.repo.Create(ctx, checkin)
if err != nil {
Expand All @@ -73,20 +71,20 @@ func (s *serviceImpl) Create(ctx context.Context, req *proto.CreateCheckInReques
}
return nil, status.Error(codes.Internal, constant.InternalServerErrorMessage)
}
span.AddEvent("Checkin created")
// span.AddEvent("Checkin created")

return &proto.CreateCheckInResponse{
CheckIn: ModelToProto(checkin),
}, nil
}

func (s *serviceImpl) FindByEmail(ctx context.Context, req *proto.FindByEmailCheckInRequest) (*proto.FindByEmailCheckInResponse, error) {
span := trace.SpanFromContext(ctx)
defer span.End()
// span := trace.SpanFromContext(ctx)
// defer span.End()

span.SetAttributes(
attribute.String("req.user.email", req.Email),
)
// span.SetAttributes(
// attribute.String("req.user.email", req.Email),
// )

if req.Email == "" {
s.log.Named("FindByUserEmail").Error("FindByUserEmail: invalid user Email")
Expand All @@ -105,31 +103,31 @@ func (s *serviceImpl) FindByEmail(ctx context.Context, req *proto.FindByEmailChe
}
return nil, status.Error(codes.Internal, constant.InternalServerErrorMessage)
}
span.AddEvent("Checkin found")
// span.AddEvent("Checkin found")

return &proto.FindByEmailCheckInResponse{
CheckIns: ModelToProtoList(checkins),
}, nil
}

func (s *serviceImpl) FindByUserId(ctx context.Context, req *proto.FindByUserIdCheckInRequest) (*proto.FindByUserIdCheckInResponse, error) {
ctx, span := s.tracer.Start(ctx, "service.checkin.FindByUserId")
span.SetAttributes(
attribute.String("req.user.id", req.UserId),
)
defer span.End()
// ctx, span := s.tracer.Start(ctx, "service.checkin.FindByUserId")
// span.SetAttributes(
// attribute.String("req.user.id", req.UserId),
// )
// defer span.End()

if req.UserId == "" {
s.log.Named("FindByUserId").Error("FindByUserId: invalid user ID")
span.RecordError(status.Error(codes.InvalidArgument, constant.ArgumentEmptyErrorMessage))
// span.RecordError(status.Error(codes.InvalidArgument, constant.ArgumentEmptyErrorMessage))
return nil, status.Error(codes.InvalidArgument, constant.ArgumentEmptyErrorMessage)
}

var checkins []*model.CheckIn
err := s.repo.FindByUserId(ctx, req.UserId, &checkins)
if err != nil {
s.log.Named("FindByUserId").Error("FindByUserId: ", zap.Error(err))
span.RecordError(status.Error(codes.InvalidArgument, constant.ArgumentEmptyErrorMessage))
// span.RecordError(status.Error(codes.InvalidArgument, constant.ArgumentEmptyErrorMessage))

if status.Code(err) == codes.Canceled {
return nil, status.Error(codes.Canceled, constant.RequestCancelledErrorMessage)
Expand Down
30 changes: 20 additions & 10 deletions internal/checkin/test/checkin.service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ func (t *CheckinServiceTest) SetupTest() {

func (t *CheckinServiceTest) TestCreateSuccess() {
repo := mock_checkin.NewMockRepository(t.controller)
svc := checkin.NewService(repo, t.logger, t.tracer)
// svc := checkin.NewService(repo, t.logger, t.tracer)
svc := checkin.NewService(repo, t.logger)

expectedResp := &proto.CreateCheckInResponse{
CheckIn: t.checkinProto,
Expand All @@ -82,7 +83,8 @@ func (t *CheckinServiceTest) TestCreateSuccess() {

func (t *CheckinServiceTest) TestCreateInternalError() {
repo := mock_checkin.NewMockRepository(t.controller)
svc := checkin.NewService(repo, t.logger, t.tracer)
// svc := checkin.NewService(repo, t.logger, t.tracer)
svc := checkin.NewService(repo, t.logger)

expectedErr := status.Error(codes.Internal, constant.InternalServerErrorMessage)

Expand All @@ -97,7 +99,8 @@ func (t *CheckinServiceTest) TestCreateInternalError() {

func (t *CheckinServiceTest) TestCreateAlreadyCheckinError() {
repo := mock_checkin.NewMockRepository(t.controller)
svc := checkin.NewService(repo, t.logger, t.tracer)
// svc := checkin.NewService(repo, t.logger, t.tracer)
svc := checkin.NewService(repo, t.logger)

expectedErr := status.Error(codes.AlreadyExists, constant.AlreadyCheckinErrorMessage)

Expand All @@ -112,7 +115,8 @@ func (t *CheckinServiceTest) TestCreateAlreadyCheckinError() {

func (t *CheckinServiceTest) TestCreateInvalidArgumentError() {
repo := mock_checkin.NewMockRepository(t.controller)
svc := checkin.NewService(repo, t.logger, t.tracer)
// svc := checkin.NewService(repo, t.logger, t.tracer)
svc := checkin.NewService(repo, t.logger)

expectedErr := status.Error(codes.InvalidArgument, constant.InvalidDataErrorMessage)

Expand All @@ -127,7 +131,8 @@ func (t *CheckinServiceTest) TestCreateInvalidArgumentError() {

func (t *CheckinServiceTest) TestFindByEmailSuccess() {
repo := mock_checkin.NewMockRepository(t.controller)
svc := checkin.NewService(repo, t.logger, t.tracer)
// svc := checkin.NewService(repo, t.logger, t.tracer)
svc := checkin.NewService(repo, t.logger)

expectedResp := &proto.FindByEmailCheckInResponse{
CheckIns: t.checkinsProto,
Expand All @@ -145,7 +150,8 @@ func (t *CheckinServiceTest) TestFindByEmailSuccess() {

func (t *CheckinServiceTest) TestFindByEmailInternalError() {
repo := mock_checkin.NewMockRepository(t.controller)
svc := checkin.NewService(repo, t.logger, t.tracer)
// svc := checkin.NewService(repo, t.logger, t.tracer)
svc := checkin.NewService(repo, t.logger)

email := t.checkinModel.Email

Expand All @@ -160,7 +166,8 @@ func (t *CheckinServiceTest) TestFindByEmailInternalError() {

func (t *CheckinServiceTest) TestFindByEmailRequestCanceledError() {
repo := mock_checkin.NewMockRepository(t.controller)
svc := checkin.NewService(repo, t.logger, t.tracer)
// svc := checkin.NewService(repo, t.logger, t.tracer)
svc := checkin.NewService(repo, t.logger)

expectedErr := status.Error(codes.Canceled, constant.RequestCancelledErrorMessage)
repo.EXPECT().FindByEmail(gomock.Any(), t.checkinModel.Email, gomock.Any()).SetArg(2, t.checkinsModel).Return(expectedErr)
Expand All @@ -173,7 +180,8 @@ func (t *CheckinServiceTest) TestFindByEmailRequestCanceledError() {

func (t *CheckinServiceTest) TestFindByUserIdSuccess() {
repo := mock_checkin.NewMockRepository(t.controller)
svc := checkin.NewService(repo, t.logger, t.tracer)
// svc := checkin.NewService(repo, t.logger, t.tracer)
svc := checkin.NewService(repo, t.logger)

expectedResp := &proto.FindByUserIdCheckInResponse{
CheckIns: t.checkinsProto,
Expand All @@ -189,7 +197,8 @@ func (t *CheckinServiceTest) TestFindByUserIdSuccess() {

func (t *CheckinServiceTest) TestFindByUserIdInternalError() {
repo := mock_checkin.NewMockRepository(t.controller)
svc := checkin.NewService(repo, t.logger, t.tracer)
// svc := checkin.NewService(repo, t.logger, t.tracer)
svc := checkin.NewService(repo, t.logger)

expectedErr := status.Error(codes.Internal, constant.InternalServerErrorMessage)
repo.EXPECT().FindByUserId(gomock.Any(), t.checkinModel.UserID, gomock.Any()).Return(expectedErr)
Expand All @@ -202,7 +211,8 @@ func (t *CheckinServiceTest) TestFindByUserIdInternalError() {

func (t *CheckinServiceTest) TestFindByUserIdRequestCanceledError() {
repo := mock_checkin.NewMockRepository(t.controller)
svc := checkin.NewService(repo, t.logger, t.tracer)
// svc := checkin.NewService(repo, t.logger, t.tracer)
svc := checkin.NewService(repo, t.logger)

expectedErr := status.Error(codes.Canceled, constant.RequestCancelledErrorMessage)
repo.EXPECT().FindByUserId(gomock.Any(), t.checkinModel.UserID, gomock.Any()).Return(expectedErr)
Expand Down
Loading