Skip to content

Commit

Permalink
Merge pull request #148 from appuio/feat/appuio_managed_queries
Browse files Browse the repository at this point in the history
Add queries for APPUiO Managed billing
  • Loading branch information
HappyTetrahedron authored Aug 22, 2023
2 parents fd1d44e + d89c741 commit affd316
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 13 deletions.
9 changes: 9 additions & 0 deletions pkg/db/seeds.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ var appuioCloudPersistentStorageQuery string
//go:embed seeds/appuio_managed_openshift_vcpu.promql
var appuioManagedOpenShiftvCPUQuery string

//go:embed seeds/appuio_managed_openshift_clusters_legacy.promql
var appuioManagedOpenShiftClusterQuery string

//go:embed seeds/appuio_managed_kubernetes_vcpu.promql
var appuioManagedKubernetesvCPUQuery string

Expand Down Expand Up @@ -71,6 +74,12 @@ var DefaultQueries = []Query{
Query: appuioManagedOpenShiftvCPUQuery,
Unit: "vCPU",
},
{
Name: "appuio_managed_openshift_clusters",
Description: "Cluster base fee",
Query: appuioManagedOpenShiftClusterQuery,
Unit: "Clusters",
},
{
Name: "appuio_managed_kubernetes_vcpu",
Description: "vCPU aggregated by cluster, node (app, storage), and service level",
Expand Down
39 changes: 39 additions & 0 deletions pkg/db/seeds/appuio_managed_openshift_clusters_legacy.promql
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Calculates number of clusters per cluster, but only for clusters with old billing
# Yes, this is always 1
# Structure of resulting product label "query:cloud:tenant:cluster:sla"

# Max values over one hour.
max_over_time(
# Add the final product label by joining the base product with the cluster ID, the tenant, and the service class.
label_join(
label_join(
label_replace(
# Add the base product identifier.
label_replace(
max by(cluster_id, vshn_service_level, tenant_id, cloud_provider) (
appuio_managed_info{vshn_service_level=~"(zero|standard|professional|premium)"}
),
"product",
"appuio_managed_openshift_clusters",
"",
".*"
),
"class",
"$0",
"vshn_service_level",
"(.*)"
),
"product",
":",
"product",
"cloud_provider",
"tenant_id",
"cluster_id",
"class"
),
"category",
":",
"tenant_id",
"cluster_id"
)[58m:1m]
)
20 changes: 11 additions & 9 deletions pkg/db/seeds/appuio_managed_openshift_vcpu.promql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Calculates vCPUs for app nodes of a cluster
# Structure of resulting product label "query:cluster:tenant::class"
# Structure of resulting product label "query:cloud:tenant:cluster:sla:role"

# Max values over one hour.
max_over_time(
Expand All @@ -9,14 +9,15 @@ max_over_time(
label_replace(
# Add the base product identifier.
label_replace(
sum by(cluster_id, vshn_service_level, tenant_id, role) (
min without(prometheus_replica) (node_cpu_info) * on (tenant_id, cluster_id, instance, vshn_service_level) group_left(role)
sum by(cluster_id, vshn_service_level, tenant_id, role, cloud_provider) (
node_cpu_info * on (tenant_id, cluster_id, instance) group_left(role)
label_join(
(group without(prometheus_replica) (kube_node_role{role=~"app|storage"})),
kube_node_role{role=~"app|storage"},
"instance",
"",
"node"
)
) * on(cluster_id) group_left(tenant_id, vshn_service_level, cloud_provider)
max by(cluster_id, tenant_id, vshn_service_level, cloud_provider)(appuio_managed_info)
),
"product",
"appuio_managed_openshift_vcpu",
Expand All @@ -31,14 +32,15 @@ max_over_time(
"product",
":",
"product",
"cluster_id",
"cloud_provider",
"tenant_id",
"role", # empty namespace
"class"
"cluster_id",
"class",
"role"
),
"category",
":",
"tenant_id",
"cluster_id"
)[59m:1m]
)
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
local c = import 'common.libsonnet';

local query = importstr '../appuio_managed_openshift_clusters_legacy.promql';

local commonLabels = {
cluster_id: 'c-managed-openshift',
};

local infoLabels = commonLabels {
tenant_id: 't-managed-openshift',
vshn_service_level: 'standard',
cloud_provider: 'cloudscale',
};

local baseSeries = {
appuioInfoLabel: c.series('appuio_managed_info', infoLabels, '1x120'),
appuioInfoLabel2: c.series('appuio_managed_info', infoLabels {
vshn_service_level: 'best_effort',
}, '1x120'),
};

local baseCalculatedLabels = infoLabels {
class: super.vshn_service_level,
category: super.tenant_id + ':' + super.cluster_id,
};

{
tests: [
c.test(
'one cluster',
baseSeries,
query,
[
{
labels: c.formatLabels(baseCalculatedLabels {
product: 'appuio_managed_openshift_clusters:cloudscale:t-managed-openshift:c-managed-openshift:standard',
}),
value: 1,
},
]
),

],
}
12 changes: 9 additions & 3 deletions pkg/db/seeds/promtest/appuio_managed_openshift_vcpu.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ local query = importstr '../appuio_managed_openshift_vcpu.promql';

local commonLabels = {
cluster_id: 'c-managed-openshift',
};

local infoLabels = commonLabels {
tenant_id: 't-managed-openshift',
vshn_service_level: 'ondemand',
cloud_provider: 'cloudscale',
};

local baseSeries = {
Expand All @@ -32,9 +36,11 @@ local baseSeries = {
instance: 'storage-test',
core: '0',
}, '1x120'),

appuioInfoLabel: c.series('appuio_managed_info', infoLabels, '1x120'),
};

local baseCalculatedLabels = commonLabels {
local baseCalculatedLabels = infoLabels {
class: super.vshn_service_level,
category: super.tenant_id + ':' + super.cluster_id,
};
Expand All @@ -49,14 +55,14 @@ local baseCalculatedLabels = commonLabels {
{
labels: c.formatLabels(baseCalculatedLabels {
role: 'app',
product: 'appuio_managed_openshift_vcpu:c-managed-openshift:t-managed-openshift:app:ondemand',
product: 'appuio_managed_openshift_vcpu:cloudscale:t-managed-openshift:c-managed-openshift:ondemand:app',
}),
value: 2,
},
{
labels: c.formatLabels(baseCalculatedLabels {
role: 'storage',
product: 'appuio_managed_openshift_vcpu:c-managed-openshift:t-managed-openshift:storage:ondemand',
product: 'appuio_managed_openshift_vcpu:cloudscale:t-managed-openshift:c-managed-openshift:ondemand:storage',
}),
value: 1,
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/db/seeds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (s *SeedsTestSuite) TestSeedDefaultQueries() {
_, err := d.Exec("DELETE FROM queries")
require.NoError(t, err)

expQueryNum := 8
expQueryNum := 9

count := "SELECT COUNT(*) FROM queries"
requireQueryEqual(t, d, 0, count)
Expand Down

0 comments on commit affd316

Please sign in to comment.