Repository navigation
Conversation
| sl.cache.updateRef(ce, ref) | ||
| } | ||
| if len(exemplars) > 0 { | ||
| _, exErr := app.AppendExemplars(ref, lset, exemplars) |
There was a problem hiding this comment.
I was worried that this will get rid of benefits for OpenTelemetry Collector, but turns out on collector side the benefits were not really taken -- as is, it was still buffering exemplars in the transaction for the later build. So we should be fine, other than the pain of breaking change again -- and no optimization path forward.
There was a problem hiding this comment.
I think we just never got very far on the migration. There was someone working on it, but I don't remember where it ended up. I'll prototype and see what impact this change would have on the (potential future) collector usage.
There was a problem hiding this comment.
Sorry, still working on the PoCs and benchmarks to isolate the impact of splitting exemplars out. AppenderV2 is considerably more efficient (around 50% in some cases) compared to appender v1, but a lot of that is grouped histogram buckets, etc. being more performant.
There was a problem hiding this comment.
Ok, so IF the AppendExemplar call always comes immediately after the Append call, we can optimize that case to make them have virtually the same performance. Otherwise, if exemplars are fully split from metric data, there is a 15-20% percent performance hit for having them separate and needing to match them up.
There was a problem hiding this comment.
LMK if there are specific things you want me to check, or if that is enough for you to make your decision.
There was a problem hiding this comment.
Those numbers are great, thanks!
Ok, so IF the AppendExemplar call always comes immediately after the Append call, we can optimize that case to make them have virtually the same performance.
It's quite hacky to depend on this though.
There was a problem hiding this comment.
Playing with the alternative: #19811
Generally it's more code lines, more API surface, but allows tight exemplars where we can.
3b0566e to
c8fbef3
Compare
Exemplars were passed to AppenderV2.Append via AppendV2Options.Exemplars, requiring every implementation to also carry the AppendPartialError plumbing needed to report per-exemplar failures out of a call whose primary purpose is appending one sample. Move exemplars to their own mandatory AppendExemplars(ref, ls, exemplars) method on AppenderV2, mirroring how AppenderV1 already separates AppendExemplar from Append. This keeps the batching behaviour scrape relies on (one call per sample carrying all of its exemplars, not one call per exemplar), while letting callers that don't have a natural 1:1 sample/exemplars pairing (e.g. a remote-write receiver) call it independently. Making it mandatory removes the need for a separate optional capability interface and its associated type-assertion/capability-check plumbing. Signed-off-by: György Krajcsovits <[email protected]>
Rename now-unused opts parameters to _ in timestampTrackerV2.Append and agent's appenderV2.Append (exemplars moved out to AppendExemplars), and restructure a nested if into an early continue in teststorage's mock per gocritic's nestingReduce suggestion. Signed-off-by: György Krajcsovits <[email protected]>
Factor AppendExemplars into its own named ExemplarAppenderV2 interface, embedded into AppenderV2, mirroring how AppenderV1's Appender interface composes ExemplarAppender, HistogramAppender, MetadataUpdater, etc. as separate named interfaces rather than inlining their methods directly. AppendExemplars remains a mandatory part of AppenderV2 (embedding still requires it); this is purely a naming/organization change. Signed-off-by: György Krajcsovits <[email protected]>
c8fbef3 to
89ef560
Compare
aknuds1
left a comment
There was a problem hiding this comment.
AFAICT, it looks mostly good. But please see my suggestions.
| } | ||
|
|
||
| // AppendExemplars implements the mandatory AppenderV2.AppendExemplars capability. | ||
| // The series identified by ref MUST already exist (it MUST have been appended to |
There was a problem hiding this comment.
| // The series identified by ref MUST already exist (it MUST have been appended to | |
| // The series identified by ref and/or ls MUST already exist (it MUST have been appended to |
| } | ||
| // Series references and chunk references are identical for agent mode. | ||
| s := a.series.GetByID(chunks.HeadSeriesRef(ref)) | ||
| if s == nil { |
There was a problem hiding this comment.
According to ExemplarAppenderV2.AppendExemplars, ref should be optional. If not supplied, ls should be used instead.
| if s == nil { | |
| if s == nil { | |
| ls = ls.WithoutEmpty() | |
| s = a.series.GetByHash(ls.Hash(), ls) | |
| } | |
| if s == nil { |
There was a problem hiding this comment.
Let's perhaps update the comment instead? We expect series to be present
| _, err = app.AppendExemplars(ref, ls, []exemplar.Exemplar{e}) | ||
| require.NoError(t, err, "should not reject valid exemplars") | ||
| }) | ||
| } |
There was a problem hiding this comment.
Corresponding regression test:
| } | |
| } | |
| func TestDB_AppendExemplarsByLabels_AppendV2(t *testing.T) { | |
| s := createTestAgentDB(t, nil, DefaultOptions()) | |
| defer s.Close() | |
| ctx := t.Context() | |
| ls := labels.FromStrings("a", "1", "empty", "") | |
| canonicalLabels := ls.WithoutEmpty() | |
| app := s.AppenderV2(ctx) | |
| ref, err := app.Append(0, ls, 0, 1, 1, nil, nil, storage.AOptions{}) | |
| require.NoError(t, err) | |
| require.NoError(t, app.Commit()) | |
| e := exemplar.Exemplar{Labels: labels.FromStrings("trace_id", "123"), Value: 1, Ts: 1, HasTs: true} | |
| app = s.AppenderV2(ctx) | |
| gotRef, err := app.AppendExemplars(0, ls, []exemplar.Exemplar{e}) | |
| require.NoError(t, err) | |
| require.Equal(t, ref, gotRef) | |
| require.NoError(t, app.Commit()) | |
| require.Equal(t, &e, s.series.GetLatestExemplar(chunks.HeadSeriesRef(ref))) | |
| missingLabels := labels.FromStrings("missing", "series") | |
| app = s.AppenderV2(ctx) | |
| _, err = app.AppendExemplars(0, missingLabels, []exemplar.Exemplar{e}) | |
| require.EqualError(t, err, "unknown series ref when trying to add exemplars: 0") | |
| require.Nil(t, s.series.GetByHash(missingLabels.Hash(), missingLabels)) | |
| require.NotNil(t, s.series.GetByHash(canonicalLabels.Hash(), canonicalLabels)) | |
| require.NoError(t, app.Rollback()) | |
| } |
|
|
||
| s := a.head.series.getByID(chunks.HeadSeriesRef(ref)) | ||
| if s == nil { | ||
| s = a.head.series.getByHash(ls.Hash(), ls) |
There was a problem hiding this comment.
AppendExemplars should canonicalize ls, same as Append does:
| s = a.head.series.getByHash(ls.Hash(), ls) | |
| ls = ls.WithoutEmpty() | |
| s = a.head.series.getByHash(ls.Hash(), ls) |
|
|
||
| l := labels.FromStrings("trace_id", "123") | ||
|
|
||
| ls := labels.FromStrings("a", "b") |
There was a problem hiding this comment.
I suggest including an empty label, to verify label canonicalization:
| ls := labels.FromStrings("a", "b") | |
| ls := labels.FromStrings("a", "b", "empty", "") |
| }) | ||
| _, err = app.AppendExemplars(ref, ls, []exemplar.Exemplar{{Labels: l, HasTs: true, Ts: -1000, Value: 1}}) | ||
| require.NoError(t, err) | ||
| require.NoError(t, app.Commit()) |
There was a problem hiding this comment.
I suggest also testing that calling app.AppendExemplars without ref works.
| require.NoError(t, app.Commit()) | |
| require.NoError(t, app.Commit()) | |
| app = head.AppenderV2(ctx) | |
| gotRef, err := app.AppendExemplars(0, ls, []exemplar.Exemplar{{Labels: l, HasTs: true, Ts: -999, Value: 2}}) | |
| require.NoError(t, err) | |
| require.Equal(t, ref, gotRef) | |
| require.NoError(t, app.Commit()) |
There was a problem hiding this comment.
Thanks!
I am still wondering if we don't want to keep Exemplars on Appender option WHILE adding separate method (potentially optional, not even part of interface) for AppendExemplars for RW receive use. This would be:
- Cleaner going forward given all our inputs except RW sends sample with an exemplars 🤔
- Not break downstream (collector) and allow optimizations in future
| // The series identified by ref and/or ls MUST already exist (it MUST have been | ||
| // appended to via Append in the same or an earlier transaction). | ||
| func (a *headAppenderV2) AppendExemplars(ref storage.SeriesRef, ls labels.Labels, exemplars []exemplar.Exemplar) (storage.SeriesRef, error) { | ||
| if len(exemplars) == 0 || !a.head.opts.EnableExemplarStorage || a.head.opts.MaxExemplars.Load() <= 0 { |
There was a problem hiding this comment.
Returning 0, nil when exemplar storage is disabled or exemplars is empty wipes out the series ref for callers like fanout, and skips checking whether the series exists. Maybe validate the series and return storage.SeriesRef(s.ref), nil?
| // via Append in the same or an earlier transaction). | ||
| func (a *appenderV2) AppendExemplars(ref storage.SeriesRef, _ labels.Labels, exemplars []exemplar.Exemplar) (storage.SeriesRef, error) { | ||
| if len(exemplars) == 0 { | ||
| return 0, nil |
There was a problem hiding this comment.
nit: If exemplars is empty, returning 0, nil clobbers the caller's series ref. Should this return ref, nil instead?
| return 0, nil | |
| if len(exemplars) == 0 { | |
| return ref, nil | |
| } |
| } | ||
| // Series references and chunk references are identical for agent mode. | ||
| s := a.series.GetByID(chunks.HeadSeriesRef(ref)) | ||
| if s == nil { |
There was a problem hiding this comment.
Let's perhaps update the comment instead? We expect series to be present
|
Added an alternative #19455 (comment) As of now we have a decision to make: Pros of A
Pros of B
I am happy to accept the (A) and make it simpler 🤔 |
|
With LLMs coding is kind of cheap, so I'm ok with option B. Still better than the current situation with both v1 and v2. |
|
Cool, let's do B then if we are less afraid of complexity now (looking on what we merge, that's kind of what we see) |
|
Closing this in favor of #19811 |
TL;DR; Exemplars are not associated with samples in a pull model on a conceptual level. When we scrape a sample at timestamp T, the exemplar scraped at the same time might have timestamp T-3h or whatever.
I'm testing if making this change makes AppenderV2 easier to adopt in RW1, RW2. See #19250, #19251, #19252 without this change.
Summary
AppenderV2.Appendcurrently accepts exemplars inline viaAppendV2Options.Exemplars, which means every implementation also has to carry theAppendPartialErrorplumbing needed to report per-exemplar failures out of a call whose primary job is appending one sample.This moves exemplars to their own mandatory method on
AppenderV2:mirroring how
AppenderV1already separatesAppendExemplarfromAppend, but keeping it batched (a slice, not one exemplar at a time) so the scrape hot path doesn't regress to one extra interface call per exemplar.Making it mandatory (rather than an optional capability interface with a runtime type-assertion) removes the need for that capability-check plumbing entirely — every
AppenderV2implementation must support it, same as V1'sAppendExemplar.Changes
storage/interface_append.go: addAppendExemplarstoAppenderV2, dropExemplarsfromAppendV2Options, retargetAppendPartialError's docs fromAppendtoAppendExemplars.tsdb/head_append_v2.go,tsdb/agent/db_append_v2.go: implementAppendExemplars, drop exemplar handling fromAppend.storage/fanout.go: dispatchAppendExemplarsto primary + secondaries (same patternAppendused for exemplar partial errors before).scrape/scrape_append_v2.go+scrape/scrape.go: callAppendExemplarsafter a successfulAppend; split the oldcheckAddError's exemplar-partial-error branch into its owncheckExemplarsAddError.storage/remote/otlptranslator/prometheusremotewrite/{helper,histograms,number_data_points}.go: attach exemplars viaAppendExemplarsafterAppendinstead of inline (including the histogram-bucket exemplar-distribution loop inhelper.go).storage/remote/write_otlp_handler.go: move the partial-error-hiding logic from theAppendwrapper to a newAppendExemplarswrapper.storage/remote/write.go:timestampTrackerV2counts exemplars viaAppendExemplarsinstead ofopts.Exemplars.cmd/prometheus/main.go:notReadyAppenderV2gets anAppendExemplarsstub.util/teststorage,storage/fanout_test.go,tsdb/head_append_v2_test.go,tsdb/head_bench_test.go,tsdb/agent/db_append_v2_test.go,storage/remote/write_otlp_handler_test.go,storage/remote/otlptranslator/prometheusremotewrite/metrics_to_prw_test.go.Test plan
go build ./...passesgo vet ./...clean (aside from pre-existing, unrelatedSeekstdmethods warnings)go test ./...passes across the whole module