-
Notifications
You must be signed in to change notification settings - Fork 66
[Otelsoak] Adds Vercel drain mode for otelsoak #1347
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
Changes from 8 commits
32fbed2
eded153
373902c
71299c8
bacc611
5b56899
8bd0d38
87cc4e2
db420a9
80404da
77ae6c8
e7cd9e5
800a25f
ddccaee
f544b08
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| include ../../../Makefile.Common |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| # HTTP body exporter | ||
|
carsonip marked this conversation as resolved.
|
||
|
|
||
| <!-- status autogenerated section --> | ||
| | Status | | | ||
| | ------------- |-----------| | ||
| | Stability | [development]: logs | | ||
| | Distributions | [] | | ||
|
|
||
| [development]: https://github.com/open-telemetry/opentelemetry-collector/blob/main/docs/component-stability.md#development | ||
| <!-- end autogenerated section --> | ||
|
|
||
| The HTTP body exporter POSTs log record bodies as the raw HTTP request body. | ||
| Bodies are joined with newlines (NDJSON-friendly). It does **not** send OTLP. | ||
|
|
||
| This is intended for load generation against Managed Input drain endpoints | ||
| (for example Vercel) when used with [`loadgenreceiver`](../../receiver/loadgenreceiver) both in otelsoak / otelbench. | ||
|
|
||
| ## Configuration | ||
|
|
||
| ```yaml | ||
| exporters: | ||
| http: | ||
| endpoint: https://example.ingest.us-central1.gcp.qa.elastic.cloud/inputs/vercel/_default_ | ||
| headers: | ||
| - name: Authorization | ||
| value: "ApiKey ${env:ELASTIC_APM_API_KEY}" | ||
| tls: | ||
| insecure_skip_verify: true | ||
| timeout: 60s | ||
| ``` | ||
|
|
||
| | Field | Description | | ||
| | --- | --- | | ||
| | `endpoint` | Full URL to POST (required). | | ||
| | `headers` | Extra HTTP headers (e.g. `Authorization`). | | ||
| | `tls` | Standard collector TLS client settings. | | ||
| | `timeout` | HTTP client timeout (default `30s`). | | ||
| | `retry_on_failure` | Standard exporter retry settings. | | ||
| | `sending_queue` | Standard exporter queue settings. | | ||
|
|
||
| `Content-Type` defaults to `application/json` when not set in `headers`. | ||
|
|
||
| ## Sample otelsoak pipeline (Vercel drain) | ||
|
|
||
| Put each drain NDJSON line in a loadgen OTLP-JSONL log body (see | ||
| `loadgen/cmd/otelsoak/testdata/vercel/`). Select logs, speed-insights, or both | ||
| with `VERCEL_SIGNAL` in [`config.vercel.example.yaml`](../../loadgen/cmd/otelsoak/config.vercel.example.yaml). | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| // Licensed to Elasticsearch B.V. under one or more contributor | ||
| // license agreements. See the NOTICE file distributed with | ||
| // this work for additional information regarding copyright | ||
| // ownership. Elasticsearch B.V. licenses this file to you under | ||
| // the Apache License, Version 2.0 (the "License"); you may | ||
| // not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| package httpexporter // import "github.com/elastic/opentelemetry-collector-components/internal/exporter/httpexporter" | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "net/url" | ||
|
|
||
| "go.opentelemetry.io/collector/config/confighttp" | ||
| "go.opentelemetry.io/collector/config/configoptional" | ||
| "go.opentelemetry.io/collector/config/configretry" | ||
| "go.opentelemetry.io/collector/exporter/exporterhelper" | ||
| ) | ||
|
|
||
| // Config configures the HTTP body exporter. | ||
| type Config struct { | ||
| // ClientConfig holds standard HTTP client settings (endpoint, headers, TLS, timeout). | ||
| confighttp.ClientConfig `mapstructure:",squash"` | ||
|
|
||
| // RetryConfig defines retry configuration for failed exports. | ||
| RetryConfig configretry.BackOffConfig `mapstructure:"retry_on_failure"` | ||
|
|
||
| // QueueConfig defines optional sending queue settings. | ||
| QueueConfig configoptional.Optional[exporterhelper.QueueBatchConfig] `mapstructure:"sending_queue"` | ||
| } | ||
|
|
||
| func (cfg *Config) Validate() error { | ||
| if cfg.Endpoint == "" { | ||
| return errors.New("endpoint is required") | ||
| } | ||
| u, err := url.Parse(cfg.Endpoint) | ||
| if err != nil { | ||
| return fmt.Errorf("endpoint must be a valid URL: %w", err) | ||
| } | ||
| if u.Scheme != "http" && u.Scheme != "https" { | ||
| return fmt.Errorf("endpoint scheme must be http or https, got %q", u.Scheme) | ||
| } | ||
| if u.Host == "" { | ||
| return errors.New("endpoint must include a host") | ||
| } | ||
| return nil | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| // Licensed to Elasticsearch B.V. under one or more contributor | ||
| // license agreements. See the NOTICE file distributed with | ||
| // this work for additional information regarding copyright | ||
| // ownership. Elasticsearch B.V. licenses this file to you under | ||
| // the Apache License, Version 2.0 (the "License"); you may | ||
| // not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| //go:generate mdatagen metadata.yaml | ||
|
|
||
| // Package httpexporter posts log record bodies as an HTTP request body. | ||
| // It is intended for load generation against raw HTTP ingest endpoints | ||
| // such as Managed Input drains (e.g. Vercel NDJSON), not OTLP. | ||
| package httpexporter // import "github.com/elastic/opentelemetry-collector-components/internal/exporter/httpexporter" |
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -0,0 +1,117 @@ | ||||
| // Licensed to Elasticsearch B.V. under one or more contributor | ||||
| // license agreements. See the NOTICE file distributed with | ||||
| // this work for additional information regarding copyright | ||||
| // ownership. Elasticsearch B.V. licenses this file to you under | ||||
| // the Apache License, Version 2.0 (the "License"); you may | ||||
| // not use this file except in compliance with the License. | ||||
| // You may obtain a copy of the License at | ||||
| // | ||||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||||
| // | ||||
| // Unless required by applicable law or agreed to in writing, | ||||
| // software distributed under the License is distributed on an | ||||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||||
| // KIND, either express or implied. See the License for the | ||||
| // specific language governing permissions and limitations | ||||
| // under the License. | ||||
|
|
||||
| package httpexporter // import "github.com/elastic/opentelemetry-collector-components/internal/exporter/httpexporter" | ||||
|
|
||||
| import ( | ||||
| "bytes" | ||||
| "context" | ||||
| "fmt" | ||||
| "io" | ||||
| "net/http" | ||||
| "strings" | ||||
|
|
||||
| "go.opentelemetry.io/collector/component" | ||||
| "go.opentelemetry.io/collector/exporter" | ||||
| "go.opentelemetry.io/collector/pdata/plog" | ||||
| "go.uber.org/zap" | ||||
| ) | ||||
|
|
||||
| const defaultContentType = "application/json" | ||||
|
|
||||
| type httpExporter struct { | ||||
| config *Config | ||||
| logger *zap.Logger | ||||
| settings component.TelemetrySettings | ||||
| httpClient *http.Client | ||||
| } | ||||
|
|
||||
| func newExporter(cfg *Config, set exporter.Settings) (*httpExporter, error) { | ||||
| if err := cfg.Validate(); err != nil { | ||||
| return nil, err | ||||
| } | ||||
| return &httpExporter{ | ||||
| config: cfg, | ||||
| logger: set.Logger, | ||||
| settings: set.TelemetrySettings, | ||||
| }, nil | ||||
| } | ||||
|
|
||||
| func (e *httpExporter) start(ctx context.Context, host component.Host) error { | ||||
| client, err := e.config.ToClient(ctx, host.GetExtensions(), e.settings) | ||||
| if err != nil { | ||||
| return fmt.Errorf("failed to create HTTP client: %w", err) | ||||
| } | ||||
| e.httpClient = client | ||||
| return nil | ||||
| } | ||||
|
|
||||
| func (e *httpExporter) pushLogs(ctx context.Context, ld plog.Logs) error { | ||||
| body := encodeLogBodies(ld) | ||||
| if len(body) == 0 { | ||||
| return nil | ||||
| } | ||||
|
|
||||
| req, err := http.NewRequestWithContext(ctx, http.MethodPost, e.config.Endpoint, bytes.NewReader(body)) | ||||
| if err != nil { | ||||
| return fmt.Errorf("failed to create request: %w", err) | ||||
| } | ||||
|
|
||||
| if req.Header.Get("Content-Type") == "" { | ||||
| req.Header.Set("Content-Type", defaultContentType) | ||||
| } | ||||
|
|
||||
| resp, err := e.httpClient.Do(req) | ||||
| if err != nil { | ||||
| return fmt.Errorf("failed to POST to %s: %w", e.config.Endpoint, err) | ||||
| } | ||||
| defer func() { | ||||
| // Drain body so connections can be reused. | ||||
| _, _ = io.Copy(io.Discard, resp.Body) | ||||
| if err := resp.Body.Close(); err != nil { | ||||
| e.logger.Warn("failed to close response body", zap.Error(err)) | ||||
| } | ||||
| }() | ||||
|
|
||||
| if resp.StatusCode < 200 || resp.StatusCode >= 300 { | ||||
|
gizas marked this conversation as resolved.
Outdated
|
||||
| return fmt.Errorf("POST %s returned status %d", e.config.Endpoint, resp.StatusCode) | ||||
| } | ||||
| return nil | ||||
| } | ||||
|
|
||||
| // encodeLogBodies joins each log record body as a line (NDJSON-friendly). | ||||
| func encodeLogBodies(ld plog.Logs) []byte { | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: return (buf bytes.Buffer, empty bool)?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||
| var b strings.Builder | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: better perf from bytes.Buffer?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fix bytes changes |
||||
| rls := ld.ResourceLogs() | ||||
| for i := 0; i < rls.Len(); i++ { | ||||
| sls := rls.At(i).ScopeLogs() | ||||
| for j := 0; j < sls.Len(); j++ { | ||||
| records := sls.At(j).LogRecords() | ||||
| for k := 0; k < records.Len(); k++ { | ||||
| line := records.At(k).Body().AsString() | ||||
| if line == "" { | ||||
| continue | ||||
| } | ||||
| if b.Len() > 0 { | ||||
| b.WriteByte('\n') | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No dont see anything. It works because we have the json.Decoder opentelemetry-collector-components/extension/vercelencodingextension/extension.go Line 202 in 2ec18e2
|
||||
| } | ||||
| b.WriteString(line) | ||||
| } | ||||
| } | ||||
| } | ||||
| return []byte(b.String()) | ||||
| } | ||||
Uh oh!
There was an error while loading. Please reload this page.