Skip to content

Commit

Permalink
add subscription state metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
katherinelc321 committed May 22, 2024
1 parent a87ee6d commit 676c5f1
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 9 deletions.
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)
RESOURCE_GROUP ?=
CLUSTER_NAME ?=
DEPLOYMENTNAME=$(RESOURCE_GROUP)
REGION ?= eastus


frontend:
Expand All @@ -31,7 +32,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 "${RESOURCE_GROUP}" != "" || (echo "RESOURCE_GROUP must be defined" && exit 1)
Expand All @@ -50,7 +52,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 ${RESOURCE_GROUP} --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
4 changes: 2 additions & 2 deletions frontend/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,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) *Frontend {
func NewFrontend(logger *slog.Logger, listener net.Listener, emitter metrics.Emitter, region string) *Frontend {
f := &Frontend{
logger: logger,
listener: listener,
Expand All @@ -69,7 +69,7 @@ func NewFrontend(logger *slog.Logger, listener net.Listener, emitter metrics.Emi
done: make(chan struct{}),
}

subscriptionStateMuxValidator := NewSubscriptionStateMuxValidator(&f.cache)
subscriptionStateMuxValidator := NewSubscriptionStateMuxValidator(&f.cache, emitter, region)

// Setup metrics middleware
metricsMiddleware := MetricsMiddleware{Emitter: emitter}
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")
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 @@ -43,7 +50,7 @@ func main() {

// Init prometheus emitter
prometheusEmitter := NewPrometheusEmitter()
frontend := NewFrontend(logger, listener, prometheusEmitter)
frontend := NewFrontend(logger, listener, prometheusEmitter, region)
// Verify the Async DB is available and accessible

logger.Info("Testing DB Access")
Expand Down
18 changes: 15 additions & 3 deletions frontend/middleware_validatesubscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"

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

const (
Expand All @@ -16,12 +17,16 @@ const (
)

type SubscriptionStateMuxValidator struct {
cache *Cache
cache *Cache
metrics metrics.Emitter
region string
}

func NewSubscriptionStateMuxValidator(c *Cache) *SubscriptionStateMuxValidator {
func NewSubscriptionStateMuxValidator(c *Cache, emitter metrics.Emitter, region string) *SubscriptionStateMuxValidator {
return &SubscriptionStateMuxValidator{
cache: c,
cache: c,
metrics: emitter,
region: region,
}
}

Expand Down Expand Up @@ -49,6 +54,13 @@ func (s *SubscriptionStateMuxValidator) MiddlewareValidateSubscriptionState(w ht
return
}

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

// the subscription exists, store its current state as context
ctx := ContextWithSubscriptionState(r.Context(), sub.State)
r = r.WithContext(ctx)
Expand Down
18 changes: 17 additions & 1 deletion frontend/middleware_validatesubscription_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,29 @@ import (
"github.com/google/go-cmp/cmp"

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

type MockEmitter struct{}

func (m *MockEmitter) EmitGauge(metric string, value float64, labels map[string]string) {
// No-op for testing
}

func (m *MockEmitter) EmitCounter(metric string, value float64, labels map[string]string) {
// No-op for testing
}

func NewMockEmitter() metrics.Emitter {
return &MockEmitter{}
}
func TestMiddlewareValidateSubscription(t *testing.T) {
subscriptionId := "1234-5678"
region := "eastus"
defaultRequestPath := fmt.Sprintf("subscriptions/%s/resourceGroups/xyz", subscriptionId)
cache := NewCache()
middleware := NewSubscriptionStateMuxValidator(cache)
mockEmitter := NewMockEmitter()
middleware := NewSubscriptionStateMuxValidator(cache, mockEmitter, region)

tests := []struct {
name string
Expand Down

0 comments on commit 676c5f1

Please sign in to comment.