From 945894e049de6d9dd27544dbc2c99b3492be73f1 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Mon, 10 Jun 2019 19:51:05 +0300 Subject: [PATCH] app/vmselect: properly handle empty label (aka __name__) in LabelEntries handler --- app/vmselect/netstorage/netstorage.go | 6 +++--- app/vmstorage/transport/server.go | 11 ++++++++--- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/app/vmselect/netstorage/netstorage.go b/app/vmselect/netstorage/netstorage.go index 7eabf8421a..8813b724cb 100644 --- a/app/vmselect/netstorage/netstorage.go +++ b/app/vmselect/netstorage/netstorage.go @@ -579,9 +579,6 @@ func GetLabelEntries(at *auth.Token, deadline Deadline) ([]storage.TagEntry, boo isPartialResult = true } - // Deduplicate label entries - labelEntries = deduplicateLabelEntries(labelEntries) - // Substitute "" with "__name__" for i := range labelEntries { e := &labelEntries[i] @@ -590,6 +587,9 @@ func GetLabelEntries(at *auth.Token, deadline Deadline) ([]storage.TagEntry, boo } } + // Deduplicate label entries + labelEntries = deduplicateLabelEntries(labelEntries) + // Sort labelEntries by the number of label values in each entry. sort.Slice(labelEntries, func(i, j int) bool { a, b := labelEntries[i].Values, labelEntries[j].Values diff --git a/app/vmstorage/transport/server.go b/app/vmstorage/transport/server.go index a296f55300..41421e6970 100644 --- a/app/vmstorage/transport/server.go +++ b/app/vmstorage/transport/server.go @@ -641,11 +641,16 @@ func (s *Server) processVMSelectLabelEntries(ctx *vmselectRequestCtx) error { // Send labelEntries to vmselect for i := range labelEntries { e := &labelEntries[i] - if err := ctx.writeString(e.Key); err != nil { - return fmt.Errorf("cannot write label %q: %s", e.Key, err) + label := e.Key + if label == "" { + // Do this substitution in order to prevent clashing with 'end of response' marker. + label = "__name__" + } + if err := ctx.writeString(label); err != nil { + return fmt.Errorf("cannot write label %q: %s", label, err) } if err := writeLabelValues(ctx, e.Values); err != nil { - return fmt.Errorf("cannot write label values for %q: %s", e.Key, err) + return fmt.Errorf("cannot write label values for %q: %s", label, err) } }