-
Notifications
You must be signed in to change notification settings - Fork 5k
Add otel-specific outputController to libbeat pipeline #50075
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| // 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 pipeline | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/elastic/beats/v7/libbeat/beat" | ||
| "github.com/elastic/beats/v7/libbeat/otel/otelconsumer" | ||
| "github.com/elastic/beats/v7/libbeat/outputs" | ||
| "github.com/elastic/beats/v7/libbeat/publisher" | ||
| "github.com/elastic/beats/v7/libbeat/publisher/queue" | ||
| "github.com/elastic/elastic-agent-libs/config" | ||
| "github.com/elastic/elastic-agent-libs/logp" | ||
| "github.com/elastic/elastic-agent-libs/monitoring" | ||
| "github.com/elastic/elastic-agent-libs/paths" | ||
| ) | ||
|
|
||
| type otelOutputController struct { | ||
| beatInfo beat.Info | ||
| logger *logp.Logger | ||
| monitors Monitors | ||
| queue queue.Queue[publisher.Event] | ||
|
|
||
| // consumer is a helper goroutine that reads event batches from the queue | ||
| // and sends them to workerChan for an output worker to process. | ||
| consumer *eventConsumer | ||
|
|
||
| // Each worker is a goroutine that will read batches from workerChan and | ||
| // send them to the output. | ||
| workers []outputWorker | ||
| workerChan chan publisher.Batch | ||
| } | ||
|
|
||
| func newOTelOutputController( | ||
| beatInfo beat.Info, | ||
| paths *paths.Path, | ||
| monitors Monitors, | ||
| retryObserver retryObserver, | ||
| queueFactory queue.QueueFactory[publisher.Event], | ||
| ) (*otelOutputController, error) { | ||
|
|
||
| // Queue metrics are reported under the pipeline namespace | ||
| var pipelineMetrics *monitoring.Registry | ||
| if monitors.Metrics != nil { | ||
| pipelineMetrics = monitors.Metrics.GetOrCreateRegistry("pipeline") | ||
| } | ||
| queueObserver := queue.NewQueueObserver(pipelineMetrics) | ||
|
|
||
| // Create the queue | ||
| queue, err := queueFactory(monitors.Logger, queueObserver, 0, nil) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("queue creation failed: %w", err) | ||
| } | ||
|
|
||
| // Initialize output group | ||
| out, err := loadOutput(monitors, func(outStats outputs.Observer) (string, outputs.Group, error) { | ||
| out, err := otelconsumer.MakeOtelConsumer(beatInfo, outStats, config.NewConfig(), paths) | ||
| return "otelconsumer", out, err | ||
| }) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| // Create output workers | ||
| workerChan := make(chan publisher.Batch) | ||
| workers := make([]outputWorker, len(out.Clients)) | ||
| logger := beatInfo.Logger.Named("otel_output_worker") | ||
| for i, client := range out.Clients { | ||
| workers[i] = makeClientWorker(workerChan, client, logger, monitors.Tracer) | ||
| } | ||
|
|
||
| // Create an event consumer pulling batches from the queue and sending | ||
| // them to the output worker channel. | ||
| consumer := newEventConsumer(monitors.Logger, retryObserver) | ||
| consumer.setTarget( | ||
| consumerTarget{ | ||
| queue: queue, | ||
| ch: workerChan, | ||
| batchSize: out.BatchSize, | ||
| timeToLive: out.Retry + 1, | ||
| }) | ||
|
|
||
| return &otelOutputController{ | ||
| beatInfo: beatInfo, | ||
| logger: beatInfo.Logger.Named("otelOutputController"), | ||
| monitors: monitors, | ||
| queue: queue, | ||
| consumer: consumer, | ||
| workers: workers, | ||
| workerChan: workerChan, | ||
| }, nil | ||
| } | ||
|
|
||
| func (c *otelOutputController) waitClose(ctx context.Context, _ bool) error { | ||
| // First: signal the queue that we're shutting down, and allow it to drain | ||
| // and process ACKs until the given context terminates. | ||
| c.logger.Infof("Output shutdown started. Waiting for enqueued events to be published.") | ||
| c.queue.Close(false) | ||
| select { | ||
| case <-c.queue.Done(): | ||
| c.logger.Infof("Continue shutdown: All enqueued events have been published.") | ||
| case <-ctx.Done(): | ||
| c.logger.Infof("Continue shutdown: Time out waiting for events to be published.") | ||
| c.queue.Close(true) | ||
| <-c.queue.Done() | ||
| } | ||
|
|
||
| // We've drained the queue as much as we can, signal eventConsumer to | ||
| // close, and wait for it to finish. After consumer.close returns, | ||
| // there will be no more writes to c.workerChan, so it is safe to close. | ||
| c.consumer.close() | ||
| close(c.workerChan) | ||
|
|
||
| // Signal the output workers to close. | ||
| for _, out := range c.workers { | ||
| out.Close() | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (c *otelOutputController) queueProducer(config queue.ProducerConfig) queue.Producer[publisher.Event] { | ||
| return c.queue.Producer(config) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| // 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 pipeline | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/elastic/beats/v7/libbeat/beat" | ||
| "github.com/elastic/beats/v7/libbeat/publisher" | ||
| "github.com/elastic/beats/v7/libbeat/publisher/queue/memqueue" | ||
| "github.com/elastic/elastic-agent-libs/logp/logptest" | ||
| "github.com/elastic/elastic-agent-libs/monitoring" | ||
| ) | ||
|
|
||
| func TestOTelQueueMetrics(t *testing.T) { | ||
| // More thorough testing of queue metrics are in the queue package, | ||
| // here we just want to make sure that they appear under the right | ||
| // monitoring namespace. | ||
| reg := monitoring.NewRegistry() | ||
| logger := logptest.NewTestingLogger(t, "") | ||
| controller, err := newOTelOutputController( | ||
| beat.Info{Logger: logger}, | ||
| nil, | ||
| Monitors{ | ||
| Logger: logger, | ||
| Metrics: reg, | ||
| }, | ||
| nilObserver, | ||
| memqueue.FactoryForSettings[publisher.Event](memqueue.Settings{Events: 1000})) | ||
| require.NoError(t, err, "creating OTel output controller should succeed") | ||
| defer controller.waitClose(context.Background(), true) | ||
| entry := reg.Get("pipeline.queue.max_events") | ||
| require.NotNil(t, entry, "pipeline.queue.max_events must exist") | ||
| value, ok := entry.(*monitoring.Uint) | ||
| require.True(t, ok, "pipeline.queue.max_events must be a *monitoring.Uint") | ||
| assert.Equal(t, uint64(1000), value.Get(), "pipeline.queue.max_events should match the events configuration key") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.