Skip to content
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

Expose the Prometheus exporter port when specified in the config #1953

Merged
merged 12 commits into from
Aug 1, 2023
16 changes: 16 additions & 0 deletions .chloggen/1689-expose-prometheusexporter-pod-when-enabled.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. operator, target allocator, github action)
component: operator

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Expose the Prometheus exporter port in the OpenTelemetry Collector container when it is used in the configuration.

# One or more tracking issues related to the change
issues: [1689]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
42 changes: 42 additions & 0 deletions pkg/collector/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,18 @@ func getConfigContainerPorts(logger logr.Logger, cfg string) map[string]corev1.C
ContainerPort: metricsPort,
Protocol: corev1.ProtocolTCP,
}

promExporterPort, err := getPrometheusExporterPort(c)
if err != nil {
logger.V(2).Info("prometheus exporter port not detected")
} else {
ports["promexporter"] = corev1.ContainerPort{
Name: "promexporter",
ContainerPort: promExporterPort,
Protocol: corev1.ProtocolTCP,
}
}

return ports
}

Expand Down Expand Up @@ -223,6 +235,36 @@ func getMetricsPort(c map[interface{}]interface{}) (int32, error) {
return int32(i64), nil
}

func getPrometheusExporterPort(c map[interface{}]interface{}) (int32, error) {
// we don't need to unmarshal the whole config, just follow the keys down to
// the prometheus endpoint.
type prometheusCfg struct {
Endpoint string
}
type exportersCfg struct {
Prometheus prometheusCfg
}
type cfg struct {
Exporters exportersCfg
}
var cOut cfg
err := mapstructure.Decode(c, &cOut)
if err != nil {
return 0, nil
}

_, port, err := net.SplitHostPort(cOut.Exporters.Prometheus.Endpoint)
iblancasa marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return 0, err
}
i64, err := strconv.ParseInt(port, 10, 32)
if err != nil {
return 0, err
}

return int32(i64), nil
}

func portMapToList(portMap map[string]corev1.ContainerPort) []corev1.ContainerPort {
ports := make([]corev1.ContainerPort, 0, len(portMap))
for _, p := range portMap {
Expand Down
15 changes: 15 additions & 0 deletions pkg/collector/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,21 @@ service:
},
},
},
{
description: "prometheus exporter ",
specConfig: `exporters:
prometheus:
endpoint: "0.0.0.0:9090"`,
specPorts: []corev1.ServicePort{},
expectedPorts: []corev1.ContainerPort{
metricContainerPort,
{
Name: "promexporter",
ContainerPort: 9090,
Protocol: corev1.ProtocolTCP,
},
},
},
}

for _, testCase := range tests {
Expand Down