Skip to content
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

[FEATURE] adding otlp endpoint #7996

Open
wants to merge 28 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
4415626
[FEATURE] adding otlp endpoint
nicolastakashi Dec 16, 2024
58e4bc2
[FEATURE] adding otlp endpoint
nicolastakashi Dec 16, 2024
6b464fa
[FEATURE] adding otlp endpoint
nicolastakashi Dec 16, 2024
8a80246
Merge branch 'main' into chore/adding-otlp-endpoint
nicolastakashi Dec 17, 2024
5f71e26
[FIX] e2e tests for otlp receiver
nicolastakashi Dec 18, 2024
593aca7
Merge branch 'main' into chore/adding-otlp-endpoint
nicolastakashi Dec 18, 2024
fdd473a
[CHORE] adding otlp flags
nicolastakashi Dec 18, 2024
18a48b6
[DOC] updating docs
nicolastakashi Dec 18, 2024
13761e2
[CHORE] copying otlptranslator
nicolastakashi Dec 18, 2024
6431b67
[CHORE] copying otlptranslator tests
nicolastakashi Dec 18, 2024
b163dab
[CHORE] copying otlptranslator tests
nicolastakashi Dec 18, 2024
543a28f
[CHORE] copying otlptranslator tests
nicolastakashi Dec 18, 2024
5aa4063
[FIX] lint issues
nicolastakashi Dec 18, 2024
f9841ba
[FIX] lint issues
nicolastakashi Dec 18, 2024
991c8e5
[FIX] lint issues
nicolastakashi Dec 18, 2024
0dc5845
[CHORE] using multi errors
nicolastakashi Dec 19, 2024
65dc21b
[CHORE] using multi errors
nicolastakashi Dec 19, 2024
ab46f3b
[FIX] span naming convention
nicolastakashi Dec 19, 2024
8be010c
[TEST] adding handler otlp unit test
nicolastakashi Dec 19, 2024
5b4600b
[TEST] upgrade collector version
nicolastakashi Dec 19, 2024
706e41e
[FIX] golang lint
nicolastakashi Dec 19, 2024
b57f248
Merge branch 'main' into chore/adding-otlp-endpoint
nicolastakashi Dec 19, 2024
fc5e612
[CHORE] adding allow size bytes limit gate
nicolastakashi Dec 20, 2024
12dbf04
[FIX] unit test otlp endpoint
nicolastakashi Dec 20, 2024
63ce67b
Apply suggestions from code review
nicolastakashi Dec 20, 2024
d2e6b60
[FIX] unit test otlp endpoint
nicolastakashi Dec 20, 2024
a2bef89
[DOC] updating docs
nicolastakashi Dec 20, 2024
493fe90
Merge branch 'main' into chore/adding-otlp-endpoint
nicolastakashi Dec 20, 2024
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
7 changes: 7 additions & 0 deletions cmd/thanos/receive.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,8 @@ func runReceive(

AsyncForwardWorkerCount: conf.asyncForwardWorkerCount,
ReplicationProtocol: receive.ReplicationProtocol(conf.replicationProtocol),
OtlpDisableTargetInfo: conf.otlpDisableTargetInfo,
OtlpResourceAttributes: conf.otlpResourceAttributes,
})

grpcProbe := prober.NewGRPC()
Expand Down Expand Up @@ -897,6 +899,8 @@ type receiveConfig struct {

headExpandedPostingsCacheSize uint64
compactedBlocksExpandedPostingsCacheSize uint64
otlpDisableTargetInfo bool
otlpResourceAttributes []string
}

