diff --git a/pkg/cloudscale/objectstorage.go b/pkg/cloudscale/objectstorage.go index b882c4f..2b2f12d 100644 --- a/pkg/cloudscale/objectstorage.go +++ b/pkg/cloudscale/objectstorage.go @@ -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" @@ -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, @@ -45,6 +47,7 @@ func NewObjectStorage(client *cloudscale.Client, k8sClient k8s.Client, controlAp clusterId: clusterId, cloudZone: cloudZone, uomMapping: uomMapping, + providerMetrics: providerMetrics, }, nil } @@ -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 diff --git a/pkg/cmd/cloudscale.go b/pkg/cmd/cloudscale.go index 763fecf..38d4a16 100644 --- a/pkg/cmd/cloudscale.go +++ b/pkg/cmd/cloudscale.go @@ -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) } diff --git a/pkg/cmd/exoscale.go b/pkg/cmd/exoscale.go index f0e6fdd..0dba485 100644 --- a/pkg/cmd/exoscale.go +++ b/pkg/cmd/exoscale.go @@ -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) } diff --git a/pkg/exoscale/objectstorage.go b/pkg/exoscale/objectstorage.go index 8f9c819..3733258 100644 --- a/pkg/exoscale/objectstorage.go +++ b/pkg/exoscale/objectstorage.go @@ -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" @@ -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 @@ -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, @@ -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) @@ -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)