Skip to content

Commit

Permalink
metrics added
Browse files Browse the repository at this point in the history
  • Loading branch information
timurIsaevIY committed Nov 25, 2024
1 parent a07f96e commit 8928059
Show file tree
Hide file tree
Showing 9 changed files with 272 additions and 75 deletions.
42 changes: 33 additions & 9 deletions cmd/attractions/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
citiesRepo "2024_2_ThereWillBeName/internal/pkg/cities/repo"
citiesUsecase "2024_2_ThereWillBeName/internal/pkg/cities/usecase"
"2024_2_ThereWillBeName/internal/pkg/logger"
metricsMw "2024_2_ThereWillBeName/internal/pkg/metrics/middleware"
grpcReviews "2024_2_ThereWillBeName/internal/pkg/reviews/delivery/grpc"
genReviews "2024_2_ThereWillBeName/internal/pkg/reviews/delivery/grpc/gen"
reviewRepo "2024_2_ThereWillBeName/internal/pkg/reviews/repo"
Expand All @@ -24,14 +25,20 @@ import (
searchRepo "2024_2_ThereWillBeName/internal/pkg/search/repo"
searchUsecase "2024_2_ThereWillBeName/internal/pkg/search/usecase"
"database/sql"
"errors"
"flag"
"fmt"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus/promhttp"
"log"
"log/slog"
"net"
"net/http"
"os"
"os/signal"
"strconv"
"syscall"
"time"

_ "github.com/lib/pq"
"google.golang.org/grpc"
Expand All @@ -52,6 +59,24 @@ func main() {
}
defer db.Close()

r := mux.NewRouter().PathPrefix("/api/v1").Subrouter()
r.PathPrefix("/metrics").Handler(promhttp.Handler())
httpSrv := &http.Server{
Addr: ":8091",
Handler: r,
ReadHeaderTimeout: 10 * time.Second,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}

go func() {

logger.Info("Starting HTTP server for metrics on :8091")
if err := httpSrv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
logger.Error(fmt.Sprintf("HTTP server listen: %s\n", err))
}
}()