func (rc *receiveConfig) registerFlag(cmd extkingpin.FlagClause) {
Expand Down Expand Up @@ -1052,6 +1056,9 @@ func (rc *receiveConfig) registerFlag(cmd extkingpin.FlagClause) {
cmd.Flag("receive.limits-config-reload-timer", "Minimum amount of time to pass for the limit configuration to be reloaded. Helps to avoid excessive reloads.").
Default("1s").Hidden().DurationVar(&rc.limitsConfigReloadTimer)

cmd.Flag("receive.otlp-disable-target-info", "Disable target information OTLP metrics ingested by Receive.").Default("false").BoolVar(&rc.otlpDisableTargetInfo)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two suggestions:

  • Maybe it would feel a bit more natural to make it a positive setting, i.e. make receive.otlp-enable-target-info (since that is also the default)
  • Describe what exactly enabling / disabling this means, something like If enabled, converts the resource to the target info metric.

cmd.Flag("receive.otlp-resource-attributes", "(Repeatable) Resource attributes to include in OTLP metrics ingested by Receive.").Default("").StringsVar(&rc.otlpResourceAttributes)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On second look, this might be a little confusing - this should indicate which resource attributes should be used as metric labels, correct? Maybe we could retain the original parameter name, something like receive.otlp-promote-resource-attributes?


rc.featureList = cmd.Flag("enable-feature", "Comma separated experimental feature names to enable. The current list of features is "+metricNamesFilter+".").Default("").Strings()
}

Expand Down
8 changes: 7 additions & 1 deletion docs/components/receive.md
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ Please see the metric `thanos_receive_forward_delay_seconds` to see if you need

The following formula is used for calculating quorum:

```go mdox-exec="sed -n '1015,1025p' pkg/receive/handler.go"
```go mdox-exec="sed -n '1029,1039p' pkg/receive/handler.go"
// writeQuorum returns minimum number of replicas that has to confirm write success before claiming replication success.
func (h *Handler) writeQuorum() int {
// NOTE(GiedriusS): this is here because otherwise RF=2 doesn't make sense as all writes
Expand Down Expand Up @@ -456,6 +456,12 @@ Flags:
configuration. If it's empty AND hashring
configuration was provided, it means that
receive will run in RoutingOnly mode.
--receive.otlp-disable-target-info
Disable target information OTLP metrics
ingested by Receive.
--receive.otlp-resource-attributes= ...
(Repeatable) Resource attributes to include in
OTLP metrics ingested by Receive.
--receive.relabel-config=<content>
Alternative to 'receive.relabel-config-file'
flag (mutually exclusive). Content of YAML file
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ require (
github.com/onsi/gomega v1.34.0
github.com/prometheus-community/prom-label-proxy v0.8.1-0.20240127162815-c1195f9aabc0
github.com/seiflotfy/cuckoofilter v0.0.0-20240715131351-a2f2c23f1771
go.opentelemetry.io/collector/pdata v1.14.1
go.opentelemetry.io/collector/semconv v0.108.1
go.opentelemetry.io/contrib/propagators/autoprop v0.54.0
go4.org/intern v0.0.0-20230525184215-6c62f75575cb
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8
Expand Down Expand Up @@ -154,8 +156,6 @@ require (
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 // indirect
github.com/sercand/kuberesolver/v4 v4.0.0 // indirect
github.com/zhangyunhao116/umap v0.0.0-20221211160557-cb7705fafa39 // indirect
go.opentelemetry.io/collector/pdata v1.14.1 // indirect
go.opentelemetry.io/collector/semconv v0.108.1 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0 // indirect
go.opentelemetry.io/contrib/propagators/ot v1.29.0 // indirect
go4.org/unsafe/assume-no-moving-gc v0.0.0-20230525183740-e7c30c78aeb2 // indirect
Expand Down
14 changes: 14 additions & 0 deletions pkg/receive/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ type Options struct {
Limiter *Limiter
AsyncForwardWorkerCount uint
ReplicationProtocol ReplicationProtocol
OtlpDisableTargetInfo bool
OtlpResourceAttributes []string
}

// Handler serves a Prometheus remote write receiving HTTP endpoint.
Expand Down Expand Up @@ -275,6 +277,18 @@ func NewHandler(logger log.Logger, o *Options) *Handler {
),
)

h.router.Post(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@saswatamcode just FYI use this endpoint naming will require users to use the metrics_endpoint instead of endpoint property, maybe worth document it or writing a blog post later about Thanos and OTLP wdyt?

"/api/v1/otlp",
instrf(
"otlp",
readyf(
middleware.RequestID(
http.HandlerFunc(h.receiveOTLPHTTP),
),
),
),
)

statusAPI := statusapi.New(statusapi.Options{
GetStats: h.getStats,
Registry: h.options.Registry,
Expand Down
178 changes: 178 additions & 0 deletions pkg/receive/handler_otlp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
// Copyright (c) The Thanos Authors.
// Licensed under the Apache License 2.0.

package receive

import (
"context"
"net/http"
"strconv"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/pkg/errors"
"github.com/prometheus/prometheus/storage/remote"
"github.com/thanos-io/thanos/pkg/receive/otlptranslator"
tprompb "github.com/thanos-io/thanos/pkg/store/storepb/prompb"
"github.com/thanos-io/thanos/pkg/tenancy"
"github.com/thanos-io/thanos/pkg/tracing"
"go.opentelemetry.io/collector/pdata/pmetric"
)

func (h *Handler) receiveOTLPHTTP(w http.ResponseWriter, r *http.Request) {
nicolastakashi marked this conversation as resolved.
Show resolved Hide resolved
var err error
span, ctx := tracing.StartSpan(r.Context(), "receive_otlp_http")
span.SetTag("receiver.mode", string(h.receiverMode))
defer span.Finish()

tenant, err := tenancy.GetTenantFromHTTP(r, h.options.TenantHeader, h.options.DefaultTenantID, h.options.TenantField)
matej-g marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
level.Error(h.logger).Log("msg", "error getting tenant from HTTP", "err", err)

Check failure

Code scanning / CodeQL

Clear-text logging of sensitive information High

Sensitive data returned by HTTP request headers
flows to a logging call.
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

tLogger := log.With(h.logger, "tenant", tenant)
span.SetTag("tenant", tenant)

writeGate := h.Limiter.WriteGate()
tracing.DoInSpan(r.Context(), "receive_write_gate_ismyturn", func(ctx context.Context) {
err = writeGate.Start(r.Context())
})

defer writeGate.Done()
if err != nil {
level.Error(tLogger).Log("err", err, "msg", "internal server error")

Check failure

Code scanning / CodeQL

Clear-text logging of sensitive information High

Sensitive data returned by HTTP request headers
flows to a logging call.
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

under, err := h.Limiter.HeadSeriesLimiter().isUnderLimit(tenant)
if err != nil {
level.Error(tLogger).Log("msg", "error while limiting", "err", err.Error())
}

// Fail request fully if tenant has exceeded set limit.
if !under {
http.Error(w, "tenant is above active series limit", http.StatusTooManyRequests)
return
}

requestLimiter := h.Limiter.RequestLimiter()
if r.ContentLength >= 0 {
if !requestLimiter.AllowSizeBytes(tenant, r.ContentLength) {
http.Error(w, "write request too large", http.StatusRequestEntityTooLarge)
return
}
}

req, err := remote.DecodeOTLPWriteRequest(r)
nicolastakashi marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
level.Error(h.logger).Log("msg", "Error decoding remote write request", "err", err.Error())
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

metrics, _, err := h.convertToPrometheusFormat(ctx, req.Metrics())
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

totalSamples := 0
for _, ts := range metrics {
totalSamples += len(ts.Samples)
}

if !requestLimiter.AllowSeries(tenant, int64(len(metrics))) {
http.Error(w, "too many timeseries", http.StatusRequestEntityTooLarge)
return
}

if !requestLimiter.AllowSamples(tenant, int64(totalSamples)) {
http.Error(w, "too many samples", http.StatusRequestEntityTooLarge)
return
}

rep := uint64(0)
// If the header is empty, we assume the request is not yet replicated.
if replicaRaw := r.Header.Get(h.options.ReplicaHeader); replicaRaw != "" {
if rep, err = strconv.ParseUint(replicaRaw, 10, 64); err != nil {
http.Error(w, "could not parse replica header", http.StatusBadRequest)
return
}
}

//TODO: (nicolastakashi) Handle metadata in the future.
wreq := tprompb.WriteRequest{
Timeseries: metrics,
}

// Exit early if the request contained no data. We don't support metadata yet. We also cannot fail here, because
// this would mean lack of forward compatibility for remote write proto.
if len(wreq.Timeseries) == 0 {
// TODO(yeya24): Handle remote write metadata.
if len(wreq.Metadata) > 0 {
// TODO(bwplotka): Do we need this error message?
level.Debug(tLogger).Log("msg", "only metadata from client; metadata ingestion not supported; skipping")
return
}
level.Debug(tLogger).Log("msg", "empty remote write request; client bug or newer remote write protocol used?; skipping")
return
}

// Apply relabeling configs.
h.relabel(&wreq)
if len(wreq.Timeseries) == 0 {
level.Debug(tLogger).Log("msg", "remote write request dropped due to relabeling.")
return
}

responseStatusCode := http.StatusOK
tenantStats, err := h.handleRequest(ctx, rep, tenant, &wreq)
if err != nil {
level.Debug(tLogger).Log("msg", "failed to handle request", "err", err.Error())
switch errors.Cause(err) {
case errNotReady:
responseStatusCode = http.StatusServiceUnavailable
case errUnavailable:
responseStatusCode = http.StatusServiceUnavailable
case errConflict:
responseStatusCode = http.StatusConflict
case errBadReplica:
responseStatusCode = http.StatusBadRequest
default:
level.Error(tLogger).Log("err", err, "msg", "internal server error")

Check failure

Code scanning / CodeQL

Clear-text logging of sensitive information High

Sensitive data returned by HTTP request headers
flows to a logging call.
responseStatusCode = http.StatusInternalServerError
}
http.Error(w, err.Error(), responseStatusCode)
}

for tenant, stats := range tenantStats {
h.writeTimeseriesTotal.WithLabelValues(strconv.Itoa(responseStatusCode), tenant).Observe(float64(stats.timeseries))
h.writeSamplesTotal.WithLabelValues(strconv.Itoa(responseStatusCode), tenant).Observe(float64(stats.totalSamples))
}

}

func (h *Handler) convertToPrometheusFormat(ctx context.Context, pmetrics pmetric.Metrics) ([]tprompb.TimeSeries, []tprompb.MetricMetadata, error) {
converter := otlptranslator.NewPrometheusConverter()
settings := otlptranslator.Settings{
AddMetricSuffixes: true,
DisableTargetInfo: h.options.OtlpDisableTargetInfo,
PromoteResourceAttributes: h.options.OtlpResourceAttributes,
}

annots, err := converter.FromMetrics(ctx, pmetrics, settings)
ws, _ := annots.AsStrings("", 0, 0)
if len(ws) > 0 {
level.Warn(h.logger).Log("msg", "Warnings translating OTLP metrics to Prometheus write request", "warnings", ws)
}

if err != nil {
level.Error(h.logger).Log("msg", "Error translating OTLP metrics to Prometheus write request", "err", err)
return nil, nil, err.Err()
}

return converter.TimeSeries(), converter.Metadata(), nil
}
Loading
Loading