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

feat(*): hijack application metrics #4286

Merged
merged 16 commits into from
May 20, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 143 additions & 25 deletions api/mesh/v1alpha1/metrics.pb.go

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

18 changes: 18 additions & 0 deletions api/mesh/v1alpha1/metrics.proto
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,22 @@ message PrometheusMetricsBackendConfig {
// If true then endpoints for scraping metrics won't require mTLS even if mTLS
// is enabled in Mesh. If nil, then it is treated as false.
google.protobuf.BoolValue skipMTLS = 4;

// Map with the configuration of applications which metrics are going to be
// scrapped by kuma-dp.
map<string, PrometheusAggregateMetricsConfig> aggregate = 5;
}

// PrometheusAggregateMetricsConfig defines endpoints that should be scrapped
// by kuma-dp for prometheus metrics.
lukidzi marked this conversation as resolved.
Show resolved Hide resolved
message PrometheusAggregateMetricsConfig {
// Port on which a service expose HTTP endpoint with Prometheus metrics.
uint32 port = 1;

// Path on which a service expose HTTP endpoint with Prometheus metrics.
string path = 2;

// 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 = 3;
}
33 changes: 29 additions & 4 deletions app/kuma-dp/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ import (
"github.com/kumahq/kuma/pkg/core/runtime/component"
core_xds "github.com/kumahq/kuma/pkg/core/xds"
util_net "github.com/kumahq/kuma/pkg/util/net"
"github.com/kumahq/kuma/pkg/util/proto"
kuma_version "github.com/kumahq/kuma/pkg/version"
"github.com/kumahq/kuma/pkg/xds/bootstrap/types"
)

