diff --git a/oraclecloud-micrometer/src/main/java/io/micronaut/oraclecloud/monitoring/micrometer/OracleCloudConfig.java b/oraclecloud-micrometer/src/main/java/io/micronaut/oraclecloud/monitoring/micrometer/OracleCloudConfig.java index 260e41a25..36191a409 100644 --- a/oraclecloud-micrometer/src/main/java/io/micronaut/oraclecloud/monitoring/micrometer/OracleCloudConfig.java +++ b/oraclecloud-micrometer/src/main/java/io/micronaut/oraclecloud/monitoring/micrometer/OracleCloudConfig.java @@ -111,6 +111,10 @@ default Validated validate() { "must match pattern '" + NAMESPACE_REGEX + "'", InvalidReason.MALFORMED); } + if (namespace().startsWith("oci_")) { + return Validated.invalid(prefix() + ".namespace", namespace(), + "cannot start with 'oci_'", InvalidReason.MALFORMED); + } return Validated.valid(prefix() + ".namespace", namespace()); }); } diff --git a/oraclecloud-micrometer/src/test/groovy/io/micronaut/oraclecloud/monitoring/OracleCloudMeterRegistryFactorySpec.groovy b/oraclecloud-micrometer/src/test/groovy/io/micronaut/oraclecloud/monitoring/OracleCloudMeterRegistryFactorySpec.groovy index f6fbad42f..f792b2c20 100644 --- a/oraclecloud-micrometer/src/test/groovy/io/micronaut/oraclecloud/monitoring/OracleCloudMeterRegistryFactorySpec.groovy +++ b/oraclecloud-micrometer/src/test/groovy/io/micronaut/oraclecloud/monitoring/OracleCloudMeterRegistryFactorySpec.groovy @@ -20,17 +20,25 @@ class OracleCloudMeterRegistryFactorySpec extends Specification { @Shared String compartmentOcid = System.getenv("MONITORING_COMPARTMENT_OCID") + @Shared + String configProfile = System.getenv("MONITORING_CONFIG_PROFILE") + def "test it publish metrics to ingestion telemetry endpoint"() { given: def confVariables = [ + "micronaut.metrics.export.oraclecloud.enabled" : true, + "micronaut.metrics.export.oraclecloud.step" : "1s", "micronaut.metrics.export.oraclecloud.namespace" : "micronaut_test", - "micronaut.metrics.export.oraclecloud.applicationName": "micronaut_test", + "micronaut.metrics.export.oraclecloud.applicationName": "micronaut_test" ] if (compartmentOcid != null && !compartmentOcid.isEmpty()) { confVariables["micronaut.metrics.export.oraclecloud.compartmentId"] = compartmentOcid } + if (configProfile != null && !configProfile.isEmpty()) { + confVariables["oci.config.profile"] = configProfile + } ApplicationContext context = ApplicationContext.run(confVariables, Environment.ORACLE_CLOUD) OracleCloudMeterRegistry cloudMeterRegistry = context.getBean(OracleCloudMeterRegistry) @@ -41,7 +49,7 @@ class OracleCloudMeterRegistryFactorySpec extends Specification { description("Testing of micronaut-oraclecloud-monitoring module"). register(cloudMeterRegistry) counter.increment(5.0) - sleep(2000) + sleep(6000) then: noExceptionThrown() diff --git a/oraclecloud-micrometer/src/test/groovy/io/micronaut/oraclecloud/monitoring/micrometer/OracleCloudNamespaceValidationSpec.groovy b/oraclecloud-micrometer/src/test/groovy/io/micronaut/oraclecloud/monitoring/micrometer/OracleCloudNamespaceValidationSpec.groovy new file mode 100644 index 000000000..1e680bbc4 --- /dev/null +++ b/oraclecloud-micrometer/src/test/groovy/io/micronaut/oraclecloud/monitoring/micrometer/OracleCloudNamespaceValidationSpec.groovy @@ -0,0 +1,45 @@ +package io.micronaut.oraclecloud.monitoring.micrometer + +import io.micronaut.context.ApplicationContext +import jakarta.inject.Inject +import spock.lang.Specification + +class OracleCloudNamespaceValidationSpec extends Specification { + + @Inject + ApplicationContext context + + def "test valid namespaces"() { + given: + var pattern = OracleCloudConfig.NAMESPACE_PATTERN + + expect: + pattern.matcher(string).matches() + + where: + string | _ + "namespace" | _ + "namespace123" | _ + "my_space_123" | _ + "example_namespace" | _ + } + + def "test invalid namespaces"() { + given: + var pattern = OracleCloudConfig.NAMESPACE_PATTERN + + expect: + !pattern.matcher(string).matches() + + where: + string | _ + "one&two" | _ + "-namespace" | _ + "Namespace%Example" | _ + "123-my-space" | _ + "NAMESPACE" | _ + "example-namespace" | _ + "EXAMPLE-Namespace-example123" | _ + "my-space-123" | _ + } +}