Skip to content

Commit

Permalink
ensure all providers are feeding metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Jun 24, 2024
1 parent 7f99373 commit f90c327
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
8 changes: 7 additions & 1 deletion pkg/cloudscale/objectstorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"time"

"github.com/prometheus/client_golang/prometheus"
"github.com/vshn/billing-collector-cloudservices/pkg/controlAPI"
"github.com/vshn/billing-collector-cloudservices/pkg/kubernetes"
"github.com/vshn/billing-collector-cloudservices/pkg/log"
Expand All @@ -30,13 +31,14 @@ type ObjectStorage struct {
clusterId string
cloudZone string
uomMapping map[string]string
providerMetrics map[string]prometheus.Counter
}

const (
namespaceLabel = "crossplane.io/claim-namespace"
)

func NewObjectStorage(client *cloudscale.Client, k8sClient k8s.Client, controlApiClient k8s.Client, salesOrder, clusterId string, cloudZone string, uomMapping map[string]string) (*ObjectStorage, error) {
func NewObjectStorage(client *cloudscale.Client, k8sClient k8s.Client, controlApiClient k8s.Client, salesOrder, clusterId string, cloudZone string, uomMapping map[string]string, providerMetrics map[string]prometheus.Counter) (*ObjectStorage, error) {
return &ObjectStorage{
client: client,
k8sClient: k8sClient,
Expand All @@ -45,6 +47,7 @@ func NewObjectStorage(client *cloudscale.Client, k8sClient k8s.Client, controlAp
clusterId: clusterId,
cloudZone: cloudZone,
uomMapping: uomMapping,
providerMetrics: providerMetrics,
}, nil
}

Expand All @@ -56,7 +59,10 @@ func (o *ObjectStorage) GetMetrics(ctx context.Context, billingDate time.Time) (
bucketMetricsRequest := cloudscale.BucketMetricsRequest{Start: billingDate, End: billingDate}
bucketMetrics, err := o.client.Metrics.GetBucketMetrics(ctx, &bucketMetricsRequest)
if err != nil {
o.providerMetrics["providerFailed"].Inc()
return nil, err
} else {
o.providerMetrics["providerSucceeded"].Inc()
}

// Fetch organisations in case salesOrder is missing
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/cloudscale.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func CloudscaleCmds(allMetrics map[string]map[string]prometheus.Counter) *cli.Co
return fmt.Errorf("load loaction: %w", err)
}

o, err := cs.NewObjectStorage(cloudscaleClient, k8sClient, k8sControlClient, salesOrder, clusterId, cloudZone, mapping)
o, err := cs.NewObjectStorage(cloudscaleClient, k8sClient, k8sControlClient, salesOrder, clusterId, cloudZone, mapping, allMetrics["providerMetrics"])
if err != nil {
return fmt.Errorf("object storage: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/exoscale.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func ExoscaleCmds(allMetrics map[string]map[string]prometheus.Counter) *cli.Comm
collectInterval = 23
}

o, err := exoscale.NewObjectStorage(exoscaleClient, k8sClient, k8sControlClient, salesOrder, clusterId, cloudZone, mapping)
o, err := exoscale.NewObjectStorage(exoscaleClient, k8sClient, k8sControlClient, salesOrder, clusterId, cloudZone, mapping, allMetrics["providerMetrics"])
if err != nil {
return fmt.Errorf("objectbucket service: %w", err)
}
Expand Down
11 changes: 10 additions & 1 deletion pkg/exoscale/objectstorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

egoscale "github.com/exoscale/egoscale/v2"
"github.com/exoscale/egoscale/v2/oapi"
"github.com/prometheus/client_golang/prometheus"
"github.com/vshn/billing-collector-cloudservices/pkg/controlAPI"
"github.com/vshn/billing-collector-cloudservices/pkg/exofixtures"
"github.com/vshn/billing-collector-cloudservices/pkg/kubernetes"
Expand All @@ -30,6 +31,7 @@ type ObjectStorage struct {
clusterId string
cloudZone string
uomMapping map[string]string
providerMetrics map[string]prometheus.Counter
}

// BucketDetail a k8s bucket object with relevant data
Expand All @@ -38,7 +40,7 @@ type BucketDetail struct {
}

// NewObjectStorage creates an ObjectStorage with the initial setup
func NewObjectStorage(exoscaleClient *egoscale.Client, k8sClient k8s.Client, controlApiClient k8s.Client, salesOrder, clusterId string, cloudZone string, uomMapping map[string]string) (*ObjectStorage, error) {
func NewObjectStorage(exoscaleClient *egoscale.Client, k8sClient k8s.Client, controlApiClient k8s.Client, salesOrder, clusterId string, cloudZone string, uomMapping map[string]string, providerMetrics map[string]prometheus.Counter) (*ObjectStorage, error) {
return &ObjectStorage{
k8sClient: k8sClient,
exoscaleClient: exoscaleClient,
Expand All @@ -47,13 +49,17 @@ func NewObjectStorage(exoscaleClient *egoscale.Client, k8sClient k8s.Client, con
clusterId: clusterId,
cloudZone: cloudZone,
uomMapping: uomMapping,
providerMetrics: providerMetrics,
}, nil
}

func (o *ObjectStorage) GetMetrics(ctx context.Context) ([]odoo.OdooMeteredBillingRecord, error) {
detail, err := o.fetchManagedBucketsAndNamespaces(ctx)
if err != nil {
o.providerMetrics["providerFailed"].Inc()
return nil, fmt.Errorf("fetchManagedBucketsAndNamespaces: %w", err)
} else {
o.providerMetrics["providerSucceeded"].Inc()
}

metrics, err := o.getBucketUsage(ctx, detail)
Expand All @@ -71,7 +77,10 @@ func (o *ObjectStorage) getBucketUsage(ctx context.Context, bucketDetails []Buck

resp, err := o.exoscaleClient.ListSosBucketsUsageWithResponse(ctx)
if err != nil {
o.providerMetrics["providerFailed"].Inc()
return nil, err
} else {
o.providerMetrics["providerSucceeded"].Inc()
}

odooMetrics, err := o.getOdooMeteredBillingRecords(ctx, *resp.JSON200.SosBucketsUsage, bucketDetails)
Expand Down

0 comments on commit f90c327

Please sign in to comment.