Skip to content

Commit

Permalink
Refactor to handle multiple traces in repository
Browse files Browse the repository at this point in the history
  • Loading branch information
HyunSu1768 committed Aug 29, 2024
1 parent 5bdaa1a commit 886ec56
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 8 deletions.
2 changes: 1 addition & 1 deletion internal/repository/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import (
)

type Repository interface {
SaveTrace(ctx context.Context, trace *models.Trace) error
SaveTraces(ctx context.Context, traces []*models.Trace) error
}
13 changes: 11 additions & 2 deletions internal/repository/mongodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,16 @@ func NewMongoRepository(uri string) (*MongoRepository, error) {
}, nil
}

func (r *MongoRepository) SaveTrace(ctx context.Context, trace *models.Trace) error {
_, err := r.traceCollection.InsertOne(ctx, trace)
func (r *MongoRepository) SaveTraces(ctx context.Context, traces []*models.Trace) error {
if len(traces) == 0 {
return nil
}

documents := make([]interface{}, len(traces))
for i, trace := range traces {
documents[i] = trace
}

_, err := r.traceCollection.InsertMany(ctx, documents)
return err
}
6 changes: 3 additions & 3 deletions internal/server/trace_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ func NewTraceServer(traceService *service.TraceService) *TraceServer {

func (s *TraceServer) Export(ctx context.Context, req *collectorpb.ExportTraceServiceRequest) (*collectorpb.ExportTraceServiceResponse, error) {
for _, resourceSpans := range req.ResourceSpans {
trace := telemetry.ConvertResourceSpansToTrace(resourceSpans)
if trace != nil {
err := s.traceService.ProcessTrace(ctx, trace)
traces := telemetry.ConvertResourceSpansToTraces(resourceSpans)
if traces != nil {
err := s.traceService.ProcessTrace(ctx, traces)
if err != nil {
return nil, status.Errorf(codes.Internal, "Failed to process trace: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/service/trace_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ func NewTraceService(repo repository.Repository) *TraceService {
return &TraceService{repo: repo}
}

func (s *TraceService) ProcessTrace(ctx context.Context, trace *models.Trace) error {
return s.repo.SaveTrace(ctx, trace)
func (s *TraceService) ProcessTrace(ctx context.Context, traces []*models.Trace) error {
return s.repo.SaveTraces(ctx, traces)
}
Binary file modified main
Binary file not shown.

0 comments on commit 886ec56

Please sign in to comment.