var runLog = dataplaneLog.WithName("run")
Expand Down Expand Up @@ -209,7 +211,7 @@ func newRunCmd(opts kuma_cmd.RunCmdOpts, rootCtx *RootContext) *cobra.Command {
runLog.Info("fetched Envoy version", "version", envoyVersion)

runLog.Info("generating bootstrap configuration")
bootstrap, bootstrapBytes, err := rootCtx.BootstrapGenerator(gracefulCtx, opts.Config.ControlPlane.URL, opts.Config, envoy.BootstrapParams{
bootstrap, kumaSidecarConfiguration, err := rootCtx.BootstrapGenerator(gracefulCtx, opts.Config.ControlPlane.URL, opts.Config, envoy.BootstrapParams{
Dataplane: opts.Dataplane,
DNSPort: cfg.DNS.EnvoyDNSPort,
EmptyDNSPort: cfg.DNS.CoreDNSEmptyPort,
Expand All @@ -221,7 +223,10 @@ func newRunCmd(opts kuma_cmd.RunCmdOpts, rootCtx *RootContext) *cobra.Command {
}
runLog.Info("received bootstrap configuration", "adminPort", bootstrap.GetAdmin().GetAddress().GetSocketAddress().GetPortValue())

opts.BootstrapConfig = bootstrapBytes
opts.BootstrapConfig, err = proto.ToYAML(bootstrap)
if err != nil {
return errors.Errorf("could not convert to yaml. %v", err)
}
opts.AdminPort = bootstrap.GetAdmin().GetAddress().GetSocketAddress().GetPortValue()

dataplane, err := envoy.New(opts)
Expand All @@ -230,8 +235,7 @@ func newRunCmd(opts kuma_cmd.RunCmdOpts, rootCtx *RootContext) *cobra.Command {
}

components = append(components, dataplane)

metricsServer := metrics.New(cfg.Dataplane, bootstrap.GetAdmin().GetAddress().GetSocketAddress().GetPortValue())
metricsServer := metrics.New(cfg.Dataplane, getApplicationsToScrape(kumaSidecarConfiguration, bootstrap.GetAdmin().GetAddress().GetSocketAddress().GetPortValue()))
components = append(components, metricsServer)

if err := rootCtx.ComponentManager.Add(components...); err != nil {
Expand Down Expand Up @@ -296,6 +300,27 @@ func newRunCmd(opts kuma_cmd.RunCmdOpts, rootCtx *RootContext) *cobra.Command {
return cmd
}

func getApplicationsToScrape(kumaSidecarConfiguration *types.KumaSidecarConfiguration, envoyAdminPort uint32) []*metrics.ApplicationToScrape {
applicationsToScrape := []*metrics.ApplicationToScrape{}
if kumaSidecarConfiguration != nil && len(kumaSidecarConfiguration.Metrics.Aggregate) > 0 {
lukidzi marked this conversation as resolved.
Show resolved Hide resolved
for _, item := range kumaSidecarConfiguration.Metrics.Aggregate {
applicationsToScrape = append(applicationsToScrape, &metrics.ApplicationToScrape{
Name: item.Name,
Path: item.Path,
Port: item.Port,
})
}
}
// by default add envoy configuration
applicationsToScrape = append(applicationsToScrape, &metrics.ApplicationToScrape{
Name: "envoy",
Path: "/stats/prometheus",
Port: envoyAdminPort,
Mutator: metrics.MergeClusters,
})
return applicationsToScrape
}

func writeFile(filename string, data []byte, perm os.FileMode) error {
if err := os.MkdirAll(filepath.Dir(filename), perm); err != nil {
return err
Expand Down
5 changes: 3 additions & 2 deletions app/kuma-dp/cmd/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
kumadp "github.com/kumahq/kuma/pkg/config/app/kuma-dp"
"github.com/kumahq/kuma/pkg/test"
util_proto "github.com/kumahq/kuma/pkg/util/proto"
"github.com/kumahq/kuma/pkg/xds/bootstrap/types"
)

var _ = Describe("run", func() {
Expand Down Expand Up @@ -95,14 +96,14 @@ var _ = Describe("run", func() {

// given
rootCtx := DefaultRootContext()
rootCtx.BootstrapGenerator = func(_ context.Context, _ string, cfg kumadp.Config, _ envoy.BootstrapParams) (*envoy_bootstrap_v3.Bootstrap, []byte, error) {
rootCtx.BootstrapGenerator = func(_ context.Context, _ string, cfg kumadp.Config, _ envoy.BootstrapParams) (*envoy_bootstrap_v3.Bootstrap, *types.KumaSidecarConfiguration, error) {
respBytes, err := os.ReadFile(filepath.Join("testdata", "bootstrap-config.golden.yaml"))
Expect(err).ToNot(HaveOccurred())
bootstrap := &envoy_bootstrap_v3.Bootstrap{}
if err := util_proto.FromYAML(respBytes, bootstrap); err != nil {
return nil, nil, err
}
return bootstrap, respBytes, nil
return bootstrap, &types.KumaSidecarConfiguration{}, nil
}

reader, writer := io.Pipe()
Expand Down
5 changes: 5 additions & 0 deletions app/kuma-dp/cmd/testdata/bootstrap-config.golden.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ dynamicResources:
node:
cluster: backend
id: dp-1.default.default
metadata:
dataplane.applications.metrics:
- name: app
port: 123
path: "/stats"
lukidzi marked this conversation as resolved.
Show resolved Hide resolved
staticResources:
clusters:
- connectTimeout: 0.250s
Expand Down
3 changes: 2 additions & 1 deletion app/kuma-dp/pkg/dataplane/envoy/envoy.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/kumahq/kuma/pkg/core/runtime/component"
pkg_log "github.com/kumahq/kuma/pkg/log"
"github.com/kumahq/kuma/pkg/util/files"
"github.com/kumahq/kuma/pkg/xds/bootstrap/types"
)

var (
Expand All @@ -36,7 +37,7 @@ type BootstrapParams struct {
DynamicMetadata map[string]string
}

type BootstrapConfigFactoryFunc func(ctx context.Context, url string, cfg kuma_dp.Config, params BootstrapParams) (*envoy_bootstrap_v3.Bootstrap, []byte, error)
type BootstrapConfigFactoryFunc func(ctx context.Context, url string, cfg kuma_dp.Config, params BootstrapParams) (*envoy_bootstrap_v3.Bootstrap, *types.KumaSidecarConfiguration, error)

type Opts struct {
Config kuma_dp.Config
Expand Down
Loading