Skip to content

Commit

Permalink
fix: label namespace logic optional
Browse files Browse the repository at this point in the history
  • Loading branch information
thobianchi committed May 17, 2021
1 parent 86fa637 commit 2207cf7
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 39 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/gogo/protobuf v1.3.2
github.com/golang/snappy v0.0.2
github.com/google/uuid v1.1.5
github.com/hashicorp/go-multierror v1.1.0
github.com/klauspost/compress v1.11.7 // indirect
github.com/pkg/errors v0.9.1
github.com/prometheus/prometheus v1.8.2-0.20210120113717-82330b96ee74
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ github.com/hashicorp/consul/api v1.3.0/go.mod h1:MmDNSzIMUjNpY/mQ398R4bk2FnqQLoP
github.com/hashicorp/consul/api v1.8.1/go.mod h1:sDjTOq0yUyv5G4h+BqSea7Fn6BU+XbolEz1952UB+mk=
github.com/hashicorp/consul/sdk v0.3.0/go.mod h1:VKf9jXwCTEY1QZP2MOLRhb5i/I/ssyNV1vwHyQBF0x8=
github.com/hashicorp/consul/sdk v0.7.0/go.mod h1:fY08Y9z5SvJqevyZNy6WWPXiG3KwBPAvlcdx16zZ0fM=
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-cleanhttp v0.5.0/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
Expand All @@ -396,6 +397,7 @@ github.com/hashicorp/go-immutable-radix v1.0.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjh
github.com/hashicorp/go-immutable-radix v1.2.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60=
github.com/hashicorp/go-msgpack v0.5.3/go.mod h1:ahLV/dePpqEmjfWmKiqvPkv/twdG7iPBM1vqhUKIvfM=
github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk=
github.com/hashicorp/go-multierror v1.1.0 h1:B9UzwGQJehnUY1yNrnwREHc3fGbC2xefo8g4TbElacI=
github.com/hashicorp/go-multierror v1.1.0/go.mod h1:spPvp8C1qA32ftKqdAHm4hHTbPw+vmowP0z+KUhOZdA=
github.com/hashicorp/go-retryablehttp v0.5.3/go.mod h1:9B5zBasrRhHXnJnui7y6sL7es7NDiJgTc6Er0maI1Xs=
github.com/hashicorp/go-rootcerts v1.0.0/go.mod h1:K6zTfqpRlCUIjkwsN4Z+hiSfzSTQa6eBIzfwKfwNnHU=
Expand Down
84 changes: 45 additions & 39 deletions processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/gogo/protobuf/proto"
"github.com/golang/snappy"
"github.com/google/uuid"
me "github.com/hashicorp/go-multierror"
"github.com/pkg/errors"
"github.com/prometheus/prometheus/prompb"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -125,48 +126,53 @@ func (p *processor) handle(ctx *fh.RequestCtx) {

log.Debugf("incoming timeseries numbers: %d", len(wrReqIn.Timeseries))

for _, ts := range wrReqIn.Timeseries {
tenant, err := p.processTimeseries(&ts)
if err != nil {
ctx.Error(err.Error(), fh.StatusInternalServerError)
return

if p.cfg.Tenant.NamespaceLabel != "" {
for _, ts := range wrReqIn.Timeseries {
tenant, err := p.processTimeseries(&ts)
if err != nil {
ctx.Error(err.Error(), fh.StatusInternalServerError)
return
}
_, ok := p.disp.nstschan[tenant]
if !ok {
log.Errorf("Not found chan for tenant: %s", tenant)
}
p.disp.nstschan[tenant] <- ts
}
_, ok := p.disp.nstschan[tenant]
if !ok {
log.Errorf("Not found chan for tenant: %s", tenant)
ctx.SetStatusCode(fh.StatusOK)
return
}
clientIP := ctx.RemoteAddr()
reqID, _ := uuid.NewRandom()

m, err := p.createWriteRequests(wrReqIn)
if err != nil {
ctx.Error(err.Error(), fh.StatusBadRequest)
return
}

var errs *me.Error
results := p.dispatch(clientIP, reqID, m)

for _, r := range results {
if r.err != nil {
errs = me.Append(errs, r.err)
p.Errorf("src=%s %s", clientIP, r.err)
} else if r.code < 200 || r.code >= 300 {
errs = me.Append(errs, fmt.Errorf("HTTP code %d (%s)", r.code, string(r.body)))
p.Errorf("src=%s req_id=%s HTTP code %d (%s)", clientIP, reqID, r.code, string(r.body))
}
p.disp.nstschan[tenant] <- ts
}
// clientIP := ctx.RemoteAddr()
// reqID, _ := uuid.NewRandom()

// m, err := p.createWriteRequests(wrReqIn)
// if err != nil {
// ctx.Error(err.Error(), fh.StatusBadRequest)
// return
// }

// var errs *me.Error
// results := p.dispatch(clientIP, reqID, m)

// for _, r := range results {
// if r.err != nil {
// errs = me.Append(errs, r.err)
// p.Errorf("src=%s %s", clientIP, r.err)
// } else if r.code < 200 || r.code >= 300 {
// errs = me.Append(errs, fmt.Errorf("HTTP code %d (%s)", r.code, string(r.body)))
// p.Errorf("src=%s req_id=%s HTTP code %d (%s)", clientIP, reqID, r.code, string(r.body))
// }
// }

// // Return 500 for any error
// if errs.ErrorOrNil() != nil {
// ctx.Error(errs.Error(), fh.StatusInternalServerError)
// return
// }

// // Otherwise if all went fine return the code and body from 1st request
// ctx.SetBody(results[0].body)

// Return 500 for any error
if errs.ErrorOrNil() != nil {
ctx.Error(errs.Error(), fh.StatusInternalServerError)
return
}

// Otherwise if all went fine return the code and body from 1st request
ctx.SetBody(results[0].body)
ctx.SetStatusCode(fh.StatusOK)
}

Expand Down

0 comments on commit 2207cf7

Please sign in to comment.