Skip to content

Commit

Permalink
Add tracing to exemplar APIs (#4299)
Browse files Browse the repository at this point in the history
* add tracing to exemplar APIs

Signed-off-by: Ian Billett <[email protected]>

* adds CHANGELOG

Signed-off-by: Ian Billett <[email protected]>

* linter

Signed-off-by: Ian Billett <[email protected]>

* Update pkg/api/query/v1.go

Co-authored-by: Kemal Akkoyun <[email protected]>
Signed-off-by: Ian Billett <[email protected]>

* rename

Signed-off-by: Ian Billett <[email protected]>

Co-authored-by: Kemal Akkoyun <[email protected]>
  • Loading branch information
bill3tt and kakkoyun authored Jun 3, 2021
1 parent 8bf32f9 commit e982ff8
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ We use _breaking :warning:_ to mark changes that are not backward compatible (re

### Added

- [#4299](https://github.com/thanos-io/thanos/pull/4299) Tracing: Add tracing to exemplar APIs.
- [#4117](https://github.com/thanos-io/thanos/pull/4117) Mixin: new alert ThanosReceiveTrafficBelowThreshold to flag if the ingestion average of the last hour dips below 50% of the ingestion average for the last 12 hours.
- [#4107](https://github.com/thanos-io/thanos/pull/4107) Store: `LabelNames` and `LabelValues` now support label matchers.
- [#3940](https://github.com/thanos-io/thanos/pull/3940) Sidecar: Added matchers support to `LabelValues`
Expand Down
17 changes: 15 additions & 2 deletions pkg/api/query/v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,15 @@ func NewExemplarsHandler(client exemplars.UnaryClient, enablePartialResponse boo
}

return func(r *http.Request) (interface{}, []error, *api.ApiError) {
span, ctx := tracing.StartSpan(r.Context(), "exemplar_query_request")
defer span.Finish()

var (
data []*exemplarspb.ExemplarData
warnings storage.Warnings
err error
)

start, err := cortexutil.ParseTime(r.FormValue("start"))
if err != nil {
return nil, nil, &api.ApiError{Typ: api.ErrorBadData, Err: err}
Expand All @@ -806,11 +815,15 @@ func NewExemplarsHandler(client exemplars.UnaryClient, enablePartialResponse boo
Query: r.FormValue("query"),
PartialResponseStrategy: ps,
}
exemplarsData, warnings, err := client.Exemplars(r.Context(), req)

tracing.DoInSpan(ctx, "retrieve_exemplars", func(ctx context.Context) {
data, warnings, err = client.Exemplars(ctx, req)
})

if err != nil {
return nil, nil, &api.ApiError{Typ: api.ErrorInternal, Err: errors.Wrap(err, "retrieving exemplars")}
}
return exemplarsData, warnings, nil
return data, warnings, nil
}
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/exemplars/exemplars.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/prometheus/prometheus/storage"
"github.com/thanos-io/thanos/pkg/exemplars/exemplarspb"
"github.com/thanos-io/thanos/pkg/store/labelpb"
"github.com/thanos-io/thanos/pkg/tracing"
)

var _ UnaryClient = &GRPCClient{}
Expand Down Expand Up @@ -73,6 +74,9 @@ func NewGRPCClientWithDedup(es exemplarspb.ExemplarsServer, replicaLabels []stri
}

func (rr *GRPCClient) Exemplars(ctx context.Context, req *exemplarspb.ExemplarsRequest) ([]*exemplarspb.ExemplarData, storage.Warnings, error) {
span, ctx := tracing.StartSpan(ctx, "exemplar_grpc_request")
defer span.Finish()

resp := &exemplarsServer{ctx: ctx}

if err := rr.proxy.Exemplars(req, resp); err != nil {
Expand Down
9 changes: 8 additions & 1 deletion pkg/exemplars/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
package exemplars

import (
"context"
"net/url"

"github.com/prometheus/prometheus/pkg/labels"
"github.com/thanos-io/thanos/pkg/exemplars/exemplarspb"
"github.com/thanos-io/thanos/pkg/promclient"
"github.com/thanos-io/thanos/pkg/store/labelpb"
"github.com/thanos-io/thanos/pkg/tracing"
)

// Prometheus implements exemplarspb.Exemplars gRPC that allows to fetch exemplars from Prometheus.
Expand Down Expand Up @@ -41,7 +43,12 @@ func (p *Prometheus) Exemplars(r *exemplarspb.ExemplarsRequest, s exemplarspb.Ex
for _, e := range exemplars {
// Make sure the returned series labels are sorted.
e.SetSeriesLabels(labelpb.ExtendSortedLabels(e.SeriesLabels.PromLabels(), extLset))
if err := s.Send(&exemplarspb.ExemplarsResponse{Result: &exemplarspb.ExemplarsResponse_Data{Data: e}}); err != nil {

var err error
tracing.DoInSpan(s.Context(), "send_exemplars_response", func(_ context.Context) {
err = s.Send(&exemplarspb.ExemplarsResponse{Result: &exemplarspb.ExemplarsResponse_Data{Data: e}})
})
if err != nil {
return err
}
}
Expand Down
11 changes: 9 additions & 2 deletions pkg/exemplars/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/prometheus/prometheus/promql/parser"
"github.com/thanos-io/thanos/pkg/exemplars/exemplarspb"
"github.com/thanos-io/thanos/pkg/store/storepb"
"github.com/thanos-io/thanos/pkg/tracing"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -52,6 +53,9 @@ type exemplarsStream struct {
}

func (s *Proxy) Exemplars(req *exemplarspb.ExemplarsRequest, srv exemplarspb.Exemplars_ExemplarsServer) error {
span, ctx := tracing.StartSpan(srv.Context(), "proxy_exemplars")
defer span.Finish()

expr, err := parser.ParseExpr(req.Query)
if err != nil {
return err
Expand All @@ -72,7 +76,7 @@ func (s *Proxy) Exemplars(req *exemplarspb.ExemplarsRequest, srv exemplarspb.Exe
}

var (
g, gctx = errgroup.WithContext(srv.Context())
g, gctx = errgroup.WithContext(ctx)
respChan = make(chan *exemplarspb.ExemplarData, 10)
exemplars []*exemplarspb.ExemplarData
)
Expand Down Expand Up @@ -147,7 +151,10 @@ func (s *Proxy) Exemplars(req *exemplarspb.ExemplarsRequest, srv exemplarspb.Exe
}

for _, e := range exemplars {
if err := srv.Send(exemplarspb.NewExemplarsResponse(e)); err != nil {
tracing.DoInSpan(srv.Context(), "send_exemplars_response", func(_ context.Context) {
err = srv.Send(exemplarspb.NewExemplarsResponse(e))
})
if err != nil {
return status.Error(codes.Unknown, errors.Wrap(err, "send exemplars response").Error())
}
}
Expand Down

0 comments on commit e982ff8

Please sign in to comment.