|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package main |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "os" |
| 21 | + "testing" |
| 22 | + |
| 23 | + gokitlog "github.com/go-kit/log" |
| 24 | + "github.com/go-logr/logr" |
| 25 | + "github.com/prometheus/client_golang/prometheus" |
| 26 | + "github.com/prometheus/common/model" |
| 27 | + "github.com/prometheus/prometheus/discovery" |
| 28 | + "github.com/prometheus/prometheus/discovery/targetgroup" |
| 29 | + "github.com/prometheus/prometheus/model/relabel" |
| 30 | + ctrl "sigs.k8s.io/controller-runtime" |
| 31 | + "sigs.k8s.io/controller-runtime/pkg/log" |
| 32 | + |
| 33 | + "github.com/open-telemetry/opentelemetry-operator/cmd/otel-allocator/allocation" |
| 34 | + "github.com/open-telemetry/opentelemetry-operator/cmd/otel-allocator/prehook" |
| 35 | + "github.com/open-telemetry/opentelemetry-operator/cmd/otel-allocator/server" |
| 36 | + "github.com/open-telemetry/opentelemetry-operator/cmd/otel-allocator/target" |
| 37 | +) |
| 38 | + |
| 39 | +// BenchmarkProcessTargets benchmarks the whole target allocation pipeline. It starts with data the prometheus |
| 40 | +// discovery manager would normally output, and pushes it all the way into the allocator. It notably doe *not* check |
| 41 | +// the HTTP server afterward. Test data is chosen to be reasonably representative of what the Prometheus service discovery |
| 42 | +// outputs in the real world. |
| 43 | +func BenchmarkProcessTargets(b *testing.B) { |
| 44 | + numTargets := 10000 |
| 45 | + targetsPerGroup := 5 |
| 46 | + groupsPerJob := 20 |
| 47 | + tsets := prepareBenchmarkData(numTargets, targetsPerGroup, groupsPerJob) |
| 48 | + |
| 49 | + b.ResetTimer() |
| 50 | + for _, strategy := range allocation.GetRegisteredAllocatorNames() { |
| 51 | + b.Run(strategy, func(b *testing.B) { |
| 52 | + targetDiscoverer, allocator := createTestDiscoverer(strategy, map[string][]*relabel.Config{}) |
| 53 | + for i := 0; i < b.N; i++ { |
| 54 | + targetDiscoverer.ProcessTargets(tsets, allocator.SetTargets) |
| 55 | + } |
| 56 | + }) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// BenchmarkProcessTargetsWithRelabelConfig is BenchmarkProcessTargets with a relabel config set. The relabel config |
| 61 | +// does not actually modify any records, but does force the prehook to perform any necessary conversions along the way. |
| 62 | +func BenchmarkProcessTargetsWithRelabelConfig(b *testing.B) { |
| 63 | + numTargets := 10000 |
| 64 | + targetsPerGroup := 5 |
| 65 | + groupsPerJob := 20 |
| 66 | + tsets := prepareBenchmarkData(numTargets, targetsPerGroup, groupsPerJob) |
| 67 | + prehookConfig := make(map[string][]*relabel.Config, len(tsets)) |
| 68 | + for jobName := range tsets { |
| 69 | + prehookConfig[jobName] = []*relabel.Config{ |
| 70 | + { |
| 71 | + Action: "keep", |
| 72 | + Regex: relabel.MustNewRegexp(".*"), |
| 73 | + }, |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + b.ResetTimer() |
| 78 | + for _, strategy := range allocation.GetRegisteredAllocatorNames() { |
| 79 | + b.Run(strategy, func(b *testing.B) { |
| 80 | + targetDiscoverer, allocator := createTestDiscoverer(strategy, prehookConfig) |
| 81 | + for i := 0; i < b.N; i++ { |
| 82 | + targetDiscoverer.ProcessTargets(tsets, allocator.SetTargets) |
| 83 | + } |
| 84 | + }) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func prepareBenchmarkData(numTargets, targetsPerGroup, groupsPerJob int) map[string][]*targetgroup.Group { |
| 89 | + numGroups := numTargets / targetsPerGroup |
| 90 | + numJobs := numGroups / groupsPerJob |
| 91 | + jobNamePrefix := "test-" |
| 92 | + groupLabels := model.LabelSet{ |
| 93 | + "__meta_kubernetes_pod_controller_name": "example", |
| 94 | + "__meta_kubernetes_pod_ip": "10.244.0.251", |
| 95 | + "__meta_kubernetes_pod_uid": "676ebee7-14f8-481e-a937-d2affaec4105", |
| 96 | + "__meta_kubernetes_endpointslice_port_protocol": "TCP", |
| 97 | + "__meta_kubernetes_endpointslice_endpoint_conditions_ready": "true", |
| 98 | + "__meta_kubernetes_service_annotation_kubectl_kubernetes_io_last_applied_configuration": "{\"apiVersion\":\"v1\",\"kind\":\"Service\",\"metadata\":{\"annotations\":{},\"labels\":{\"app\":\"example\"},\"name\":\"example-svc\",\"namespace\":\"example\"},\"spec\":{\"clusterIP\":\"None\",\"ports\":[{\"name\":\"http-example\",\"port\":9006,\"targetPort\":9006}],\"selector\":{\"app\":\"example\"},\"type\":\"ClusterIP\"}}\n", |
| 99 | + "__meta_kubernetes_endpointslice_labelpresent_app": "true", |
| 100 | + "__meta_kubernetes_endpointslice_name": "example-svc-qgwxf", |
| 101 | + "__address__": "10.244.0.251:9006", |
| 102 | + "__meta_kubernetes_endpointslice_endpoint_conditions_terminating": "false", |
| 103 | + "__meta_kubernetes_pod_labelpresent_pod_template_hash": "true", |
| 104 | + "__meta_kubernetes_endpointslice_label_kubernetes_io_service_name": "example-svc", |
| 105 | + "__meta_kubernetes_endpointslice_labelpresent_service_kubernetes_io_headless": "true", |
| 106 | + "__meta_kubernetes_pod_label_pod_template_hash": "6b549885f8", |
| 107 | + "__meta_kubernetes_endpointslice_address_target_name": "example-6b549885f8-7tbcw", |
| 108 | + "__meta_kubernetes_pod_labelpresent_app": "true", |
| 109 | + "somelabel": "somevalue", |
| 110 | + } |
| 111 | + exampleTarget := model.LabelSet{ |
| 112 | + "__meta_kubernetes_endpointslice_port": "9006", |
| 113 | + "__meta_kubernetes_service_label_app": "example", |
| 114 | + "__meta_kubernetes_endpointslice_port_name": "http-example", |
| 115 | + "__meta_kubernetes_pod_ready": "true", |
| 116 | + "__meta_kubernetes_endpointslice_address_type": "IPv4", |
| 117 | + "__meta_kubernetes_endpointslice_label_endpointslice_kubernetes_io_managed_by": "endpointslice-controller.k8s.io", |
| 118 | + "__meta_kubernetes_endpointslice_labelpresent_endpointslice_kubernetes_io_managed_by": "true", |
| 119 | + "__meta_kubernetes_endpointslice_label_app": "example", |
| 120 | + "__meta_kubernetes_endpointslice_endpoint_conditions_serving": "true", |
| 121 | + "__meta_kubernetes_pod_phase": "Running", |
| 122 | + "__meta_kubernetes_pod_controller_kind": "ReplicaSet", |
| 123 | + "__meta_kubernetes_service_annotationpresent_kubectl_kubernetes_io_last_applied_configuration": "true", |
| 124 | + "__meta_kubernetes_service_labelpresent_app": "true", |
| 125 | + "__meta_kubernetes_endpointslice_labelpresent_kubernetes_io_service_name": "true", |
| 126 | + "__meta_kubernetes_endpointslice_annotation_endpoints_kubernetes_io_last_change_trigger_time": "2023-09-27T16:01:29Z", |
| 127 | + "__meta_kubernetes_pod_name": "example-6b549885f8-7tbcw", |
| 128 | + "__meta_kubernetes_service_name": "example-svc", |
| 129 | + "__meta_kubernetes_namespace": "example", |
| 130 | + "__meta_kubernetes_endpointslice_annotationpresent_endpoints_kubernetes_io_last_change_trigger_time": "true", |
| 131 | + "__meta_kubernetes_pod_node_name": "kind-control-plane", |
| 132 | + "__meta_kubernetes_endpointslice_address_target_kind": "Pod", |
| 133 | + "__meta_kubernetes_pod_host_ip": "172.18.0.2", |
| 134 | + "__meta_kubernetes_endpointslice_label_service_kubernetes_io_headless": "", |
| 135 | + "__meta_kubernetes_pod_label_app": "example", |
| 136 | + } |
| 137 | + targets := []model.LabelSet{} |
| 138 | + for i := 0; i < numTargets; i++ { |
| 139 | + targets = append(targets, exampleTarget.Clone()) |
| 140 | + } |
| 141 | + groups := make([]*targetgroup.Group, numGroups) |
| 142 | + for i := 0; i < numGroups; i++ { |
| 143 | + groupTargets := targets[(i * targetsPerGroup):(i*targetsPerGroup + targetsPerGroup)] |
| 144 | + groups[i] = &targetgroup.Group{ |
| 145 | + Labels: groupLabels, |
| 146 | + Targets: groupTargets, |
| 147 | + } |
| 148 | + } |
| 149 | + tsets := make(map[string][]*targetgroup.Group, numJobs) |
| 150 | + for i := 0; i < numJobs; i++ { |
| 151 | + jobGroups := groups[(i * groupsPerJob):(i*groupsPerJob + groupsPerJob)] |
| 152 | + jobName := fmt.Sprintf("%s%d", jobNamePrefix, i) |
| 153 | + tsets[jobName] = jobGroups |
| 154 | + } |
| 155 | + return tsets |
| 156 | +} |
| 157 | + |
| 158 | +func createTestDiscoverer(allocationStrategy string, prehookConfig map[string][]*relabel.Config) (*target.Discoverer, allocation.Allocator) { |
| 159 | + ctx := context.Background() |
| 160 | + logger := ctrl.Log.WithName(fmt.Sprintf("bench-%s", allocationStrategy)) |
| 161 | + ctrl.SetLogger(logr.New(log.NullLogSink{})) |
| 162 | + allocatorPrehook := prehook.New("relabel-config", logger) |
| 163 | + allocatorPrehook.SetConfig(prehookConfig) |
| 164 | + allocator, err := allocation.New(allocationStrategy, logger, allocation.WithFilter(allocatorPrehook)) |
| 165 | + srv := server.NewServer(logger, allocator, "localhost:0") |
| 166 | + if err != nil { |
| 167 | + setupLog.Error(err, "Unable to initialize allocation strategy") |
| 168 | + os.Exit(1) |
| 169 | + } |
| 170 | + registry := prometheus.NewRegistry() |
| 171 | + sdMetrics, _ := discovery.CreateAndRegisterSDMetrics(registry) |
| 172 | + discoveryManager := discovery.NewManager(ctx, gokitlog.NewNopLogger(), registry, sdMetrics) |
| 173 | + targetDiscoverer := target.NewDiscoverer(logger, discoveryManager, allocatorPrehook, srv) |
| 174 | + return targetDiscoverer, allocator |
| 175 | +} |
0 commit comments