Skip to content

Commit

Permalink
Merge pull request #523 from traPtitech/feature/seat_traffic_metrics
Browse files Browse the repository at this point in the history
席関連のメトリクス整理
  • Loading branch information
mazrean authored Oct 28, 2022
2 parents adab99a + 04220e0 commit 12ada7d
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 93 deletions.
47 changes: 40 additions & 7 deletions src/repository/gorm2/v2_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type MetricsCollectorV2 struct {
gameImageGauge *prometheus.GaugeVec
gameVideoGauge *prometheus.GaugeVec
gameFileGauge *prometheus.GaugeVec
gameURLGauge prometheus.Gauge
seatGauge *prometheus.GaugeVec
}

func (mc *MetricsCollectorV2) Metrics(p *gormPrometheus.Prometheus) []prometheus.Collector {
Expand Down Expand Up @@ -77,13 +77,13 @@ func (mc *MetricsCollectorV2) Metrics(p *gormPrometheus.Prometheus) []prometheus
}, []string{"type"})
}

if mc.gameURLGauge == nil {
mc.gameURLGauge = promauto.NewGauge(prometheus.GaugeOpts{
if mc.seatGauge == nil {
mc.seatGauge = promauto.NewGaugeVec(prometheus.GaugeOpts{
Namespace: mc.Prefix,
Subsystem: "game_url",
Subsystem: "seat",
Name: "count",
Help: "Number of game urls",
})
Help: "Number of seats",
}, []string{"status"})
}

go func() {
Expand All @@ -100,7 +100,7 @@ func (mc *MetricsCollectorV2) Metrics(p *gormPrometheus.Prometheus) []prometheus
mc.gameImageGauge,
mc.gameVideoGauge,
mc.gameFileGauge,
mc.gameURLGauge,
mc.seatGauge,
}
}

Expand Down Expand Up @@ -131,6 +131,11 @@ func (mc *MetricsCollectorV2) collect(p *gormPrometheus.Prometheus) {
if err != nil {
p.DB.Logger.Error(ctx, "failed to collect game file metrics", err)
}

err = mc.collectSeatMetrics(ctx, p)
if err != nil {
p.DB.Logger.Error(ctx, "failed to collect seat metrics", err)
}
}

func (mc *MetricsCollectorV2) collectAccessTokenMetrics(ctx context.Context, p *gormPrometheus.Prometheus) error {
Expand Down Expand Up @@ -284,3 +289,31 @@ func (mc *MetricsCollectorV2) collectGameVideoMetrics(ctx context.Context, p *go

return nil
}

func (mc *MetricsCollectorV2) collectSeatMetrics(ctx context.Context, p *gormPrometheus.Prometheus) error {
var seatCounts []struct {
Type string `gorm:"column:type"`
Count int64 `gorm:"column:count"`
}

err := p.DB.
Session(&gorm.Session{}).
Unscoped().
Model(&migrate.SeatTable2{}).
Joins("JOIN seat_statuses ON seats.status_id = seat_statuses.id AND seat_statuses.active").
Select("seat_statuses.name AS type, count(*) as count").
Group("type").
Find(&seatCounts).Error
if err != nil {
return fmt.Errorf("failed to get game video counts: %w", err)
}

mc.seatGauge.Reset()
for _, count := range seatCounts {
mc.seatGauge.
WithLabelValues(count.Type).
Set(float64(count.Count))
}

return nil
}
72 changes: 0 additions & 72 deletions src/service/v2/metrics.go

This file was deleted.

28 changes: 18 additions & 10 deletions src/service/v2/seat.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
"log"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/traPtitech/trap-collection-server/src/cache"
"github.com/traPtitech/trap-collection-server/src/domain"
"github.com/traPtitech/trap-collection-server/src/domain/values"
Expand All @@ -19,23 +21,29 @@ type Seat struct {
db repository.DB
seatRepository repository.Seat
seatCache cache.Seat
seatMetrics *SeatMetrics
}

func NewSeat(
db repository.DB,
seatRepository repository.Seat,
seatCache cache.Seat,
seatMetrics *SeatMetrics,
) *Seat {
return &Seat{
db: db,
seatRepository: seatRepository,
seatCache: seatCache,
seatMetrics: seatMetrics,
}
}

var (
seatTrafficCounter = promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: "service_trap_collection",
Subsystem: "seat",
Name: "traffic",
Help: "The number of traffic to seat service",
}, []string{"status"})
)

func (s *Seat) GetSeats(ctx context.Context) ([]*domain.Seat, error) {
seats, err := s.seatCache.GetActiveSeats(ctx)
if err != nil && !errors.Is(err, cache.ErrCacheMiss) {
Expand All @@ -57,9 +65,6 @@ func (s *Seat) GetSeats(ctx context.Context) ([]*domain.Seat, error) {
log.Printf("error: failed to set seats to cache: %v\n", err)
}

// 基本はSeatStatusのupdate時に更新しているが、定期的に正確な情報への同期を行いたいので
s.seatMetrics.UpdateWithActiveSeats(seats)

return seats, nil
}

Expand Down Expand Up @@ -87,15 +92,20 @@ func (s *Seat) UpdateSeatStatus(ctx context.Context, seatID values.SeatID, statu
return nil
}

switch {
case seat.Status() == values.SeatStatusInUse && status == values.SeatStatusEmpty:
seatTrafficCounter.WithLabelValues("out").Inc()
case seat.Status() == values.SeatStatusEmpty && status == values.SeatStatusInUse:
seatTrafficCounter.WithLabelValues("in").Inc()
}

seat.SetStatus(status)

err = s.seatRepository.UpdateSeatsStatus(ctx, []values.SeatID{seatID}, status)
if err != nil {
return fmt.Errorf("failed to update seats status: %w", err)
}

s.seatMetrics.UpdateWithNewSeatStatus(status)

return nil
})
if err != nil {
Expand Down Expand Up @@ -188,7 +198,5 @@ func (s *Seat) UpdateSeatNum(ctx context.Context, num uint) ([]*domain.Seat, err
log.Printf("error: failed to set seats to cache: %v", err)
}

s.seatMetrics.UpdateWithActiveSeats(activeSeats)

return activeSeats, nil
}
2 changes: 0 additions & 2 deletions src/wire/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,6 @@ var (
wire.Bind(new(service.Seat), new(*v2.Seat)),
v2.NewSeat,

v2.NewSeatMetrics,

v2.NewUser,
)
)
3 changes: 1 addition & 2 deletions src/wire/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 12ada7d

Please sign in to comment.