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 subscription state metrics #137

Merged
merged 3 commits into from
May 29, 2024
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
7 changes: 5 additions & 2 deletions frontend/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ ARO_HCP_FRONTEND_IMAGE ?= $(ARO_HCP_BASE_IMAGE)/arohcpfrontend:$(COMMIT)
RESOURCEGROUP ?= aro-hcp-${AKSCONFIG}-$(USER)
CLUSTER_NAME ?=
DEPLOYMENTNAME=$(RESOURCEGROUP)
REGION ?= eastus


frontend:
Expand Down Expand Up @@ -38,7 +39,8 @@ deploy:
oc process -f ./deploy/aro-hcp-frontend.yml --local \
-p ARO_HCP_FRONTEND_IMAGE=${ARO_HCP_FRONTEND_IMAGE} \
-p FRONTEND_MI_CLIENT_ID="$${FRONTEND_MI_CLIENT_ID}" \
-p DB_NAME="$${DB_NAME}"| oc apply -f -
-p DB_NAME="$${DB_NAME}"
-p REGION=${REGION}| oc apply -f -

undeploy:
@test "${RESOURCEGROUP}" != "" || (echo "RESOURCEGROUP must be defined" && exit 1)
Expand All @@ -57,7 +59,8 @@ deploy-private:
oc process -f ./deploy/aro-hcp-frontend.yml --local \
-p ARO_HCP_FRONTEND_IMAGE=${ARO_HCP_FRONTEND_IMAGE} \
-p FRONTEND_MI_CLIENT_ID="$${FRONTEND_MI_CLIENT_ID}" \
-p DB_NAME="$${DB_NAME}" > "$${TMP_DEPLOY}";\
-p DB_NAME="$${DB_NAME}" \
-p REGION=${REGION}> "$${TMP_DEPLOY}";\
az aks command invoke --resource-group ${RESOURCEGROUP} --name ${CLUSTER_NAME} --command "kubectl create -f $$(basename $${TMP_DEPLOY})" --file "$${TMP_DEPLOY}"

undeploy-private:
Expand Down
4 changes: 4 additions & 0 deletions frontend/deploy/aro-hcp-frontend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ parameters:
- name: DB_NAME
description: Name of the Cosmos DB object in Azure
value: "none"
- name: REGION
required: true

objects:
- apiVersion: v1
Expand Down Expand Up @@ -67,6 +69,8 @@ objects:
value: ${DB_NAME}
- name: DB_URL
value: "https://${DB_NAME}.documents.azure.com:443/"
- name: REGION
value: ${REGION}
ports:
- containerPort: 8443
protocol: TCP
Expand Down
11 changes: 10 additions & 1 deletion frontend/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type Frontend struct {
ready atomic.Value
done chan struct{}
metrics metrics.Emitter
region string
}

// MuxPattern forms a URL pattern suitable for passing to http.ServeMux.
Expand All @@ -53,7 +54,7 @@ func MuxPattern(method string, segments ...string) string {
return fmt.Sprintf("%s /%s", method, strings.ToLower(path.Join(segments...)))
}

func NewFrontend(logger *slog.Logger, listener net.Listener, emitter metrics.Emitter, dbClient *DBClient) *Frontend {
func NewFrontend(logger *slog.Logger, listener net.Listener, emitter metrics.Emitter, dbClient *DBClient, region string) *Frontend {
f := &Frontend{
logger: logger,
listener: listener,
Expand All @@ -67,6 +68,7 @@ func NewFrontend(logger *slog.Logger, listener net.Listener, emitter metrics.Emi
cache: *NewCache(),
dbClient: *dbClient,
done: make(chan struct{}),
region: region,
}

subscriptionStateMuxValidator := NewSubscriptionStateMuxValidator(&f.cache)
Expand Down Expand Up @@ -432,6 +434,13 @@ func (f *Frontend) ArmSubscriptionAction(writer http.ResponseWriter, request *ht
subId := request.PathValue(PathSegmentSubscriptionID)
f.cache.SetSubscription(subId, &subscription)

// Emit the subscription state metric
f.metrics.EmitGauge("subscription_lifecycle", 1, map[string]string{
"region": f.region,
"subscriptionid": subId,
"state": string(subscription.State),
})

resp, err := json.Marshal(subscription)
if err != nil {
f.logger.Error(err.Error())
Expand Down
9 changes: 8 additions & 1 deletion frontend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ func main() {

logger.Info(fmt.Sprintf("%s (%s) started", ProgramName, version))

// Fetch the region from the env variable
region := os.Getenv("REGION")
katherinelc321 marked this conversation as resolved.
Show resolved Hide resolved
if region == "" {
logger.Error("REGION env variable is not set.")
}
logger.Info(fmt.Sprintf("Application running in region: %s", region))

ctx := context.Background()
stop := make(chan struct{})

Expand All @@ -52,7 +59,7 @@ func main() {
logger.Error(fmt.Sprintf("Creating the database client failed: %v", err))
}

frontend := NewFrontend(logger, listener, prometheusEmitter, dbClient)
frontend := NewFrontend(logger, listener, prometheusEmitter, dbClient, region)

// Verify the Async DB is available and accessible
logger.Info("Testing DB Access")
Expand Down
24 changes: 24 additions & 0 deletions frontend/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/prometheus/client_golang/prometheus"
"golang.org/x/exp/maps"

"github.com/Azure/ARO-HCP/internal/api/arm"
"github.com/Azure/ARO-HCP/internal/metrics"
)

Expand Down Expand Up @@ -50,6 +51,7 @@ func (pe *PrometheusEmitter) EmitCounter(name string, value float64, labels map[

type MetricsMiddleware struct {
metrics.Emitter
cache *Cache
}

type logResponseWriter struct {
Expand All @@ -76,6 +78,28 @@ func (mm MetricsMiddleware) Metrics() MiddlewareFunc {
routePattern := r.URL.Path
duration := time.Since(startTime).Milliseconds()

subscriptionId := r.PathValue(PathSegmentSubscriptionID)
if subscriptionId != "" {
sub, exists := mm.cache.GetSubscription(subscriptionId)

if !exists {
arm.WriteError(
w, http.StatusBadRequest,
arm.CloudErrorInvalidSubscriptionState, "",
UnregisteredSubscriptionStateMessage,
subscriptionId)
return
}

mm.Emitter.EmitCounter("frontend_count", 1.0, map[string]string{
"verb": r.Method,
"api_version": r.URL.Query().Get(APIVersionKey),
"code": strconv.Itoa(lrw.statusCode),
"route": routePattern,
"state": string(sub.State),
})
}

// Emit metrics
mm.Emitter.EmitCounter("frontend_count", 1.0, map[string]string{
"verb": r.Method,
Expand Down
Loading