|
| 1 | +package prometheus_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/go-kit/log" |
| 11 | + promclient "github.com/prometheus/client_golang/prometheus" |
| 12 | + "github.com/prometheus/prometheus/model/labels" |
| 13 | + "github.com/prometheus/prometheus/prompb" |
| 14 | + "github.com/prometheus/prometheus/storage" |
| 15 | + "github.com/prometheus/prometheus/storage/remote" |
| 16 | + "github.com/stretchr/testify/require" |
| 17 | + |
| 18 | + "github.com/grafana/alloy/internal/component" |
| 19 | + "github.com/grafana/alloy/internal/component/prometheus" |
| 20 | + "github.com/grafana/alloy/internal/component/prometheus/relabel" |
| 21 | + "github.com/grafana/alloy/internal/component/prometheus/remotewrite" |
| 22 | + "github.com/grafana/alloy/internal/runtime/componenttest" |
| 23 | + "github.com/grafana/alloy/internal/service/labelstore" |
| 24 | + "github.com/grafana/alloy/internal/util" |
| 25 | + "github.com/grafana/alloy/syntax" |
| 26 | +) |
| 27 | + |
| 28 | +// This test simulates a scrape -> remote_write pipeline, without actually scraping |
| 29 | +func TestPipeline(t *testing.T) { |
| 30 | + handler, writeResult := channelServerHandler(t) |
| 31 | + pipeline, srv, ls := newDefaultPipeline(t, util.TestLogger(t), handler) |
| 32 | + defer srv.Close() |
| 33 | + |
| 34 | + // We need to use a future timestamp since remote_write will ignore any |
| 35 | + // sample which is earlier than the time when it started. Adding a minute |
| 36 | + // ensures that our samples will never get ignored. |
| 37 | + sampleTimestamp := time.Now().Add(time.Minute).UnixMilli() |
| 38 | + |
| 39 | + // Send metrics to our component. These will be written to the WAL and |
| 40 | + // subsequently written to our HTTP server. |
| 41 | + lset1 := labels.FromStrings("foo", "bar") |
| 42 | + sendMetric(t, pipeline.Appender(t.Context()), lset1, sampleTimestamp, 12) |
| 43 | + lset2 := labels.FromStrings("fizz", "buzz") |
| 44 | + sendMetric(t, pipeline.Appender(t.Context()), lset2, sampleTimestamp, 34) |
| 45 | + |
| 46 | + expect := []prompb.TimeSeries{{ |
| 47 | + Labels: []prompb.Label{ |
| 48 | + {Name: "cluster", Value: "local"}, |
| 49 | + {Name: "foo", Value: "bar"}, |
| 50 | + }, |
| 51 | + Samples: []prompb.Sample{ |
| 52 | + {Timestamp: sampleTimestamp, Value: 12}, |
| 53 | + }, |
| 54 | + }, { |
| 55 | + Labels: []prompb.Label{ |
| 56 | + {Name: "cluster", Value: "local"}, |
| 57 | + {Name: "fizz", Value: "buzz"}, |
| 58 | + }, |
| 59 | + Samples: []prompb.Sample{ |
| 60 | + {Timestamp: sampleTimestamp, Value: 34}, |
| 61 | + }, |
| 62 | + }} |
| 63 | + |
| 64 | + select { |
| 65 | + case <-time.After(time.Minute): |
| 66 | + require.FailNow(t, "timed out waiting for metrics") |
| 67 | + case res := <-writeResult: |
| 68 | + require.Equal(t, expect, res.Timeseries) |
| 69 | + } |
| 70 | + |
| 71 | + ref := ls.GetOrAddGlobalRefID(lset1) |
| 72 | + require.NotZero(t, ref) |
| 73 | + localRef := ls.GetLocalRefID("prometheus.remote_write.test", ref) |
| 74 | + require.NotZero(t, localRef) |
| 75 | + |
| 76 | + ref = ls.GetOrAddGlobalRefID(lset2) |
| 77 | + require.NotZero(t, ref) |
| 78 | + localRef = ls.GetLocalRefID("prometheus.remote_write.test", ref) |
| 79 | + require.NotZero(t, localRef) |
| 80 | +} |
| 81 | + |
| 82 | +// This test simulates a scrape -> relabel -> remote_write pipeline, without actually scraping |
| 83 | +func TestRelabelPipeline(t *testing.T) { |
| 84 | + handler, writeResult := channelServerHandler(t) |
| 85 | + pipeline, srv, ls := newRelabelPipeline(t, util.TestLogger(t), handler) |
| 86 | + defer srv.Close() |
| 87 | + |
| 88 | + // We need to use a future timestamp since remote_write will ignore any |
| 89 | + // sample which is earlier than the time when it started. Adding a minute |
| 90 | + // ensures that our samples will never get ignored. |
| 91 | + sampleTimestamp := time.Now().Add(time.Minute).UnixMilli() |
| 92 | + |
| 93 | + // Send metrics to our component. These will be written to the WAL and |
| 94 | + // subsequently written to our HTTP server. |
| 95 | + lset1 := labels.FromStrings("foo", "bar") |
| 96 | + sendMetric(t, pipeline.Appender(t.Context()), lset1, sampleTimestamp, 12) |
| 97 | + lset2 := labels.FromStrings("fizz", "buzz") |
| 98 | + sendMetric(t, pipeline.Appender(t.Context()), lset2, sampleTimestamp, 34) |
| 99 | + |
| 100 | + expect := []prompb.TimeSeries{{ |
| 101 | + Labels: []prompb.Label{ |
| 102 | + {Name: "cluster", Value: "local"}, |
| 103 | + {Name: "foo", Value: "bar"}, |
| 104 | + {Name: "lbl", Value: "foo"}, |
| 105 | + }, |
| 106 | + Samples: []prompb.Sample{ |
| 107 | + {Timestamp: sampleTimestamp, Value: 12}, |
| 108 | + }, |
| 109 | + }, { |
| 110 | + Labels: []prompb.Label{ |
| 111 | + {Name: "cluster", Value: "local"}, |
| 112 | + {Name: "fizz", Value: "buzz"}, |
| 113 | + {Name: "lbl", Value: "foo"}, |
| 114 | + }, |
| 115 | + Samples: []prompb.Sample{ |
| 116 | + {Timestamp: sampleTimestamp, Value: 34}, |
| 117 | + }, |
| 118 | + }} |
| 119 | + |
| 120 | + select { |
| 121 | + case <-time.After(time.Minute): |
| 122 | + require.FailNow(t, "timed out waiting for metrics") |
| 123 | + case res := <-writeResult: |
| 124 | + require.Equal(t, expect, res.Timeseries) |
| 125 | + } |
| 126 | + |
| 127 | + ref := ls.GetOrAddGlobalRefID(lset1) |
| 128 | + require.NotZero(t, ref) |
| 129 | + // This was relabeled, so we shouldn't have a local ref |
| 130 | + localRef := ls.GetLocalRefID("prometheus.remote_write.test", ref) |
| 131 | + require.Zero(t, localRef) |
| 132 | + |
| 133 | + ref = ls.GetOrAddGlobalRefID(lset2) |
| 134 | + require.NotZero(t, ref) |
| 135 | + // This was relabeled, so we shouldn't have a local ref |
| 136 | + localRef = ls.GetLocalRefID("prometheus.remote_write.test", ref) |
| 137 | + require.Zero(t, localRef) |
| 138 | + |
| 139 | + lset1Relabeled := labels.NewBuilder(lset1).Set("lbl", "foo").Labels() |
| 140 | + ref = ls.GetOrAddGlobalRefID(lset1Relabeled) |
| 141 | + require.NotZero(t, ref) |
| 142 | + localRef = ls.GetLocalRefID("prometheus.remote_write.test", ref) |
| 143 | + require.NotZero(t, localRef) |
| 144 | + |
| 145 | + lset2Relabeled := labels.NewBuilder(lset2).Set("lbl", "foo").Labels() |
| 146 | + ref = ls.GetOrAddGlobalRefID(lset2Relabeled) |
| 147 | + require.NotZero(t, ref) |
| 148 | + localRef = ls.GetLocalRefID("prometheus.remote_write.test", ref) |
| 149 | + require.NotZero(t, localRef) |
| 150 | +} |
| 151 | + |
| 152 | +func channelServerHandler(t *testing.T) (http.HandlerFunc, chan *prompb.WriteRequest) { |
| 153 | + writeResult := make(chan *prompb.WriteRequest) |
| 154 | + serverHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 155 | + req, err := remote.DecodeWriteRequest(r.Body) |
| 156 | + if err != nil { |
| 157 | + http.Error(w, err.Error(), http.StatusBadRequest) |
| 158 | + return |
| 159 | + } |
| 160 | + |
| 161 | + select { |
| 162 | + case writeResult <- req: |
| 163 | + default: |
| 164 | + require.Fail(t, "failed to send remote_write result over channel") |
| 165 | + } |
| 166 | + }) |
| 167 | + |
| 168 | + return serverHandler, writeResult |
| 169 | +} |
| 170 | + |
| 171 | +func BenchmarkPipelines(b *testing.B) { |
| 172 | + tests := []struct { |
| 173 | + name string |
| 174 | + pipelineBuilder func(t testing.TB, logger log.Logger, handlerFunc http.HandlerFunc) (storage.Appendable, *httptest.Server, labelstore.LabelStore) |
| 175 | + }{ |
| 176 | + {"default", newDefaultPipeline}, |
| 177 | + {"relabel", newRelabelPipeline}, |
| 178 | + } |
| 179 | + |
| 180 | + numberOfMetrics := []int{2, 10, 100, 1000} |
| 181 | + |
| 182 | + for _, n := range numberOfMetrics { |
| 183 | + for _, tt := range tests { |
| 184 | + // Don't need the server to do anything here |
| 185 | + pipeline, srv, _ := tt.pipelineBuilder(b, log.NewNopLogger(), func(w http.ResponseWriter, r *http.Request) {}) |
| 186 | + b.Run(fmt.Sprintf("%s/%d-metrics", tt.name, n), func(b *testing.B) { |
| 187 | + b.ReportAllocs() |
| 188 | + b.ResetTimer() |
| 189 | + |
| 190 | + for b.Loop() { |
| 191 | + for i := 0; i < n; i++ { |
| 192 | + sendMetric( |
| 193 | + b, |
| 194 | + pipeline.Appender(b.Context()), |
| 195 | + labels.FromStrings(fmt.Sprintf("metric-%d", i), fmt.Sprintf("metric-%d", i)), |
| 196 | + time.Now().Add(time.Minute).UnixMilli(), |
| 197 | + float64(i), |
| 198 | + ) |
| 199 | + } |
| 200 | + } |
| 201 | + }) |
| 202 | + srv.Close() |
| 203 | + } |
| 204 | + } |
| 205 | +} |
| 206 | + |
| 207 | +func newDefaultPipeline(t testing.TB, logger log.Logger, handlerFunc http.HandlerFunc) (storage.Appendable, *httptest.Server, labelstore.LabelStore) { |
| 208 | + srv := httptest.NewServer(handlerFunc) |
| 209 | + |
| 210 | + ls := labelstore.New(logger, promclient.DefaultRegisterer) |
| 211 | + rwAppendable := newRemoteWriteComponent(t, logger, srv.URL, ls) |
| 212 | + pipelineAppendable := prometheus.NewFanout([]storage.Appendable{rwAppendable}, promclient.DefaultRegisterer, ls) |
| 213 | + |
| 214 | + return pipelineAppendable, srv, ls |
| 215 | +} |
| 216 | + |
| 217 | +func newRelabelPipeline(t testing.TB, logger log.Logger, handlerFunc http.HandlerFunc) (storage.Appendable, *httptest.Server, labelstore.LabelStore) { |
| 218 | + srv := httptest.NewServer(handlerFunc) |
| 219 | + |
| 220 | + ls := labelstore.New(logger, promclient.DefaultRegisterer) |
| 221 | + rwAppendable := newRemoteWriteComponent(t, logger, srv.URL, ls) |
| 222 | + relabelAppendable := newRelabelComponent(t, logger, []storage.Appendable{rwAppendable}, ls) |
| 223 | + pipelineAppendable := prometheus.NewFanout([]storage.Appendable{relabelAppendable}, promclient.DefaultRegisterer, ls) |
| 224 | + |
| 225 | + return pipelineAppendable, srv, ls |
| 226 | +} |
| 227 | + |
| 228 | +func newRemoteWriteComponent(t testing.TB, logger log.Logger, url string, ls *labelstore.Service) storage.Appendable { |
| 229 | + // Create our component and wait for it to start running, so we can write |
| 230 | + // metrics to the WAL. |
| 231 | + cfg := fmt.Sprintf(` |
| 232 | + external_labels = { |
| 233 | + cluster = "local", |
| 234 | + } |
| 235 | + endpoint { |
| 236 | + name = "test-url" |
| 237 | + url = "%s/api/v1/write" |
| 238 | + remote_timeout = "100ms" |
| 239 | +
|
| 240 | + queue_config { |
| 241 | + batch_send_deadline = "100ms" |
| 242 | + } |
| 243 | + } |
| 244 | + `, url) |
| 245 | + var args remotewrite.Arguments |
| 246 | + require.NoError(t, syntax.Unmarshal([]byte(cfg), &args)) |
| 247 | + |
| 248 | + tc, err := componenttest.NewControllerFromID(logger, "prometheus.remote_write") |
| 249 | + require.NoError(t, err) |
| 250 | + go func() { |
| 251 | + err = tc.Run(componenttest.TestContext(t), args, func(opts component.Options) component.Options { |
| 252 | + inner := opts.GetServiceData |
| 253 | + opts.GetServiceData = func(name string) (interface{}, error) { |
| 254 | + if name == labelstore.ServiceName { |
| 255 | + return ls, nil |
| 256 | + } |
| 257 | + return inner(name) |
| 258 | + } |
| 259 | + return opts |
| 260 | + }) |
| 261 | + require.NoError(t, err) |
| 262 | + }() |
| 263 | + require.NoError(t, tc.WaitRunning(5*time.Second)) |
| 264 | + |
| 265 | + return tc.Exports().(remotewrite.Exports).Receiver |
| 266 | +} |
| 267 | + |
| 268 | +func newRelabelComponent(t testing.TB, logger log.Logger, forwardTo []storage.Appendable, ls *labelstore.Service) storage.Appendable { |
| 269 | + cfg := `forward_to = [] |
| 270 | + rule { |
| 271 | + action = "replace" |
| 272 | + target_label = "lbl" |
| 273 | + replacement = "foo" |
| 274 | + }` |
| 275 | + var args relabel.Arguments |
| 276 | + require.NoError(t, syntax.Unmarshal([]byte(cfg), &args)) |
| 277 | + args.ForwardTo = forwardTo |
| 278 | + |
| 279 | + tc, err := componenttest.NewControllerFromID(logger, "prometheus.relabel") |
| 280 | + require.NoError(t, err) |
| 281 | + go func() { |
| 282 | + err = tc.Run(componenttest.TestContext(t), args, func(opts component.Options) component.Options { |
| 283 | + inner := opts.GetServiceData |
| 284 | + opts.GetServiceData = func(name string) (interface{}, error) { |
| 285 | + if name == labelstore.ServiceName { |
| 286 | + return ls, nil |
| 287 | + } |
| 288 | + return inner(name) |
| 289 | + } |
| 290 | + return opts |
| 291 | + }) |
| 292 | + require.NoError(t, err) |
| 293 | + }() |
| 294 | + require.NoError(t, tc.WaitRunning(5*time.Second)) |
| 295 | + |
| 296 | + return tc.Exports().(relabel.Exports).Receiver |
| 297 | +} |
| 298 | + |
| 299 | +func sendMetric( |
| 300 | + t testing.TB, |
| 301 | + appender storage.Appender, |
| 302 | + labels labels.Labels, |
| 303 | + time int64, |
| 304 | + value float64, |
| 305 | +) { |
| 306 | + _, err := appender.Append(0, labels, time, value) |
| 307 | + require.NoError(t, err) |
| 308 | + require.NoError(t, appender.Commit()) |
| 309 | +} |
0 commit comments