Skip to content

Commit

Permalink
feat: add prometheus exporter and promremotewrite exporter (#261)
Browse files Browse the repository at this point in the history
Signed-off-by: Smuu <[email protected]>
  • Loading branch information
smuu authored Apr 10, 2024
1 parent 4b9957f commit 3ab9345
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 29 deletions.
72 changes: 51 additions & 21 deletions pkg/knuu/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ type ObsyConfig struct {
// otelCollectorVersion is the version of the otel collector to use
otelCollectorVersion string

// prometheusPort is the port on which the prometheus server will be exposed
prometheusPort int
// prometheusJobName is the name of the prometheus job
prometheusJobName string
// prometheusScrapeInterval is the scrape interval for the prometheus job
prometheusScrapeInterval string
// prometheusEndpointPort is the port on which the prometheus server will be exposed
prometheusEndpointPort int
// prometheusEndpointJobName is the name of the prometheus job
prometheusEndpointJobName string
// prometheusEndpointScrapeInterval is the scrape interval for the prometheus job
prometheusEndpointScrapeInterval string

// jaegerGrpcPort is the port on which the jaeger grpc server is exposed
jaegerGrpcPort int
Expand All @@ -52,6 +52,12 @@ type ObsyConfig struct {
otlpUsername string
// otlpPassword is the password to use for the otlp collector
otlpPassword string

// prometheusExporterEndpoint is the endpoint of the prometheus exporter
prometheusExporterEndpoint string

// prometheusRemoteWriteExporterEndpoint is the endpoint of the prometheus remote write
prometheusRemoteWriteExporterEndpoint string
}

// SecurityContext represents the security settings for a container
Expand Down Expand Up @@ -105,18 +111,20 @@ func NewInstance(name string) (*Instance, error) {
return nil, fmt.Errorf("error generating k8s name for instance '%s': %w", name, err)
}
obsyConfig := &ObsyConfig{
otelCollectorVersion: "0.83.0",
otlpPort: 0,
prometheusPort: 0,
prometheusJobName: "",
prometheusScrapeInterval: "",
jaegerGrpcPort: 0,
jaegerThriftCompactPort: 0,
jaegerThriftHttpPort: 0,
otlpEndpoint: "",
otlpUsername: "",
otlpPassword: "",
jaegerEndpoint: "",
otelCollectorVersion: "0.83.0",
otlpPort: 0,
prometheusEndpointPort: 0,
prometheusEndpointJobName: "",
prometheusEndpointScrapeInterval: "",
jaegerGrpcPort: 0,
jaegerThriftCompactPort: 0,
jaegerThriftHttpPort: 0,
otlpEndpoint: "",
otlpUsername: "",
otlpPassword: "",
jaegerEndpoint: "",
prometheusExporterEndpoint: "",
prometheusRemoteWriteExporterEndpoint: "",
}
securityContext := &SecurityContext{
privileged: false,
Expand Down Expand Up @@ -858,9 +866,9 @@ func (i *Instance) SetPrometheusEndpoint(port int, jobName, scapeInterval string
if err := i.validateStateForObsy("Prometheus endpoint"); err != nil {
return err
}
i.obsyConfig.prometheusPort = port
i.obsyConfig.prometheusJobName = jobName
i.obsyConfig.prometheusScrapeInterval = scapeInterval
i.obsyConfig.prometheusEndpointPort = port
i.obsyConfig.prometheusEndpointJobName = jobName
i.obsyConfig.prometheusEndpointScrapeInterval = scapeInterval
logrus.Debugf("Set Prometheus endpoint '%d' for instance '%s'", port, i.name)
return nil
}
Expand Down Expand Up @@ -902,6 +910,28 @@ func (i *Instance) SetJaegerExporter(endpoint string) error {
return nil
}

// SetPrometheusExporter sets the Prometheus exporter for the instance
// This function can only be called in the state 'Preparing' or 'Committed'
func (i *Instance) SetPrometheusExporter(endpoint string) error {
if err := i.validateStateForObsy("Prometheus exporter"); err != nil {
return err
}
i.obsyConfig.prometheusExporterEndpoint = endpoint
logrus.Debugf("Set Prometheus exporter '%s' for instance '%s'", endpoint, i.name)
return nil
}

// SetPrometheusRemoteWriteExporter sets the Prometheus remote write exporter for the instance
// This function can only be called in the state 'Preparing' or 'Committed'
func (i *Instance) SetPrometheusRemoteWriteExporter(endpoint string) error {
if err := i.validateStateForObsy("Prometheus remote write exporter"); err != nil {
return err
}
i.obsyConfig.prometheusRemoteWriteExporterEndpoint = endpoint
logrus.Debugf("Set Prometheus remote write exporter '%s' for instance '%s'", endpoint, i.name)
return nil
}

// SetPrivileged sets the privileged status for the instance
// This function can only be called in the state 'Preparing' or 'Committed'
func (i *Instance) SetPrivileged(privileged bool) error {
Expand Down
2 changes: 1 addition & 1 deletion pkg/knuu/instance_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ func setStateForSidecars(sidecars []*Instance, state InstanceState) {

// isObservabilityEnabled returns true if observability is enabled
func (i *Instance) isObservabilityEnabled() bool {
return i.obsyConfig.otlpPort != 0 || i.obsyConfig.prometheusPort != 0 || i.obsyConfig.jaegerGrpcPort != 0 || i.obsyConfig.jaegerThriftCompactPort != 0 || i.obsyConfig.jaegerThriftHttpPort != 0
return i.obsyConfig.otlpPort != 0 || i.obsyConfig.prometheusEndpointPort != 0 || i.obsyConfig.jaegerGrpcPort != 0 || i.obsyConfig.jaegerThriftCompactPort != 0 || i.obsyConfig.jaegerThriftHttpPort != 0
}

func (i *Instance) validateStateForObsy(endpoint string) error {
Expand Down
55 changes: 48 additions & 7 deletions pkg/knuu/instance_otel.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package knuu

import (
"fmt"

"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -85,8 +86,10 @@ type JaegerThriftHTTP struct {
}

type Exporters struct {
OTLPHTTP OTLPHTTPExporter `yaml:"otlphttp,omitempty"`
Jaeger JaegerExporter `yaml:"jaeger,omitempty"`
OTLPHTTP OTLPHTTPExporter `yaml:"otlphttp,omitempty"`
Jaeger JaegerExporter `yaml:"jaeger,omitempty"`
Prometheus PrometheusExporter `yaml:"prometheus,omitempty"`
PrometheusRemoteWrite PrometheusRemoteWriteExporter `yaml:"prometheusremotewrite,omitempty"`
}

type OTLPHTTPExporter struct {
Expand All @@ -103,6 +106,15 @@ type JaegerExporter struct {
TLS TLS `yaml:"tls,omitempty"`
}

type PrometheusExporter struct {
Endpoint string `yaml:"endpoint,omitempty"`
}

type PrometheusRemoteWriteExporter struct {
Endpoint string `yaml:"endpoint,omitempty"`
TLS TLS `yaml:"tls,omitempty"`
}

type TLS struct {
Insecure bool `yaml:"insecure,omitempty"`
}
Expand Down Expand Up @@ -220,11 +232,11 @@ func (i *Instance) createPrometheusReceiver() Prometheus {
Config: PrometheusConfig{
ScrapeConfigs: []ScrapeConfig{
{
JobName: i.obsyConfig.prometheusJobName,
ScrapeInterval: i.obsyConfig.prometheusScrapeInterval,
JobName: i.obsyConfig.prometheusEndpointJobName,
ScrapeInterval: i.obsyConfig.prometheusEndpointScrapeInterval,
StaticConfigs: []StaticConfig{
{
Targets: []string{fmt.Sprintf("localhost:%d", i.obsyConfig.prometheusPort)},
Targets: []string{fmt.Sprintf("localhost:%d", i.obsyConfig.prometheusEndpointPort)},
},
},
},
Expand Down Expand Up @@ -254,7 +266,7 @@ func (i *Instance) createReceivers() Receivers {
receivers.OTLP = i.createOtlpReceiver()
}

if i.obsyConfig.prometheusPort != 0 {
if i.obsyConfig.prometheusEndpointPort != 0 {
receivers.Prometheus = i.createPrometheusReceiver()
}

Expand Down Expand Up @@ -283,6 +295,21 @@ func (i *Instance) createJaegerExporter() JaegerExporter {
}
}

func (i *Instance) createPrometheusExporter() PrometheusExporter {
return PrometheusExporter{
Endpoint: i.obsyConfig.prometheusExporterEndpoint,
}
}

func (i *Instance) createPrometheusRemoteWriteExporter() PrometheusRemoteWriteExporter {
return PrometheusRemoteWriteExporter{
Endpoint: i.obsyConfig.prometheusRemoteWriteExporterEndpoint,
TLS: TLS{
Insecure: true,
},
}
}

func (i *Instance) createExporters() Exporters {
exporters := Exporters{}

Expand All @@ -294,6 +321,14 @@ func (i *Instance) createExporters() Exporters {
exporters.Jaeger = i.createJaegerExporter()
}

if i.obsyConfig.prometheusEndpointPort != 0 {
exporters.Prometheus = i.createPrometheusExporter()
}

if i.obsyConfig.prometheusRemoteWriteExporterEndpoint != "" {
exporters.PrometheusRemoteWrite = i.createPrometheusRemoteWriteExporter()
}

return exporters
}

Expand All @@ -302,12 +337,18 @@ func (i *Instance) prepareMetricsForServicePipeline() Metrics {
if i.obsyConfig.otlpPort != 0 {
metrics.Receivers = append(metrics.Receivers, "otlp")
}
if i.obsyConfig.prometheusPort != 0 {
if i.obsyConfig.prometheusEndpointPort != 0 {
metrics.Receivers = append(metrics.Receivers, "prometheus")
}
if i.obsyConfig.otlpEndpoint != "" {
metrics.Exporters = append(metrics.Exporters, "otlphttp")
}
if i.obsyConfig.prometheusExporterEndpoint != "" {
metrics.Exporters = append(metrics.Exporters, "prometheus")
}
if i.obsyConfig.prometheusRemoteWriteExporterEndpoint != "" {
metrics.Exporters = append(metrics.Exporters, "prometheusremotewrite")
}
return metrics
}

Expand Down

0 comments on commit 3ab9345

Please sign in to comment.