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

Add metrics for inbound HTTP requests #97

Merged
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ vet: ## Run go vet against code.
lint:
@echo "Run lint"
golangci-lint --version
golangci-lint run --verbose --print-resources-usage --modules-download-mode=vendor --timeout=5m0s
golangci-lint run --verbose --print-resources-usage --timeout=5m0s

.PHONY: deps-update
deps-update:
Expand Down
85 changes: 79 additions & 6 deletions internal/cmd/server/start_alarm_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,21 @@ package server

import (
"context"
"errors"
"fmt"
"log/slog"
"net/http"
"net/url"
"strings"

"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/spf13/cobra"

"github.com/openshift-kni/oran-o2ims/internal"
"github.com/openshift-kni/oran-o2ims/internal/exit"
"github.com/openshift-kni/oran-o2ims/internal/logging"
"github.com/openshift-kni/oran-o2ims/internal/metrics"
"github.com/openshift-kni/oran-o2ims/internal/network"
"github.com/openshift-kni/oran-o2ims/internal/service"
)
Expand All @@ -49,6 +52,7 @@ func AlarmServer() *cobra.Command {
}
flags := result.Flags()
network.AddListenerFlags(flags, network.APIListener, network.APIAddress)
network.AddListenerFlags(flags, network.MetricsListener, network.MetricsAddress)
_ = flags.String(
cloudIDFlagName,
"",
Expand Down Expand Up @@ -105,6 +109,19 @@ func (c *AlarmServerCommand) run(cmd *cobra.Command, argv []string) error {
// Get the flags:
flags := cmd.Flags()

// Create the exit handler:
exitHandler, err := exit.NewHandler().
SetLogger(c.logger).
Build()
if err != nil {
c.logger.ErrorContext(
ctx,
"Failed to create exit handler",
slog.String("error", err.Error()),
)
return exit.Error(1)
}

// Get the cloud identifier:
cloudID, err := flags.GetString(cloudIDFlagName)
if err != nil {
Expand Down Expand Up @@ -228,6 +245,23 @@ func (c *AlarmServerCommand) run(cmd *cobra.Command, argv []string) error {
)
}

// Create the metrics wrapper:
metricsWrapper, err := metrics.NewHandlerWrapper().
AddPaths(
"/o2ims-infrastructureMonitoring/-/alarms/-",
"/o2ims-infrastructureMonitoring/-/alarmProbableCauses/-",
).
SetSubsystem("inbound").
Build()
if err != nil {
c.logger.ErrorContext(
ctx,
"Failed to create metrics wrapper",
slog.String("error", err.Error()),
)
return exit.Error(1)
}

// Create the router:
router := mux.NewRouter()
router.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -236,6 +270,7 @@ func (c *AlarmServerCommand) run(cmd *cobra.Command, argv []string) error {
router.MethodNotAllowedHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
service.SendError(w, http.StatusMethodNotAllowed, "Method not allowed")
})
router.Use(metricsWrapper)

// Generate the search API URL according the backend URL
backendURL, err = c.generateAlarmmanagerApiUrl(backendURL)
Expand Down Expand Up @@ -275,24 +310,62 @@ func (c *AlarmServerCommand) run(cmd *cobra.Command, argv []string) error {
}
c.logger.InfoContext(
ctx,
"API listening",
"API server listening",
slog.String("address", apiListener.Addr().String()),
)
apiServer := http.Server{
apiServer := &http.Server{
Addr: apiListener.Addr().String(),
Handler: router,
}
err = apiServer.Serve(apiListener)
exitHandler.AddServer(apiServer)
go func() {
err = apiServer.Serve(apiListener)
if err != nil && !errors.Is(err, http.ErrServerClosed) {
c.logger.ErrorContext(
ctx,
"API server finished with error",
slog.String("error", err.Error()),
)
}
}()

// Start the metrics server:
metricsListener, err := network.NewListener().
SetLogger(c.logger).
SetFlags(flags, network.MetricsListener).
Build()
if err != nil {
c.logger.ErrorContext(
ctx,
"API server finished with error",
"Failed to create metrics listener",
slog.String("error", err.Error()),
)
return exit.Error(1)
}

return nil
c.logger.InfoContext(
ctx,
"Metrics server listening",
slog.String("address", metricsListener.Addr().String()),
)
metricsHandler := promhttp.Handler()
metricsServer := &http.Server{
Addr: metricsListener.Addr().String(),
Handler: metricsHandler,
}
exitHandler.AddServer(metricsServer)
go func() {
err = metricsServer.Serve(metricsListener)
if err != nil && !errors.Is(err, http.ErrServerClosed) {
c.logger.ErrorContext(
ctx,
"Metrics server finished with error",
slog.String("error", err.Error()),
)
}
}()

// Wait for exit signals:
return exitHandler.Wait(ctx)
}

func (c *AlarmServerCommand) createAlarmHandler(
Expand Down
84 changes: 78 additions & 6 deletions internal/cmd/server/start_alarm_subscription_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@ License.
package server

import (
"errors"
"log/slog"
"net/http"

"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/spf13/cobra"

"github.com/openshift-kni/oran-o2ims/internal"
"github.com/openshift-kni/oran-o2ims/internal/authentication"
"github.com/openshift-kni/oran-o2ims/internal/authorization"
"github.com/openshift-kni/oran-o2ims/internal/exit"
"github.com/openshift-kni/oran-o2ims/internal/logging"
"github.com/openshift-kni/oran-o2ims/internal/metrics"
"github.com/openshift-kni/oran-o2ims/internal/network"
"github.com/openshift-kni/oran-o2ims/internal/service"
)
Expand All @@ -45,6 +48,7 @@ func AlarmSubscriptionServer() *cobra.Command {
authorization.AddFlags(flags)

network.AddListenerFlags(flags, network.APIListener, network.APIAddress)
network.AddListenerFlags(flags, network.MetricsListener, network.MetricsAddress)
_ = flags.String(
cloudIDFlagName,
"",
Expand Down Expand Up @@ -80,6 +84,19 @@ func (c *AlarmSubscriptionServerCommand) run(cmd *cobra.Command, argv []string)
// Get the flags:
flags := cmd.Flags()

// Create the exit handler:
exitHandler, err := exit.NewHandler().
SetLogger(logger).
Build()
if err != nil {
logger.ErrorContext(
ctx,
"Failed to create exit handler",
slog.String("error", err.Error()),
)
return exit.Error(1)
}

// Get the cloud identifier:
cloudID, err := flags.GetString(cloudIDFlagName)
if err != nil {
Expand Down Expand Up @@ -161,6 +178,23 @@ func (c *AlarmSubscriptionServerCommand) run(cmd *cobra.Command, argv []string)
)
return exit.Error(1)
}

// Create the metrics wrapper:
metricsWrapper, err := metrics.NewHandlerWrapper().
AddPaths(
"/o2ims-infrastructureMonitoring/-/alarmSubscriptions/-",
).
SetSubsystem("inbound").
Build()
if err != nil {
logger.ErrorContext(
ctx,
"Failed to create metrics wrapper",
slog.String("error", err.Error()),
)
return exit.Error(1)
}

// Create the router:
router := mux.NewRouter()
router.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -169,7 +203,7 @@ func (c *AlarmSubscriptionServerCommand) run(cmd *cobra.Command, argv []string)
router.MethodNotAllowedHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
service.SendError(w, http.StatusMethodNotAllowed, "Method not allowed")
})
router.Use(authenticationWrapper, authorizationWrapper)
router.Use(metricsWrapper, authenticationWrapper, authorizationWrapper)

// Create the handler:
handler, err := service.NewAlarmSubscriptionHandler().
Expand Down Expand Up @@ -226,22 +260,60 @@ func (c *AlarmSubscriptionServerCommand) run(cmd *cobra.Command, argv []string)
}
logger.InfoContext(
ctx,
"API listening",
"API server listening",
slog.String("address", apiListener.Addr().String()),
)
apiServer := http.Server{
apiServer := &http.Server{
Addr: apiListener.Addr().String(),
Handler: router,
}
err = apiServer.Serve(apiListener)
exitHandler.AddServer(apiServer)
go func() {
err = apiServer.Serve(apiListener)
if err != nil && !errors.Is(err, http.ErrServerClosed) {
logger.ErrorContext(
ctx,
"API server finished with error",
slog.String("error", err.Error()),
)
}
}()

// Start the metrics server:
metricsListener, err := network.NewListener().
SetLogger(logger).
SetFlags(flags, network.MetricsListener).
Build()
if err != nil {
logger.ErrorContext(
ctx,
"API server finished with error",
"Failed to create metrics listener",
slog.String("error", err.Error()),
)
return exit.Error(1)
}
logger.InfoContext(
ctx,
"Metrics server listening",
slog.String("address", metricsListener.Addr().String()),
)
metricsHandler := promhttp.Handler()
metricsServer := &http.Server{
Addr: metricsListener.Addr().String(),
Handler: metricsHandler,
}
exitHandler.AddServer(metricsServer)
go func() {
err = metricsServer.Serve(metricsListener)
if err != nil && !errors.Is(err, http.ErrServerClosed) {
logger.ErrorContext(
ctx,
"Metrics server finished with error",
slog.String("error", err.Error()),
)
}
}()

return nil
// Wait for exit signals:
return exitHandler.Wait(ctx)
}
Loading
Loading