-
Notifications
You must be signed in to change notification settings - Fork 324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
API gateway metrics #3811
API gateway metrics #3811
Changes from 10 commits
362dcc7
8ec9391
e9e5150
53bdf97
33ae5cd
76880fb
efe0384
6c09cf1
10aad73
6e7790b
8a096a6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:enhancement | ||
api-gateway: Expose prometheus scrape metrics on api-gateway pods. | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package common | ||
|
||
import ( | ||
"strconv" | ||
|
||
"github.com/hashicorp/consul-k8s/control-plane/api/v1alpha1" | ||
"github.com/hashicorp/consul-k8s/control-plane/connect-inject/constants" | ||
gwv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1" | ||
) | ||
|
||
const ( | ||
defaultScrapePort = 20200 | ||
defaultScrapePath = "/metrics" | ||
) | ||
|
||
type MetricsConfig struct { | ||
Enabled bool | ||
Path string | ||
Port int | ||
} | ||
|
||
func gatewayMetricsEnabled(gateway gwv1beta1.Gateway, gcc v1alpha1.GatewayClassConfig, config HelmConfig) bool { | ||
// first check our annotations, if something is there, then it means we've explicitly | ||
// annotated metrics collection | ||
if scrape, isSet := gateway.Annotations[constants.AnnotationEnableMetrics]; isSet { | ||
return scrape == "true" | ||
andrewstucki marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// if it's not set on the annotation, then we check to see if it's set on the GatewayClassConfig | ||
if gcc.Spec.Metrics.Enabled != nil { | ||
return *gcc.Spec.Metrics.Enabled | ||
} | ||
|
||
// otherwise, fallback to the global helm setting | ||
return config.EnableGatewayMetrics | ||
} | ||
|
||
func fetchPortString(gateway gwv1beta1.Gateway, gcc v1alpha1.GatewayClassConfig, config HelmConfig) string { | ||
// first check our annotations, if something is there, then it means we've explicitly | ||
// annotated metrics collection | ||
if portString, isSet := gateway.Annotations[constants.AnnotationPrometheusScrapePort]; isSet { | ||
return portString | ||
} | ||
|
||
// if it's not set on the annotation, then we check to see if it's set on the GatewayClassConfig | ||
if gcc.Spec.Metrics.Port != nil { | ||
return strconv.Itoa(int(*gcc.Spec.Metrics.Port)) | ||
} | ||
|
||
// otherwise, fallback to the global helm setting | ||
return config.DefaultPrometheusScrapePort | ||
} | ||
|
||
func gatewayMetricsPort(gateway gwv1beta1.Gateway, gcc v1alpha1.GatewayClassConfig, config HelmConfig) int { | ||
portString := fetchPortString(gateway, gcc, config) | ||
|
||
port, err := strconv.Atoi(portString) | ||
if err != nil { | ||
// if we can't parse the port string, just use the default | ||
// TODO: log an error | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you intending to merge with this TODO in place? |
||
return defaultScrapePort | ||
} | ||
|
||
if port < 1024 || port > 65535 { | ||
// if we requested a privileged port, use the default | ||
// TODO: log an error | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same question here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, for now just to get this out the door, we can go back later and pass a logger instance into this if we want to log on an invalid port. |
||
return defaultScrapePort | ||
} | ||
|
||
return port | ||
} | ||
|
||
func gatewayMetricsPath(gateway gwv1beta1.Gateway, gcc v1alpha1.GatewayClassConfig, config HelmConfig) string { | ||
// first check our annotations, if something is there, then it means we've explicitly | ||
// annotated metrics collection | ||
if path, isSet := gateway.Annotations[constants.AnnotationPrometheusScrapePath]; isSet { | ||
return path | ||
} | ||
|
||
// if it's not set on the annotation, then we check to see if it's set on the GatewayClassConfig | ||
if gcc.Spec.Metrics.Path != nil { | ||
return *gcc.Spec.Metrics.Path | ||
} | ||
|
||
// otherwise, fallback to the global helm setting | ||
return config.DefaultPrometheusScrapePath | ||
Comment on lines
+82
to
+94
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we be doing any validation on the path here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideally, yes, but from what I can tell we don't really have solid path validation anywhere else in our implementation (unless you can point me to a prior art?). Was thinking we'd just keep things equivalent for now, but we could see if we could |
||
} | ||
|
||
func GatewayMetricsConfig(gateway gwv1beta1.Gateway, gcc v1alpha1.GatewayClassConfig, config HelmConfig) MetricsConfig { | ||
return MetricsConfig{ | ||
Enabled: gatewayMetricsEnabled(gateway, gcc, config), | ||
Path: gatewayMetricsPath(gateway, gcc, config), | ||
Port: gatewayMetricsPort(gateway, gcc, config), | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.