Skip to content

feat(tagged): refactor tag count querying, use it in autocomplete #318

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
95 changes: 72 additions & 23 deletions autocomplete/autocomplete.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,29 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
}

func (h *Handler) requestExpr(r *http.Request) (*where.Where, *where.Where, map[string]bool, error) {
f := r.Form["expr"]
expr := make([]string, 0, len(f))
func getTagCountQuerier(config *config.Config, opts clickhouse.Options) *finder.TagCountQuerier {
var tcq *finder.TagCountQuerier = nil
if config.ClickHouse.TagsCountTable != "" {
tcq = finder.NewTagCountQuerier(
config.ClickHouse.URL,
config.ClickHouse.TagsCountTable,
opts,
config.FeatureFlags.UseCarbonBehavior,
config.FeatureFlags.DontMatchMissingTags,
config.ClickHouse.TaggedUseDaily,
)
}

return tcq
}

func (h *Handler) requestExpr(r *http.Request, tcq *finder.TagCountQuerier, from, until int64) (*where.Where, *where.Where, map[string]bool, error) {
formExpr := r.Form["expr"]
expr := make([]string, 0, len(formExpr))

for i := 0; i < len(f); i++ {
if f[i] != "" {
expr = append(expr, f[i])
for i := 0; i < len(formExpr); i++ {
if formExpr[i] != "" {
expr = append(expr, formExpr[i])
}
}

Expand All @@ -95,6 +111,21 @@ func (h *Handler) requestExpr(r *http.Request) (*where.Where, *where.Where, map[
return wr, pw, usedTags, err
}

if tcq != nil {
tagValuesCosts, err := tcq.GetCostsFromCountTable(r.Context(), terms, from, until)
if err != nil {
return wr, pw, usedTags, err
}

if tagValuesCosts != nil {
finder.SetCosts(terms, tagValuesCosts)
} else if len(h.config.ClickHouse.TaggedCosts) != 0 {
finder.SetCosts(terms, h.config.ClickHouse.TaggedCosts)
}
}

finder.SortTaggedTermsByCost(terms)

wr, pw, err = finder.TaggedWhere(terms, h.config.FeatureFlags.UseCarbonBehavior, h.config.FeatureFlags.DontMatchMissingTags)
if err != nil {
return wr, pw, usedTags, err
Expand Down Expand Up @@ -214,6 +245,7 @@ func (h *Handler) ServeTags(w http.ResponseWriter, r *http.Request) {
queueFail bool
queueDuration time.Duration
findCache bool
opts clickhouse.Options
)

username := r.Header.Get("X-Forwarded-User")
Expand All @@ -239,7 +271,7 @@ func (h *Handler) ServeTags(w http.ResponseWriter, r *http.Request) {
limiter.SendDuration(queueDuration.Milliseconds())
metrics.SendFindMetrics(metrics.TagsRequestMetric, status, dMS, 0, h.config.Metrics.ExtendedStat, metricsCount)

if !findCache && chReadRows != 0 && chReadBytes != 0 {
if !findCache && chReadRows > 0 && chReadBytes > 0 {
errored := status != http.StatusOK && status != http.StatusNotFound
metrics.SendQueryRead(metrics.AutocompleteQMetric, 0, 0, dMS, metricsCount, readBytes, chReadRows, chReadBytes, errored)
}
Expand Down Expand Up @@ -290,7 +322,21 @@ func (h *Handler) ServeTags(w http.ResponseWriter, r *http.Request) {
}
}

wr, pw, usedTags, err := h.requestExpr(r)
opts = clickhouse.Options{
TLSConfig: h.config.ClickHouse.TLSConfig,
Timeout: h.config.ClickHouse.IndexTimeout,
ConnectTimeout: h.config.ClickHouse.ConnectTimeout,
CheckRequestProgress: h.config.FeatureFlags.LogQueryProgress,
ProgressSendingInterval: h.config.ClickHouse.ProgressSendingInterval,
}

wr, pw, usedTags, err := h.requestExpr(
r,
getTagCountQuerier(h.config, opts),
start.AddDate(0, 0, -h.config.ClickHouse.TaggedAutocompleDays).Unix(),
start.Unix(),
)

if err != nil {
status = http.StatusBadRequest
http.Error(w, err.Error(), status)
Expand Down Expand Up @@ -366,13 +412,7 @@ func (h *Handler) ServeTags(w http.ResponseWriter, r *http.Request) {
scope.WithTable(r.Context(), h.config.ClickHouse.TaggedTable),
h.config.ClickHouse.URL,
sql,
clickhouse.Options{
TLSConfig: h.config.ClickHouse.TLSConfig,
Timeout: h.config.ClickHouse.IndexTimeout,
ConnectTimeout: h.config.ClickHouse.ConnectTimeout,
CheckRequestProgress: h.config.FeatureFlags.LogQueryProgress,
ProgressSendingInterval: h.config.ClickHouse.ProgressSendingInterval,
},
opts,
nil,
)

Expand Down Expand Up @@ -490,6 +530,7 @@ func (h *Handler) ServeValues(w http.ResponseWriter, r *http.Request) {
queueFail bool
queueDuration time.Duration
findCache bool
opts clickhouse.Options
)

username := r.Header.Get("X-Forwarded-User")
Expand Down Expand Up @@ -567,8 +608,22 @@ func (h *Handler) ServeValues(w http.ResponseWriter, r *http.Request) {
}
}

opts = clickhouse.Options{
TLSConfig: h.config.ClickHouse.TLSConfig,
Timeout: h.config.ClickHouse.IndexTimeout,
ConnectTimeout: h.config.ClickHouse.ConnectTimeout,
CheckRequestProgress: h.config.FeatureFlags.LogQueryProgress,
ProgressSendingInterval: h.config.ClickHouse.ProgressSendingInterval,
}

if !findCache {
wr, pw, usedTags, err := h.requestExpr(r)
wr, pw, usedTags, err := h.requestExpr(
r,
getTagCountQuerier(h.config, opts),
start.AddDate(0, 0, -h.config.ClickHouse.TaggedAutocompleDays).Unix(),
start.Unix(),
)

if err == finder.ErrCostlySeriesByTag {
status = http.StatusForbidden
http.Error(w, err.Error(), status)
Expand Down Expand Up @@ -640,13 +695,7 @@ func (h *Handler) ServeValues(w http.ResponseWriter, r *http.Request) {
scope.WithTable(r.Context(), h.config.ClickHouse.TaggedTable),
h.config.ClickHouse.URL,
sql,
clickhouse.Options{
TLSConfig: h.config.ClickHouse.TLSConfig,
Timeout: h.config.ClickHouse.IndexTimeout,
ConnectTimeout: h.config.ClickHouse.ConnectTimeout,
CheckRequestProgress: h.config.FeatureFlags.LogQueryProgress,
ProgressSendingInterval: h.config.ClickHouse.ProgressSendingInterval,
},
opts,
nil,
)

Expand Down
Loading
Loading