diff --git a/deploy/traits.yaml b/deploy/traits.yaml index 847c167db0..89265aa3d3 100755 --- a/deploy/traits.yaml +++ b/deploy/traits.yaml @@ -621,13 +621,16 @@ traits: - Kubernetes - Knative - OpenShift - description: 'The Prometheus trait configures the Prometheus JMX exporter and exposes - the integration with a `Service`and a `ServiceMonitor` resources so that the Prometheus - endpoint can be scraped.WARNING: The creation of the `ServiceMonitor` resource - requires the https://github.com/coreos/prometheus-operator[Prometheus Operator]custom - resource definition to be installed.You can set `service-monitor` to `false` for - the Prometheus trait to work without the Prometheus operator.It''s disabled by - default.' + description: 'The Prometheus trait configures a Prometheus-compatible endpoint. + It also exposes the integration with a `Service`and a `ServiceMonitor` resources, + so that the endpoint can be scraped automatically, when using the Prometheusoperator.The + metrics exposed vary depending on the configured runtime. With Quarkus, the metrics + are exposedusing MicroProfile Metrics. While with the default runtime, they are + exposed using the Prometheus JMX exporter.WARNING: The creation of the `ServiceMonitor` + resource requires the https://github.com/coreos/prometheus-operator[Prometheus + Operator]custom resource definition to be installed.You can set `service-monitor` + to `false` for the Prometheus trait to work without the Prometheus operator.It''s + disabled by default.' properties: - name: enabled type: bool @@ -635,7 +638,7 @@ traits: property. - name: port type: int - description: The Prometheus endpoint port (default `9779`). + description: The Prometheus endpoint port (default `9779`, or `8080` with Quarkus). - name: service-monitor type: bool description: Whether a `ServiceMonitor` resource is created (default `true`). @@ -645,9 +648,10 @@ traits: is `true`. - name: configmap type: string - description: To use a custom ConfigMap containing the Prometheus exporter configuration - (under the `content` ConfigMap key). When this property is left empty (default),Camel - K generates a standard Prometheus configuration for the integration. + description: To use a custom ConfigMap containing the Prometheus JMX exporter + configuration (under the `content` ConfigMap key).When this property is left + empty (default), Camel K generates a standard Prometheus configuration for the + integration.It is not applicable when using Quarkus. - name: pull-secret platform: false profiles: diff --git a/docs/modules/traits/pages/prometheus.adoc b/docs/modules/traits/pages/prometheus.adoc index fdff7b6791..29e02effb1 100755 --- a/docs/modules/traits/pages/prometheus.adoc +++ b/docs/modules/traits/pages/prometheus.adoc @@ -1,8 +1,12 @@ = Prometheus Trait // Start of autogenerated code - DO NOT EDIT! (description) -The Prometheus trait configures the Prometheus JMX exporter and exposes the integration with a `Service` -and a `ServiceMonitor` resources so that the Prometheus endpoint can be scraped. +The Prometheus trait configures a Prometheus-compatible endpoint. It also exposes the integration with a `Service` +and a `ServiceMonitor` resources, so that the endpoint can be scraped automatically, when using the Prometheus +operator. + +The metrics exposed vary depending on the configured runtime. With Quarkus, the metrics are exposed +using MicroProfile Metrics. While with the default runtime, they are exposed using the Prometheus JMX exporter. WARNING: The creation of the `ServiceMonitor` resource requires the https://github.com/coreos/prometheus-operator[Prometheus Operator] custom resource definition to be installed. @@ -33,7 +37,7 @@ The following configuration options are available: | prometheus.port | int -| The Prometheus endpoint port (default `9779`). +| The Prometheus endpoint port (default `9779`, or `8080` with Quarkus). | prometheus.service-monitor | bool @@ -45,8 +49,9 @@ The following configuration options are available: | prometheus.configmap | string -| To use a custom ConfigMap containing the Prometheus exporter configuration (under the `content` ConfigMap key). When this property is left empty (default), -Camel K generates a standard Prometheus configuration for the integration. +| To use a custom ConfigMap containing the Prometheus JMX exporter configuration (under the `content` ConfigMap key). +When this property is left empty (default), Camel K generates a standard Prometheus configuration for the integration. +It is not applicable when using Quarkus. |=== diff --git a/pkg/trait/prometheus.go b/pkg/trait/prometheus.go index 47bcc6a52a..cffb4f4d78 100644 --- a/pkg/trait/prometheus.go +++ b/pkg/trait/prometheus.go @@ -34,11 +34,15 @@ import ( "github.com/apache/camel-k/pkg/util" ) -// The Prometheus trait configures the Prometheus JMX exporter and exposes the integration with a `Service` -// and a `ServiceMonitor` resources so that the Prometheus endpoint can be scraped. +// The Prometheus trait configures a Prometheus-compatible endpoint. It also exposes the integration with a `Service` +// and a `ServiceMonitor` resources, so that the endpoint can be scraped automatically, when using the Prometheus +// operator. +// +// The metrics exposed vary depending on the configured runtime. With Quarkus, the metrics are exposed +// using MicroProfile Metrics. While with the default runtime, they are exposed using the Prometheus JMX exporter. // // WARNING: The creation of the `ServiceMonitor` resource requires the https://github.com/coreos/prometheus-operator[Prometheus Operator] -//custom resource definition to be installed. +// custom resource definition to be installed. // You can set `service-monitor` to `false` for the Prometheus trait to work without the Prometheus operator. // // It's disabled by default. @@ -46,14 +50,15 @@ import ( // +camel-k:trait=prometheus type prometheusTrait struct { BaseTrait `property:",squash"` - // The Prometheus endpoint port (default `9779`). - Port int `property:"port"` + // The Prometheus endpoint port (default `9779`, or `8080` with Quarkus). + Port *int `property:"port"` // Whether a `ServiceMonitor` resource is created (default `true`). ServiceMonitor bool `property:"service-monitor"` // The `ServiceMonitor` resource labels, applicable when `service-monitor` is `true`. ServiceMonitorLabels string `property:"service-monitor-labels"` - // To use a custom ConfigMap containing the Prometheus exporter configuration (under the `content` ConfigMap key). When this property is left empty (default), - // Camel K generates a standard Prometheus configuration for the integration. + // To use a custom ConfigMap containing the Prometheus JMX exporter configuration (under the `content` ConfigMap key). + // When this property is left empty (default), Camel K generates a standard Prometheus configuration for the integration. + // It is not applicable when using Quarkus. ConfigMap string `property:"configmap"` } @@ -66,7 +71,6 @@ const ( func newPrometheusTrait() Trait { return &prometheusTrait{ BaseTrait: NewBaseTrait("prometheus", 1900), - Port: 9779, ServiceMonitor: true, } } @@ -81,22 +85,28 @@ func (t *prometheusTrait) Configure(e *Environment) (bool, error) { func (t *prometheusTrait) Apply(e *Environment) (err error) { if e.IntegrationInPhase(v1.IntegrationPhaseInitialization) { - // Add the Camel management and Prometheus agent dependencies - util.StringSliceUniqueAdd(&e.Integration.Status.Dependencies, "mvn:org.apache.camel/camel-management") - // TODO: We may want to make the Prometheus version configurable - util.StringSliceUniqueAdd(&e.Integration.Status.Dependencies, "mvn:io.prometheus.jmx/jmx_prometheus_javaagent:0.3.1") + switch e.CamelCatalog.Runtime.Provider { + case v1.RuntimeProviderQuarkus: + // Add the Camel Quarkus MP Metrics extension + util.StringSliceUniqueAdd(&e.Integration.Status.Dependencies, "mvn:org.apache.camel.quarkus/camel-quarkus-microprofile-metrics") + case v1.RuntimeProviderMain: + // Add the Camel management and Prometheus agent dependencies + util.StringSliceUniqueAdd(&e.Integration.Status.Dependencies, "mvn:org.apache.camel/camel-management") + // TODO: We may want to make the Prometheus version configurable + util.StringSliceUniqueAdd(&e.Integration.Status.Dependencies, "mvn:io.prometheus.jmx/jmx_prometheus_javaagent:0.3.1") - // Use the provided configuration or add the default Prometheus JMX exporter configuration - configMapName := t.getJmxExporterConfigMapOrAdd(e) + // Use the provided configuration or add the default Prometheus JMX exporter configuration + configMapName := t.getJmxExporterConfigMapOrAdd(e) - e.Integration.Status.AddOrReplaceGeneratedResources(v1.ResourceSpec{ - Type: v1.ResourceTypeData, - DataSpec: v1.DataSpec{ - Name: prometheusJmxExporterConfigFileName, - ContentRef: configMapName, - }, - MountPath: prometheusJmxExporterConfigMountPath, - }) + e.Integration.Status.AddOrReplaceGeneratedResources(v1.ResourceSpec{ + Type: v1.ResourceTypeData, + DataSpec: v1.DataSpec{ + Name: prometheusJmxExporterConfigFileName, + ContentRef: configMapName, + }, + MountPath: prometheusJmxExporterConfigMountPath, + }) + } return nil } @@ -117,9 +127,20 @@ func (t *prometheusTrait) Apply(e *Environment) (err error) { Reason: v1.IntegrationConditionPrometheusAvailableReason, } - // Configure the Prometheus Java agent - options := []string{strconv.Itoa(t.Port), path.Join(prometheusJmxExporterConfigMountPath, prometheusJmxExporterConfigFileName)} - container.Args = append(container.Args, "-javaagent:dependencies/io.prometheus.jmx.jmx_prometheus_javaagent-0.3.1.jar="+strings.Join(options, ":")) + var port int + switch e.CamelCatalog.Runtime.Provider { + case v1.RuntimeProviderQuarkus: + port = 8080 + case v1.RuntimeProviderMain: + port = 9779 + // Configure the Prometheus Java agent + options := []string{strconv.Itoa(port), path.Join(prometheusJmxExporterConfigMountPath, prometheusJmxExporterConfigFileName)} + container.Args = append(container.Args, "-javaagent:dependencies/io.prometheus.jmx.jmx_prometheus_javaagent-0.3.1.jar="+strings.Join(options, ":")) + } + + if t.Port == nil { + t.Port = &port + } // Configure the Prometheus container port containerPort := t.getContainerPort() @@ -179,7 +200,7 @@ func (t *prometheusTrait) Apply(e *Environment) (err error) { func (t *prometheusTrait) getContainerPort() *corev1.ContainerPort { containerPort := corev1.ContainerPort{ - ContainerPort: int32(t.Port), + ContainerPort: int32(*t.Port), Protocol: corev1.ProtocolTCP, } return &containerPort @@ -188,10 +209,10 @@ func (t *prometheusTrait) getContainerPort() *corev1.ContainerPort { func (t *prometheusTrait) getServicePort() *corev1.ServicePort { servicePort := corev1.ServicePort{ Name: prometheusPortName, - Port: int32(t.Port), + Port: int32(*t.Port), Protocol: corev1.ProtocolTCP, // Avoid relying on named port, as Knative enforces specific values used for content negotiation - TargetPort: intstr.FromInt(t.Port), + TargetPort: intstr.FromInt(*t.Port), } return &servicePort } diff --git a/pkg/trait/prometheus_test.go b/pkg/trait/prometheus_test.go index 9d5e921d7e..6eb6db5d22 100644 --- a/pkg/trait/prometheus_test.go +++ b/pkg/trait/prometheus_test.go @@ -30,6 +30,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" v1 "github.com/apache/camel-k/pkg/apis/camel/v1" + "github.com/apache/camel-k/pkg/util/camel" "github.com/apache/camel-k/pkg/util/kubernetes" ) @@ -172,8 +173,14 @@ func createNominalPrometheusTest() (*prometheusTrait, *Environment) { enabled := true trait.Enabled = &enabled + camelCatalog, err := camel.DefaultCatalog() + if err != nil { + panic(err) + } + environment := &Environment{ - Catalog: NewCatalog(context.TODO(), nil), + Catalog: NewCatalog(context.TODO(), nil), + CamelCatalog: camelCatalog, Integration: &v1.Integration{ ObjectMeta: metav1.ObjectMeta{ Namespace: "integration-namespace",