reviewsRepo := reviewRepo.NewReviewRepository(db)
reviewUsecase := reviewUsecase.NewReviewsUsecase(reviewsRepo)
placeRepo := placeRepo.NewPLaceRepository(db)
Expand All @@ -63,21 +88,20 @@ func main() {
searchRepo := searchRepo.NewSearchRepository(db)
searchUsecase := searchUsecase.NewSearchUsecase(searchRepo)

grpcAttractionsServer := grpc.NewServer()
metricMw := metricsMw.Create()

attractionsHandler := grpcAttractions.NewGrpcAttractionsHandler(placeUsecase)
genPlaces.RegisterAttractionsServer(grpcAttractionsServer, attractionsHandler)

citiesHandler := grpcCities.NewGrpcCitiesHandler(citiesUsecase)
genCities.RegisterCitiesServer(grpcAttractionsServer, citiesHandler)

reviewsHandler := grpcReviews.NewGrpcReviewsHandler(reviewUsecase)
genReviews.RegisterReviewsServer(grpcAttractionsServer, reviewsHandler)

categoriesHandler := grpcCategories.NewGrpcCategoriesHandler(categoriesUsecase)
genCategories.RegisterCategoriesServer(grpcAttractionsServer, categoriesHandler)

searchHandler := grpcSearch.NewGrpcSearchHandler(searchUsecase, logger)

grpcAttractionsServer := grpc.NewServer(grpc.UnaryInterceptor(metricMw.ServerMetricsInterceptor))

genPlaces.RegisterAttractionsServer(grpcAttractionsServer, attractionsHandler)
genCities.RegisterCitiesServer(grpcAttractionsServer, citiesHandler)
genReviews.RegisterReviewsServer(grpcAttractionsServer, reviewsHandler)
genCategories.RegisterCategoriesServer(grpcAttractionsServer, categoriesHandler)
genSearch.RegisterSearchServer(grpcAttractionsServer, searchHandler)

reflection.Register(grpcAttractionsServer)
Expand Down
2 changes: 2 additions & 0 deletions cmd/gateway/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"errors"
"flag"
"fmt"
"github.com/prometheus/client_golang/prometheus/promhttp"
"log"
"log/slog"
"net/http"
Expand Down Expand Up @@ -91,6 +92,7 @@ func main() {
corsMiddleware := middleware.NewCORSMiddleware([]string{cfg.AllowedOrigin})
r := mux.NewRouter().PathPrefix("/api/v1").Subrouter()
r.Use(corsMiddleware.CorsMiddleware)
r.PathPrefix("/metrics").Handler(promhttp.Handler())

// Обработка ненайденных маршрутов
r.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down
28 changes: 27 additions & 1 deletion cmd/trips/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,27 @@ package main
import (
"2024_2_ThereWillBeName/internal/models"
"2024_2_ThereWillBeName/internal/pkg/logger"
metricsMw "2024_2_ThereWillBeName/internal/pkg/metrics/middleware"
grpcTrips "2024_2_ThereWillBeName/internal/pkg/trips/delivery/grpc"
"2024_2_ThereWillBeName/internal/pkg/trips/delivery/grpc/gen"
tripRepo "2024_2_ThereWillBeName/internal/pkg/trips/repo"
tripUsecase "2024_2_ThereWillBeName/internal/pkg/trips/usecase"
"database/sql"
"errors"
"flag"
"fmt"
"github.com/gorilla/mux"
_ "github.com/lib/pq"
"github.com/prometheus/client_golang/prometheus/promhttp"
"log"
"log/slog"
"net"
"net/http"
"os"
"os/signal"
"strconv"
"syscall"
"time"

"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
Expand All @@ -37,10 +43,30 @@ func main() {
}
defer db.Close()

r := mux.NewRouter().PathPrefix("/api/v1").Subrouter()
r.PathPrefix("/metrics").Handler(promhttp.Handler())
httpSrv := &http.Server{
Addr: ":8092",
Handler: r,
ReadHeaderTimeout: 10 * time.Second,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}

go func() {

logger.Info("Starting HTTP server for metrics on :8092")
if err := httpSrv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
logger.Error(fmt.Sprintf("HTTP server listen: %s\n", err))
}
}()

tripRepo := tripRepo.NewTripRepository(db)
tripUsecase := tripUsecase.NewTripsUsecase(tripRepo)

grpcTripsServer := grpc.NewServer()
metricMw := metricsMw.Create()

grpcTripsServer := grpc.NewServer(grpc.UnaryInterceptor(metricMw.ServerMetricsInterceptor))
tripsHandler := grpcTrips.NewGrpcTripHandler(tripUsecase, logger)
gen.RegisterTripsServer(grpcTripsServer, tripsHandler)
reflection.Register(grpcTripsServer)
Expand Down
28 changes: 27 additions & 1 deletion cmd/users/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,27 @@ package main
import (
"2024_2_ThereWillBeName/internal/models"
"2024_2_ThereWillBeName/internal/pkg/logger"
metricsMw "2024_2_ThereWillBeName/internal/pkg/metrics/middleware"
grpcUsers "2024_2_ThereWillBeName/internal/pkg/user/delivery/grpc"
"2024_2_ThereWillBeName/internal/pkg/user/delivery/grpc/gen"
userRepo "2024_2_ThereWillBeName/internal/pkg/user/repo"
userUsecase "2024_2_ThereWillBeName/internal/pkg/user/usecase"
"database/sql"
"errors"
"flag"
"fmt"
"github.com/gorilla/mux"
_ "github.com/lib/pq"
"github.com/prometheus/client_golang/prometheus/promhttp"
"log"
"log/slog"
"net"
"net/http"
"os"
"os/signal"
"strconv"
"syscall"
"time"

"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
Expand All @@ -39,10 +45,30 @@ func main() {
}
defer db.Close()

r := mux.NewRouter().PathPrefix("/api/v1").Subrouter()
r.PathPrefix("/metrics").Handler(promhttp.Handler())
httpSrv := &http.Server{
Addr: ":8093",
Handler: r,
ReadHeaderTimeout: 10 * time.Second,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}

go func() {

logger.Info("Starting HTTP server for metrics on :8093")
if err := httpSrv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
logger.Error(fmt.Sprintf("HTTP server listen: %s\n", err))
}
}()

userRepo := userRepo.NewAuthRepository(db)
userUsecase := userUsecase.NewUserUsecase(userRepo, storagePath)

grpcUsersServer := grpc.NewServer()
metricMw := metricsMw.Create()

grpcUsersServer := grpc.NewServer(grpc.UnaryInterceptor(metricMw.ServerMetricsInterceptor))
usersHandler := grpcUsers.NewGrpcUserHandler(userUsecase, logger)
gen.RegisterUserServiceServer(grpcUsersServer, usersHandler)
reflection.Register(grpcUsersServer)
Expand Down
65 changes: 59 additions & 6 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ services:
volumes:
- ./assets/avatars:/assets/avatars
ports:
- 8080:8080
- "8080:8080"
depends_on:
- tripdb
networks:
- my_network
attractions:
container_name: attractions
build:
Expand All @@ -28,9 +30,11 @@ services:
volumes:
- ./assets/avatars:/assets/avatars
ports:
- 8081:8081
- "8081:8081"
depends_on:
- tripdb
networks:
- my_network
trips:
container_name: trips
build:
Expand All @@ -43,9 +47,11 @@ services:
volumes:
- ./assets/avatars:/assets/avatars
ports:
- 50053:50053
- "50053:50053"
depends_on:
- tripdb
networks:
- my_network
users:
container_name: users
build:
Expand All @@ -59,9 +65,11 @@ services:
volumes:
- ./assets/avatars:/assets/avatars
ports:
- 50052:50052
- "50052:50052"
depends_on:
- tripdb
networks:
- my_network
survey:
container_name: survey
build:
Expand All @@ -75,9 +83,11 @@ services:
volumes:
- ./assets/avatars:/assets/avatars
ports:
- 50054:50054
- "50054:50054"
depends_on:
- tripdb
networks:
- my_network
tripdb:
container_name: postgres
image: postgres:15
Expand All @@ -90,4 +100,47 @@ services:
- ./data:/var/lib/postgresql/data
restart: unless-stopped
ports:
- 5432:5432
- "5432:5432"
networks:
- my_network
prometheus:
image: prom/prometheus:latest
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
command:
- --config.file=/etc/prometheus/prometheus.yml
ports:
- "9090:9090"
networks:
- my_network
node_exporter:
depends_on:
- prometheus
image: quay.io/prometheus/node-exporter:latest
container_name: node_exporter
volumes:
- /proc:/host/proc:ro
- /sys:/host/sys:ro
- /:/rootfs:ro
command:
- --path.procfs=/host/proc
- --path.sysfs=/host/sys
- --collector.filesystem.ignored-mount-points
- ^/(sys|proc|dev|host|etc|rootfs/var/lib/docker/containers|rootfs/var/lib/docker/overlay2|rootfs/run/docker/netns|rootfs/var/lib/docker/aufs)($$|/)
hostname: node_exporter
ports:
- "9100:9100"
networks:
- my_network
grafana:
image: grafana/grafana-enterprise
volumes:
- ./grafana:/var/lib/grafana/
- ./grafana/provisioning/:/etc/grafana/provisioning/
ports:
- "3000:3000"
networks:
- my_network

networks:
my_network:
14 changes: 14 additions & 0 deletions internal/pkg/metrics/interfaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package metrics

import (
"context"
"google.golang.org/grpc"
"time"
)

type MetricsHTTP interface {
IncreaseHits(string, string)
IncreaseErr(string, string)
AddDurationToHistogram(string, time.Duration)
ServerMetricsInterceptor(context.Context, interface{}, *grpc.UnaryServerInfo, grpc.UnaryHandler) (interface{}, error)
}
Loading

0 comments on commit 8928059

Please sign in to comment.