Skip to content

Commit

Permalink
metrics added (#48)
Browse files Browse the repository at this point in the history
* metrics added

* updated

* some fix

* merged with configs

* reg metric + последовательность

* cadvisor added

* cadvisor added

* small fixes

* grafana folder added

* small fixes

* Delete grafana directory
  • Loading branch information
timurIsaevIY authored Dec 8, 2024
1 parent d74ca07 commit 4cffdc4
Show file tree
Hide file tree
Showing 18 changed files with 3,632 additions and 93 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ go.mod
2024-2-Zdes-budet-nazvanie-UykwHnIE.crt
data/
.env
/grafana/
2 changes: 1 addition & 1 deletion build/attraction.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ FROM scratch AS runner
WORKDIR /build
COPY --from=builder /github.com/go-park-mail-ru/2024_2_ThereWillBeName/attractions/.bin .
COPY --from=builder /github.com/go-park-mail-ru/2024_2_ThereWillBeName/attractions/config config/
EXPOSE 50051
EXPOSE 50051 8091
ENTRYPOINT ["./.bin"]
2 changes: 1 addition & 1 deletion build/gateway.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ FROM scratch AS runner
WORKDIR /build
COPY --from=builder /github.com/go-park-mail-ru/2024_2_ThereWillBeName/gateway/.bin .
COPY --from=builder /github.com/go-park-mail-ru/2024_2_ThereWillBeName/gateway/config config/
EXPOSE 8080
EXPOSE 8081
ENTRYPOINT ["./.bin"]
60 changes: 46 additions & 14 deletions cmd/attractions/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"2024_2_ThereWillBeName/internal/pkg/config"
"2024_2_ThereWillBeName/internal/pkg/dblogger"
"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 @@ -25,14 +26,19 @@ import (
searchRepo "2024_2_ThereWillBeName/internal/pkg/search/repo"
searchUsecase "2024_2_ThereWillBeName/internal/pkg/search/usecase"
"database/sql"
"errors"
"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 @@ -46,18 +52,33 @@ func main() {
logger := setupLogger()

db, err := sql.Open("postgres", fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", cfg.Database.DbHost, cfg.Database.DbPort, cfg.Database.DbUser, cfg.Database.DbPass, cfg.Database.DbName))
if err != nil {
log.Fatalf("failed to connect to database: %v", err)
}
defer db.Close()

err = db.Ping()
if err != nil {
log.Fatalf("failed to ping database: %v", err)
}

metricMw := metricsMw.Create()
metricMw.RegisterMetrics()
wrappedDB := dblogger.NewDB(db, logger)

r := mux.NewRouter()
r.Handle("/metrics", promhttp.Handler())
httpSrv := &http.Server{
Addr: fmt.Sprintf(":%d", cfg.Metric.AttractionPort),
Handler: r,
ReadHeaderTimeout: 10 * time.Second,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}

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

reviewsRepo := reviewRepo.NewReviewRepository(wrappedDB)
reviewUsecase := reviewUsecase.NewReviewsUsecase(reviewsRepo)
placeRepo := placeRepo.NewPLaceRepository(wrappedDB)
Expand All @@ -69,21 +90,18 @@ func main() {
searchRepo := searchRepo.NewSearchRepository(wrappedDB)
searchUsecase := searchUsecase.NewSearchUsecase(searchRepo)

grpcAttractionsServer := grpc.NewServer()

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 All @@ -101,6 +119,20 @@ func main() {

stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
go func() {
ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop()

for {
select {
case <-ticker.C:
metricMw.TrackSystemMetrics("attractions")
case <-stop:
return
}
}
}()

<-stop

log.Println("Shutting down gRPC server...")
Expand Down
44 changes: 41 additions & 3 deletions cmd/gateway/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
httpresponse "2024_2_ThereWillBeName/internal/pkg/httpresponses"
"2024_2_ThereWillBeName/internal/pkg/jwt"
"2024_2_ThereWillBeName/internal/pkg/logger"
metricsMw "2024_2_ThereWillBeName/internal/pkg/metrics/middleware"
"2024_2_ThereWillBeName/internal/pkg/middleware"
genReviews "2024_2_ThereWillBeName/internal/pkg/reviews/delivery/grpc/gen"
httpReviews "2024_2_ThereWillBeName/internal/pkg/reviews/delivery/http"
Expand All @@ -26,13 +27,15 @@ import (
"context"
"errors"
"fmt"
"github.com/prometheus/client_golang/prometheus/promhttp"
"log"
"log/slog"
"net/http"
"os"
"os/signal"
"strconv"
"syscall"
"time"

_ "github.com/lib/pq"

Expand All @@ -46,6 +49,9 @@ func main() {

logger := setupLogger()

metricMw := metricsMw.Create()
metricMw.RegisterMetrics()

jwtSecret := os.Getenv("JWT_SECRET")
jwtHandler := jwt.NewJWT(jwtSecret, logger)

Expand Down Expand Up @@ -86,6 +92,24 @@ func main() {
corsMiddleware := middleware.NewCORSMiddleware(cfg.AllowedOrigins)
r := mux.NewRouter().PathPrefix("/api/v1").Subrouter()
r.Use(corsMiddleware.CorsMiddleware)
r.Use(metricMw.MetricsMiddleware)
r.Use(corsMiddleware.CorsMiddleware)
metricsRouter := mux.NewRouter().PathPrefix("/api/v1").Subrouter()
metricsRouter.Handle("/metrics", promhttp.Handler())
httpSrvMw := &http.Server{
Addr: fmt.Sprintf(":%d", cfg.Metric.GatewayPort),
Handler: metricsRouter,
ReadHeaderTimeout: 10 * time.Second,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}

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

r.Use(middleware.RequestLoggerMiddleware(logger))

Expand All @@ -105,8 +129,8 @@ func main() {
places := r.PathPrefix("/places").Subrouter()
places.HandleFunc("", placesHandler.GetPlacesHandler).Methods(http.MethodGet)
places.HandleFunc("/search", placesHandler.SearchPlacesHandler).Methods(http.MethodGet)
places.HandleFunc("/category", placesHandler.GetPlacesByCategoryHandler).Methods(http.MethodGet)
places.HandleFunc("/{id}", placesHandler.GetPlaceHandler).Methods(http.MethodGet)
places.HandleFunc("/category/{categoryName}", placesHandler.GetPlacesByCategoryHandler).Methods(http.MethodGet)

categoriesHandler := httpCategories.NewCategoriesHandler(categoriesClient, logger)
categories := r.PathPrefix("/categories").Subrouter()
Expand Down Expand Up @@ -164,9 +188,9 @@ func main() {
survey.Handle("/{id}", middleware.MiddlewareAuth(jwtHandler, http.HandlerFunc(surveyHandler.CreateSurveyResponse), logger)).Methods(http.MethodPost)
survey.Handle("/users/{id}", middleware.MiddlewareAuth(jwtHandler, http.HandlerFunc(surveyHandler.GetSurveyStatsByUserId), logger)).Methods(http.MethodGet)

httpSrv := &http.Server{Handler: r, Addr: fmt.Sprintf(":%d", 8080)}
httpSrv := &http.Server{Handler: r, Addr: fmt.Sprintf(":%d", cfg.HttpServer.Address)}
go func() {
logger.Info("HTTP server listening on :%d", 8080)
logger.Info(fmt.Sprintf("HTTP server listening on :%d", cfg.HttpServer.Address))
if err := httpSrv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
logger.Error("failed to serve HTTP: %d", err)
os.Exit(1)
Expand All @@ -175,6 +199,20 @@ func main() {

stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)

go func() {
ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop()

for {
select {
case <-ticker.C:
metricMw.TrackSystemMetrics("gateway")
case <-stop:
return
}
}
}()
<-stop

logger.Info("Shutting down HTTP server...")
Expand Down
46 changes: 43 additions & 3 deletions cmd/trips/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,26 @@ import (
"2024_2_ThereWillBeName/internal/pkg/config"
"2024_2_ThereWillBeName/internal/pkg/dblogger"
"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"
"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"

_ "github.com/lib/pq"
"time"

"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
Expand All @@ -29,6 +34,9 @@ func main() {

logger := setupLogger()

metricMw := metricsMw.Create()
metricMw.RegisterMetrics()

db, err := sql.Open("postgres", fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", cfg.Database.DbHost, cfg.Database.DbPort, cfg.Database.DbUser, cfg.Database.DbPass, cfg.Database.DbName))
if err != nil {
log.Fatalf("failed to connect to database: %v", err)
Expand All @@ -42,10 +50,28 @@ func main() {

wrappedDB := dblogger.NewDB(db, logger)

r := mux.NewRouter()
r.Handle("/metrics", promhttp.Handler())
httpSrv := &http.Server{
Addr: fmt.Sprintf(":%d", cfg.Metric.TripPort),
Handler: r,
ReadHeaderTimeout: 10 * time.Second,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}

go func() {

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

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

grpcTripsServer := grpc.NewServer()
grpcTripsServer := grpc.NewServer(grpc.UnaryInterceptor(metricMw.ServerMetricsInterceptor))
tripsHandler := grpcTrips.NewGrpcTripHandler(tripUsecase, logger)
gen.RegisterTripsServer(grpcTripsServer, tripsHandler)
reflection.Register(grpcTripsServer)
Expand All @@ -63,6 +89,20 @@ func main() {

stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)

go func() {
ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop()

for {
select {
case <-ticker.C:
metricMw.TrackSystemMetrics("trips")
case <-stop:
return
}
}
}()
<-stop

log.Println("Shutting down gRPC server...")
Expand Down
Loading

0 comments on commit 4cffdc4

Please sign in to comment.