Skip to content

Commit 6027059

Browse files
committed
Merge branch '2922-configure-exporter-queries-and-configuration' into 'main'
Resolve "Configure Exporter Queries And Configuration" Closes #2922 See merge request ongresinc/stackgres!1662
2 parents 8f76b63 + 6feea00 commit 6027059

11 files changed

Lines changed: 321 additions & 39 deletions

File tree

stackgres-k8s/src/common/src/main/java/io/stackgres/common/crd/sgcluster/StackGresClusterConfigurations.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ public class StackGresClusterConfigurations {
4646
@Valid
4747
private StackGresClusterObservability observability;
4848

49+
@Valid
50+
private StackGresClusterPostgresExporter postgresExporter;
51+
4952
@ReferencedField("sgPostgresConfig")
5053
interface SgPostgresConfig extends FieldReference {
5154
}
@@ -114,10 +117,18 @@ public void setObservability(StackGresClusterObservability observability) {
114117
this.observability = observability;
115118
}
116119

120+
public StackGresClusterPostgresExporter getPostgresExporter() {
121+
return postgresExporter;
122+
}
123+
124+
public void setPostgresExporter(StackGresClusterPostgresExporter postgresExporter) {
125+
this.postgresExporter = postgresExporter;
126+
}
127+
117128
@Override
118129
public int hashCode() {
119-
return Objects.hash(backups, binding, credentials, observability, patroni, sgPoolingConfig,
120-
sgPostgresConfig);
130+
return Objects.hash(backups, binding, credentials, observability, patroni, postgresExporter,
131+
sgPoolingConfig, sgPostgresConfig);
121132
}
122133

123134
@Override
@@ -133,6 +144,7 @@ public boolean equals(Object obj) {
133144
&& Objects.equals(credentials, other.credentials)
134145
&& Objects.equals(observability, other.observability)
135146
&& Objects.equals(patroni, other.patroni)
147+
&& Objects.equals(postgresExporter, other.postgresExporter)
136148
&& Objects.equals(sgPoolingConfig, other.sgPoolingConfig)
137149
&& Objects.equals(sgPostgresConfig, other.sgPostgresConfig);
138150
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (C) 2019 OnGres, Inc.
3+
* SPDX-License-Identifier: AGPL-3.0-or-later
4+
*/
5+
6+
package io.stackgres.common.crd.sgcluster;
7+
8+
import java.util.Objects;
9+
10+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
11+
import com.fasterxml.jackson.annotation.JsonInclude;
12+
import io.quarkus.runtime.annotations.RegisterForReflection;
13+
import io.stackgres.common.StackGresUtil;
14+
15+
@RegisterForReflection
16+
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
17+
@JsonIgnoreProperties(ignoreUnknown = true)
18+
public class StackGresClusterPostgresExporter {
19+
20+
private StackGresClusterPostgresExporterQueries queries;
21+
22+
public StackGresClusterPostgresExporterQueries getQueries() {
23+
return queries;
24+
}
25+
26+
public void setQueries(StackGresClusterPostgresExporterQueries queries) {
27+
this.queries = queries;
28+
}
29+
30+
@Override
31+
public int hashCode() {
32+
return Objects.hash(queries);
33+
}
34+
35+
@Override
36+
public boolean equals(Object obj) {
37+
if (this == obj) {
38+
return true;
39+
}
40+
if (!(obj instanceof StackGresClusterPostgresExporter)) {
41+
return false;
42+
}
43+
StackGresClusterPostgresExporter other = (StackGresClusterPostgresExporter) obj;
44+
return Objects.equals(queries, other.queries);
45+
}
46+
47+
public String toString() {
48+
return StackGresUtil.toPrettyYaml(this);
49+
}
50+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (C) 2019 OnGres, Inc.
3+
* SPDX-License-Identifier: AGPL-3.0-or-later
4+
*/
5+
6+
package io.stackgres.common.crd.sgcluster;
7+
8+
import java.util.Map;
9+
10+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
11+
import com.fasterxml.jackson.annotation.JsonInclude;
12+
import io.quarkus.runtime.annotations.RegisterForReflection;
13+
import io.stackgres.common.StackGresUtil;
14+
import io.stackgres.common.crd.JsonObject;
15+
16+
@RegisterForReflection
17+
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
18+
@JsonIgnoreProperties(ignoreUnknown = true)
19+
public class StackGresClusterPostgresExporterQueries extends JsonObject {
20+
21+
public StackGresClusterPostgresExporterQueries() {
22+
super();
23+
}
24+
25+
public StackGresClusterPostgresExporterQueries(Map<String, Object> m) {
26+
super(m);
27+
}
28+
29+
public String toString() {
30+
return StackGresUtil.toPrettyYaml(this);
31+
}
32+
}

stackgres-k8s/src/common/src/main/java/io/stackgres/common/crd/sgshardedcluster/StackGresShardedClusterConfigurations.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import io.stackgres.common.StackGresUtil;
1515
import io.stackgres.common.crd.sgcluster.StackGresClusterCredentials;
1616
import io.stackgres.common.crd.sgcluster.StackGresClusterObservability;
17+
import io.stackgres.common.crd.sgcluster.StackGresClusterPostgresExporter;
1718
import io.stackgres.common.crd.sgcluster.StackGresClusterServiceBinding;
1819
import io.sundr.builder.annotations.Buildable;
1920
import jakarta.validation.Valid;
@@ -38,6 +39,9 @@ public class StackGresShardedClusterConfigurations {
3839
@Valid
3940
private StackGresClusterObservability observability;
4041

42+
@Valid
43+
private StackGresClusterPostgresExporter postgresExporter;
44+
4145
public List<StackGresShardedClusterBackupConfiguration> getBackups() {
4246
return backups;
4347
}
@@ -70,9 +74,17 @@ public void setObservability(StackGresClusterObservability observability) {
7074
this.observability = observability;
7175
}
7276

77+
public StackGresClusterPostgresExporter getPostgresExporter() {
78+
return postgresExporter;
79+
}
80+
81+
public void setPostgresExporter(StackGresClusterPostgresExporter postgresExporter) {
82+
this.postgresExporter = postgresExporter;
83+
}
84+
7385
@Override
7486
public int hashCode() {
75-
return Objects.hash(backups, binding, credentials, observability);
87+
return Objects.hash(backups, binding, credentials, observability, postgresExporter);
7688
}
7789

7890
@Override
@@ -86,7 +98,8 @@ public boolean equals(Object obj) {
8698
StackGresShardedClusterConfigurations other = (StackGresShardedClusterConfigurations) obj;
8799
return Objects.equals(backups, other.backups) && Objects.equals(binding, other.binding)
88100
&& Objects.equals(credentials, other.credentials)
89-
&& Objects.equals(observability, other.observability);
101+
&& Objects.equals(observability, other.observability)
102+
&& Objects.equals(postgresExporter, other.postgresExporter);
90103
}
91104

92105
@Override

stackgres-k8s/src/common/src/main/resources/crds/SGCluster.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,19 @@ spec:
10801080
If not set, a default configuration will be used. Disabling connection pooling altogether is possible if the disableConnectionPooling property of the pods object is set to true.
10811081
10821082
**Changing this field may require a restart.**
1083+
postgresExporter:
1084+
type: object
1085+
description: Section to configure the [postgres_exporter](https://github.com/prometheus-community/postgres_exporter) component.
1086+
properties:
1087+
queries:
1088+
type: object
1089+
description: |
1090+
Section to configure the [postgres_exporter](https://github.com/prometheus-community/postgres_exporter) queries.yaml configuration.
1091+
1092+
The queries defined here will overwrite those created by the operator.
1093+
1094+
**WARNING**: Changing this may beak some of the functionality that depend on the query overwritten.
1095+
"x-kubernetes-preserve-unknown-fields": true
10831096
observability:
10841097
type: object
10851098
description: Allow to specify Observability configuration (related to logs, metrics and traces)

stackgres-k8s/src/common/src/main/resources/crds/SGShardedCluster.yaml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,19 @@ spec:
563563
- sgObjectStorage: 'backupconf'
564564
```
565565
properties:
566+
postgresExporter:
567+
type: object
568+
description: Section to configure the [postgres_exporter](https://github.com/prometheus-community/postgres_exporter) component.
569+
properties:
570+
queries:
571+
type: object
572+
description: |
573+
Section to configure the [postgres_exporter](https://github.com/prometheus-community/postgres_exporter) queries.yaml configuration.
574+
575+
The queries defined here will overwrite those created by the operator.
576+
577+
**WARNING**: Changing this may beak some of the functionality that depend on the query overwritten.
578+
"x-kubernetes-preserve-unknown-fields": true
566579
observability:
567580
type: object
568581
description: Allow to specify Observability configuration (related to logs, metrics and traces)
@@ -1443,6 +1456,19 @@ spec:
14431456
If sharding type is `shardingsphere` then this field is ignored.
14441457

14451458
**Changing this field may require a restart.**
1459+
postgresExporter:
1460+
type: object
1461+
description: Section to configure the [postgres_exporter](https://github.com/prometheus-community/postgres_exporter) component.
1462+
properties:
1463+
queries:
1464+
type: object
1465+
description: |
1466+
Section to configure the [postgres_exporter](https://github.com/prometheus-community/postgres_exporter) queries.yaml configuration.
1467+
1468+
The queries defined here will overwrite those created by the operator.
1469+
1470+
**WARNING**: Changing this may beak some of the functionality that depend on the query overwritten.
1471+
"x-kubernetes-preserve-unknown-fields": true
14461472
patroni: &patroni
14471473
type: object
14481474
description: |
@@ -2086,6 +2112,19 @@ spec:
20862112
If not set, a default configuration will be used. Disabling connection pooling altogether is possible if the disableConnectionPooling property of the pods object is set to true.
20872113

20882114
**Changing this field may require a restart.**
2115+
postgresExporter:
2116+
type: object
2117+
description: Section to configure the [postgres_exporter](https://github.com/prometheus-community/postgres_exporter) component.
2118+
properties:
2119+
queries:
2120+
type: object
2121+
description: |
2122+
Section to configure the [postgres_exporter](https://github.com/prometheus-community/postgres_exporter) queries.yaml configuration.
2123+
2124+
The queries defined here will overwrite those created by the operator.
2125+
2126+
**WARNING**: Changing this may beak some of the functionality that depend on the query overwritten.
2127+
"x-kubernetes-preserve-unknown-fields": true
20892128
patroni: *patroni
20902129
replication: *replication
20912130
metadata:
@@ -2384,6 +2423,19 @@ spec:
23842423
Name of the [SGPoolingConfig](https://stackgres.io/doc/latest/reference/crd/sgpoolconfig) used for this cluster. Each pod contains a sidecar with a connection pooler (currently: [PgBouncer](https://www.pgbouncer.org/)). The connection pooler is implemented as a sidecar.
23852424
23862425
If not set, a default configuration will be used. Disabling connection pooling altogether is possible if the disableConnectionPooling property of the pods object is set to true.
2426+
postgresExporter:
2427+
type: object
2428+
description: Section to configure the [postgres_exporter](https://github.com/prometheus-community/postgres_exporter) component.
2429+
properties:
2430+
queries:
2431+
type: object
2432+
description: |
2433+
Section to configure the [postgres_exporter](https://github.com/prometheus-community/postgres_exporter) queries.yaml configuration.
2434+
2435+
The queries defined here will overwrite those created by the operator.
2436+
2437+
**WARNING**: Changing this may beak some of the functionality that depend on the query overwritten.
2438+
"x-kubernetes-preserve-unknown-fields": true
23872439
replication: *replication
23882440
metadata:
23892441
type: object

stackgres-k8s/src/operator/src/main/java/io/stackgres/operator/conciliation/factory/cluster/sidecars/pgexporter/PostgresExporter.java

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,12 @@
77

88
import static io.stackgres.common.StackGresUtil.getDefaultPullPolicy;
99

10-
import java.io.IOException;
1110
import java.nio.charset.StandardCharsets;
1211
import java.util.Map;
1312
import java.util.Objects;
1413
import java.util.Optional;
1514
import java.util.stream.Stream;
1615

17-
import com.fasterxml.jackson.core.JsonProcessingException;
1816
import com.fasterxml.jackson.databind.node.ObjectNode;
1917
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
2018
import com.google.common.io.Resources;
@@ -37,7 +35,9 @@
3735
import io.stackgres.common.crd.VolumeBuilder;
3836
import io.stackgres.common.crd.VolumeMountBuilder;
3937
import io.stackgres.common.crd.sgcluster.StackGresCluster;
38+
import io.stackgres.common.crd.sgcluster.StackGresClusterConfigurations;
4039
import io.stackgres.common.crd.sgcluster.StackGresClusterPods;
40+
import io.stackgres.common.crd.sgcluster.StackGresClusterPostgresExporter;
4141
import io.stackgres.common.crd.sgcluster.StackGresClusterSpec;
4242
import io.stackgres.common.labels.LabelFactoryForCluster;
4343
import io.stackgres.operator.common.Sidecar;
@@ -218,33 +218,6 @@ private HasMetadata buildSource(StackGresClusterContext context) {
218218
.map(StackGresClusterPods::getDisableConnectionPooling)
219219
.orElse(false);
220220

221-
if (pgBouncerIsDisabled) {
222-
final YAMLMapper yamlMapper = yamlMapperProvider.get();
223-
final ObjectNode queries;
224-
final String data;
225-
try {
226-
queries = (ObjectNode) yamlMapper
227-
.readTree(PostgresExporter.class.getResource("/prometheus-postgres-exporter/queries.yaml"));
228-
229-
var fieldNames = Seq.seq(queries.fieldNames()).toList();
230-
for (var fieldName : fieldNames) {
231-
if (fieldName.startsWith(POSTGRES_EXPORTER_PGBOUNCER_QUERIES_PREFIX)) {
232-
queries.remove(fieldName);
233-
}
234-
}
235-
236-
data = yamlMapper.writeValueAsString(queries);
237-
238-
builder.withData(Map.of(QUERIES_YAML, data));
239-
240-
return builder.build();
241-
} catch (JsonProcessingException e) {
242-
throw new RuntimeException("couldn't serialize prometheus postgres exporter queries to a string", e);
243-
} catch (IOException e) {
244-
throw new RuntimeException("couldn't read prometheus postgres exporter queries file", e);
245-
}
246-
}
247-
248221
final String queriesResourcePath;
249222

250223
final long versionAsNumber = StackGresVersion.getStackGresVersionAsNumber(context.getCluster());
@@ -254,13 +227,47 @@ private HasMetadata buildSource(StackGresClusterContext context) {
254227
queriesResourcePath = "/prometheus-postgres-exporter/queries.yaml";
255228
}
256229

230+
final YAMLMapper yamlMapper = yamlMapperProvider.get();
231+
final ObjectNode queries;
232+
final String data;
233+
try {
234+
queries = (ObjectNode) yamlMapper
235+
.readTree(Unchecked.supplier(
236+
() -> Resources.asCharSource(
237+
Objects.requireNonNull(PostgresExporter.class.getResource(queriesResourcePath)),
238+
StandardCharsets.UTF_8)
239+
.read()).get());
240+
} catch (Exception ex) {
241+
throw new RuntimeException("Error while deserializing " + queriesResourcePath, ex);
242+
}
243+
244+
var fieldNames = Seq.seq(queries.fieldNames()).toList();
245+
if (pgBouncerIsDisabled) {
246+
for (var fieldName : fieldNames) {
247+
if (fieldName.startsWith(POSTGRES_EXPORTER_PGBOUNCER_QUERIES_PREFIX)) {
248+
queries.remove(fieldName);
249+
}
250+
}
251+
}
252+
253+
Optional<ObjectNode> customQueriesFound = Optional.of(context.getCluster())
254+
.map(StackGresCluster::getSpec)
255+
.map(StackGresClusterSpec::getConfigurations)
256+
.map(StackGresClusterConfigurations::getPostgresExporter)
257+
.map(StackGresClusterPostgresExporter::getQueries)
258+
.map(yamlMapper::valueToTree);
259+
260+
customQueriesFound.ifPresent(customQueries -> Seq.seq(customQueries.fields())
261+
.forEach(customQuery -> queries.set(customQuery.getKey(), customQuery.getValue())));
262+
263+
try {
264+
data = yamlMapper.writeValueAsString(queries);
265+
} catch (Exception ex) {
266+
throw new RuntimeException("Error while serializing queries", ex);
267+
}
268+
257269
return builder
258-
.withData(Map.of(QUERIES_YAML,
259-
Unchecked.supplier(() ->
260-
Resources
261-
.asCharSource(
262-
Objects.requireNonNull(PostgresExporter.class.getResource(queriesResourcePath)),
263-
StandardCharsets.UTF_8).read()).get()))
270+
.withData(Map.of(QUERIES_YAML, data))
264271
.build();
265272
}
266273

0 commit comments

Comments
 (0)