Skip to content

Commit

Permalink
fix(kuma-dp): allow to configure address of application to scrape (#5326
Browse files Browse the repository at this point in the history
)

Signed-off-by: Łukasz Dziedziak <lukidzi@gmail.com>
  • Loading branch information
lukidzi authored Nov 17, 2022
1 parent af163dc commit 83be219
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 21 deletions.
35 changes: 23 additions & 12 deletions api/mesh/v1alpha1/metrics.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions api/mesh/v1alpha1/metrics.proto
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ message PrometheusAggregateMetricsConfig {
// If false then the application won't be scrapped. If nil, then it is treated
// as true and kuma-dp scrapes metrics from the service.
google.protobuf.BoolValue enabled = 4;

// Address on which a service expose HTTP endpoint with Prometheus metrics.
string address = 5;
}

// PrometheusEnvoyConfig defines filters that should be passed to Envoy
Expand Down
27 changes: 18 additions & 9 deletions pkg/xds/bootstrap/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,22 +206,14 @@ func (b *bootstrapGenerator) getMetricsConfig(
if err != nil {
return err
}

var address string
if b.enableLocalhostInboundCluster {
address = core_mesh.IPv4Loopback.String()
} else {
address = dataplane.Spec.GetNetworking().GetAddress()
}

if config != nil {
aggregateConfig := []AggregateMetricsConfig{}
for _, config := range config.GetAggregate() {
if config.GetEnabled() != nil && !config.GetEnabled().GetValue() {
continue
}
aggregateConfig = append(aggregateConfig, AggregateMetricsConfig{
Address: address,
Address: b.getMetricsAddress(config, dataplane),
Name: config.Name,
Port: config.Port,
Path: config.Path,
Expand All @@ -232,6 +224,23 @@ func (b *bootstrapGenerator) getMetricsConfig(
return nil
}

func (b *bootstrapGenerator) getMetricsAddress(
metricsConfig *mesh_proto.PrometheusAggregateMetricsConfig,
dataplane *core_mesh.DataplaneResource,
) string {
if metricsConfig.Address != "" {
return metricsConfig.Address
} else {
var address string
if b.enableLocalhostInboundCluster {
address = core_mesh.IPv4Loopback.String()
} else {
address = dataplane.Spec.GetNetworking().GetAddress()
}
return address
}
}

func (b *bootstrapGenerator) validateRequest(request types.BootstrapRequest) error {
if b.dpAuthEnabled && request.DataplaneToken == "" && request.DataplaneTokenPath == "" {
return DpTokenRequired
Expand Down
45 changes: 45 additions & 0 deletions test/e2e_env/universal/observability/applications_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ metrics:
func ApplicationsMetrics() {
const mesh = "applications-metrics"
const meshNoAggregate = "applications-metrics-no-aggregate"
const meshWithLocalhostBound = "applications-metrics-localhost-bound"

dpLocalhostBoundAggregateConfig := `
metrics:
type: prometheus
conf:
aggregate:
- name: localhost-bound-not-exposed
port: 8080
path: "/localhost-bound-not-exposed"
- name: localhost-bound
port: 8080
address: "127.0.0.1"
path: "/localhost-bound"`

dpAggregateConfig := `
metrics:
Expand Down Expand Up @@ -92,6 +106,7 @@ metrics:
err := NewClusterSetup().
Install(MeshWithMetricsEnabeld(meshNoAggregate)).
Install(MeshKubernetesAndMetricsAggregate(mesh)).
Install(MeshWithMetricsEnabeld(meshWithLocalhostBound)).
Install(TestServerUniversal("test-server", mesh,
WithTransparentProxy(true),
WithArgs([]string{"echo", "--instance", "test-server"}),
Expand All @@ -110,6 +125,12 @@ metrics:
WithServiceName("test-server-dp-metrics"),
WithAppendDataplaneYaml(dpAggregateConfig),
)).
Install(TestServerUniversal("test-server-dp-metrics-localhost", meshWithLocalhostBound,
WithTransparentProxy(true),
WithArgs([]string{"echo", "--instance", "test-server-dp-metrics-localhost", "--ip", "127.0.0.1"}),
WithServiceName("test-server-dp-metrics-localhost"),
WithAppendDataplaneYaml(dpLocalhostBoundAggregateConfig),
)).
Setup(env.Cluster)
Expect(err).ToNot(HaveOccurred())
})
Expand All @@ -118,6 +139,8 @@ metrics:
Expect(env.Cluster.DeleteMesh(mesh)).To(Succeed())
Expect(env.Cluster.DeleteMeshApps(meshNoAggregate)).To(Succeed())
Expect(env.Cluster.DeleteMesh(meshNoAggregate)).To(Succeed())
Expect(env.Cluster.DeleteMeshApps(meshWithLocalhostBound)).To(Succeed())
Expect(env.Cluster.DeleteMesh(meshWithLocalhostBound)).To(Succeed())
})

It("should scrape metrics defined in mesh and not fail when defined service doesn't exist", func() {
Expand Down Expand Up @@ -193,4 +216,26 @@ metrics:
// metric from envoy
Expect(stdout).To(ContainSubstring("envoy_server_concurrency"))
})

It("should allow to define and expose localhost bound server", func() {
// given
ip := env.Cluster.GetApp("test-server-dp-metrics-localhost").GetIP()

// when
stdout, _, err := env.Cluster.ExecWithRetries("", "", "test-server-dp-metrics-localhost",
"curl", "-v", "-m", "3", "--fail", "http://"+net.JoinHostPort(ip, "1234")+"/metrics?filter=concurrency")

// then
Expect(err).ToNot(HaveOccurred())
Expect(stdout).ToNot(BeNil())
Expect(stdout).To(ContainSubstring(string(expfmt.FmtText)))

// path doesn't have defined address
Expect(stdout).ToNot(ContainSubstring("localhost-bound-not-exposed"))

// exposed localhost bound service
Expect(stdout).To(ContainSubstring("localhost-bound"))
// metric from envoy
Expect(stdout).To(ContainSubstring("envoy_server_concurrency"))
})
}

0 comments on commit 83be219

Please sign in to comment.