Skip to content

Commit

Permalink
Add Decouple and Batch Processors to Collector (open-telemetry#959)
Browse files Browse the repository at this point in the history
* Add Decouple and Batch Processors

Add a new processor that decouples the receiver and exporter sides of the pipeline and is aware of lambda lifecycle events.

Also add the Batch processor to the list of available processor to reduce the cost of lambda function invocation at the expense of data being delayed.

* Fix missing go.sum entries

* Add link to lambda lifecycle

* Add additional comments to clarify lifecycle

* Update README.md

* Add Makefile to lambdalifecycle

* Fix race detector error

* Update dependencies
  • Loading branch information
adcharre authored Nov 2, 2023
1 parent f27bde2 commit 2027890
Show file tree
Hide file tree
Showing 22 changed files with 1,356 additions and 5 deletions.
7 changes: 7 additions & 0 deletions collector/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ go 1.20

replace github.com/open-telemetry/opentelemetry-lambda/collector/lambdacomponents => ./lambdacomponents

replace github.com/open-telemetry/opentelemetry-lambda/collector/lambdalifecycle => ./lambdalifecycle

replace github.com/open-telemetry/opentelemetry-lambda/collector/processor/coldstartprocessor => ./processor/coldstartprocessor

replace github.com/open-telemetry/opentelemetry-lambda/collector/processor/decoupleprocessor => ./processor/decoupleprocessor

replace github.com/open-telemetry/opentelemetry-lambda/collector/receiver/telemetryapireceiver => ./receiver/telemetryapireceiver

// fixes ambiguous import error: found package cloud.google.com/go/compute/metadata in multiple modules:
Expand All @@ -18,6 +22,7 @@ require (
github.com/golang-collections/go-datastructures v0.0.0-20150211160725-59788d5eb259
github.com/open-telemetry/opentelemetry-collector-contrib/confmap/provider/s3provider v0.88.0
github.com/open-telemetry/opentelemetry-lambda/collector/lambdacomponents v0.88.0
github.com/open-telemetry/opentelemetry-lambda/collector/lambdalifecycle v0.0.0-00010101000000-000000000000
github.com/stretchr/testify v1.8.4
go.opentelemetry.io/collector/component v0.88.0
go.opentelemetry.io/collector/confmap v0.88.0
Expand Down Expand Up @@ -94,6 +99,7 @@ require (
github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourceprocessor v0.88.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/processor/spanprocessor v0.88.0 // indirect
github.com/open-telemetry/opentelemetry-lambda/collector/processor/coldstartprocessor v0.88.0 // indirect
github.com/open-telemetry/opentelemetry-lambda/collector/processor/decoupleprocessor v0.0.0-00010101000000-000000000000 // indirect
github.com/open-telemetry/opentelemetry-lambda/collector/receiver/telemetryapireceiver v0.88.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
Expand Down Expand Up @@ -138,6 +144,7 @@ require (
go.opentelemetry.io/collector/featuregate v1.0.0-rcv0017 // indirect
go.opentelemetry.io/collector/pdata v1.0.0-rcv0017 // indirect
go.opentelemetry.io/collector/processor v0.88.0 // indirect
go.opentelemetry.io/collector/processor/batchprocessor v0.88.0 // indirect
go.opentelemetry.io/collector/processor/memorylimiterprocessor v0.88.0 // indirect
go.opentelemetry.io/collector/receiver v0.88.0 // indirect
go.opentelemetry.io/collector/receiver/otlpreceiver v0.88.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions collector/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,8 @@ go.opentelemetry.io/collector/pdata v1.0.0-rcv0017 h1:AgALhc2VenoA5l1DvTdg7mkzaB
go.opentelemetry.io/collector/pdata v1.0.0-rcv0017/go.mod h1:Rv9fOclA5AtM/JGm0d4jBOIAo1+jBA13UT5Bx0ovXi4=
go.opentelemetry.io/collector/processor v0.88.0 h1:5BUZaH+RhTpgTVqBZCrBnN/vl0M1CtwQsZ8ek4iH1lc=
go.opentelemetry.io/collector/processor v0.88.0/go.mod h1:2T5KxgBQxXuuyMu9dh+PIBxQ/geCFYcdnjmlWZx8o3E=
go.opentelemetry.io/collector/processor/batchprocessor v0.88.0 h1:KEifeRMC9JysHpVhQPEyD29C+gqhP0cHuFpJMJUbE/Y=
go.opentelemetry.io/collector/processor/batchprocessor v0.88.0/go.mod h1:SQhHxRcZ92/DLufTYzb4xnxnR/uuW5makoqezBlJgJ4=
go.opentelemetry.io/collector/processor/memorylimiterprocessor v0.88.0 h1:m37nR0I1F7ao3qAJtzHB4GwBr1qrtDaPKNfeBOYzDF0=
go.opentelemetry.io/collector/processor/memorylimiterprocessor v0.88.0/go.mod h1:p5+E4COkqfUPip3q2HFGuieBZbryI0h8KPP7jNYZvsY=
go.opentelemetry.io/collector/receiver v0.88.0 h1:MPvVAFOfjl0+Ylka7so8QoK8T2Za2471rv5t3sqbbSY=
Expand Down
40 changes: 35 additions & 5 deletions collector/internal/lifecycle/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package lifecycle
import (
"context"
"fmt"
"github.com/open-telemetry/opentelemetry-lambda/collector/lambdalifecycle"
"os"
"os/signal"
"path/filepath"
Expand All @@ -42,11 +43,12 @@ type collectorWrapper interface {
}

type manager struct {
logger *zap.Logger
collector collectorWrapper
extensionClient *extensionapi.Client
listener *telemetryapi.Listener
wg sync.WaitGroup
logger *zap.Logger
collector collectorWrapper
extensionClient *extensionapi.Client
listener *telemetryapi.Listener
wg sync.WaitGroup
lifecycleListeners []lambdalifecycle.Listener
}

func NewManager(ctx context.Context, logger *zap.Logger, version string) (context.Context, *manager) {
Expand Down Expand Up @@ -132,6 +134,7 @@ func (lm *manager) processEvents(ctx context.Context) error {
// Exit if we receive a SHUTDOWN event
if res.EventType == extensionapi.Shutdown {
lm.logger.Info("Received SHUTDOWN event")
lm.notifyEnvironmentShutdown()
lm.listener.Shutdown()
err = lm.collector.Stop()
if err != nil {
Expand All @@ -142,10 +145,37 @@ func (lm *manager) processEvents(ctx context.Context) error {
return err
}

lm.notifyFunctionInvoked()

err = lm.listener.Wait(ctx, res.RequestID)
if err != nil {
lm.logger.Error("problem waiting for platform.runtimeDone event", zap.Error(err), zap.String("requestID", res.RequestID))
}

// Check other components are ready before allowing the freezing of the environment.
lm.notifyFunctionFinished()
}
}
}

func (lm *manager) notifyFunctionInvoked() {
for _, listener := range lm.lifecycleListeners {
listener.FunctionInvoked()
}
}

func (lm *manager) notifyFunctionFinished() {
for _, listener := range lm.lifecycleListeners {
listener.FunctionFinished()
}
}

func (lm *manager) notifyEnvironmentShutdown() {
for _, listener := range lm.lifecycleListeners {
listener.EnvironmentShutdown()
}
}

func (lm *manager) AddListener(listener lambdalifecycle.Listener) {
lm.lifecycleListeners = append(lm.lifecycleListeners, listener)
}
4 changes: 4 additions & 0 deletions collector/lambdacomponents/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/probabilisticsamplerprocessor"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourceprocessor"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/spanprocessor"
"github.com/open-telemetry/opentelemetry-lambda/collector/processor/decoupleprocessor"
"go.opentelemetry.io/collector/exporter"
"go.opentelemetry.io/collector/exporter/loggingexporter"
"go.opentelemetry.io/collector/exporter/otlpexporter"
"go.opentelemetry.io/collector/exporter/otlphttpexporter"
"go.opentelemetry.io/collector/extension"
"go.opentelemetry.io/collector/otelcol"
"go.opentelemetry.io/collector/processor"
"go.opentelemetry.io/collector/processor/batchprocessor"
"go.opentelemetry.io/collector/processor/memorylimiterprocessor"
"go.opentelemetry.io/collector/receiver"
"go.opentelemetry.io/collector/receiver/otlpreceiver"
Expand Down Expand Up @@ -67,6 +69,8 @@ func Components(extensionID string) (otelcol.Factories, error) {
resourceprocessor.NewFactory(),
spanprocessor.NewFactory(),
coldstartprocessor.NewFactory(),
decoupleprocessor.NewFactory(),
batchprocessor.NewFactory(),
)
if err != nil {
errs = append(errs, err)
Expand Down
7 changes: 7 additions & 0 deletions collector/lambdacomponents/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourceprocessor v0.88.0
github.com/open-telemetry/opentelemetry-collector-contrib/processor/spanprocessor v0.88.0
github.com/open-telemetry/opentelemetry-lambda/collector/processor/coldstartprocessor v0.88.0
github.com/open-telemetry/opentelemetry-lambda/collector/processor/decoupleprocessor v0.0.0-00010101000000-000000000000
github.com/open-telemetry/opentelemetry-lambda/collector/receiver/telemetryapireceiver v0.88.0
go.opentelemetry.io/collector/exporter v0.88.0
go.opentelemetry.io/collector/exporter/loggingexporter v0.88.0
Expand All @@ -19,6 +20,7 @@ require (
go.opentelemetry.io/collector/extension v0.88.0
go.opentelemetry.io/collector/otelcol v0.88.0
go.opentelemetry.io/collector/processor v0.88.0
go.opentelemetry.io/collector/processor/batchprocessor v0.88.0
go.opentelemetry.io/collector/processor/memorylimiterprocessor v0.88.0
go.opentelemetry.io/collector/receiver v0.88.0
go.opentelemetry.io/collector/receiver/otlpreceiver v0.88.0
Expand Down Expand Up @@ -82,6 +84,7 @@ require (
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/prometheus v0.88.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/prometheusremotewrite v0.88.0 // indirect
github.com/open-telemetry/opentelemetry-lambda/collector v0.88.0 // indirect
github.com/open-telemetry/opentelemetry-lambda/collector/lambdalifecycle v0.0.0-00010101000000-000000000000 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/prometheus/client_golang v1.17.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
Expand Down Expand Up @@ -163,6 +166,10 @@ replace cloud.google.com/go => cloud.google.com/go v0.107.0

replace github.com/open-telemetry/opentelemetry-lambda/collector => ../

replace github.com/open-telemetry/opentelemetry-lambda/collector/lambdalifecycle => ../lambdalifecycle

replace github.com/open-telemetry/opentelemetry-lambda/collector/processor/coldstartprocessor => ../processor/coldstartprocessor

replace github.com/open-telemetry/opentelemetry-lambda/collector/processor/decoupleprocessor => ../processor/decoupleprocessor

replace github.com/open-telemetry/opentelemetry-lambda/collector/receiver/telemetryapireceiver => ../receiver/telemetryapireceiver
2 changes: 2 additions & 0 deletions collector/lambdacomponents/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,8 @@ go.opentelemetry.io/collector/pdata v1.0.0-rcv0017 h1:AgALhc2VenoA5l1DvTdg7mkzaB
go.opentelemetry.io/collector/pdata v1.0.0-rcv0017/go.mod h1:Rv9fOclA5AtM/JGm0d4jBOIAo1+jBA13UT5Bx0ovXi4=
go.opentelemetry.io/collector/processor v0.88.0 h1:5BUZaH+RhTpgTVqBZCrBnN/vl0M1CtwQsZ8ek4iH1lc=
go.opentelemetry.io/collector/processor v0.88.0/go.mod h1:2T5KxgBQxXuuyMu9dh+PIBxQ/geCFYcdnjmlWZx8o3E=
go.opentelemetry.io/collector/processor/batchprocessor v0.88.0 h1:KEifeRMC9JysHpVhQPEyD29C+gqhP0cHuFpJMJUbE/Y=
go.opentelemetry.io/collector/processor/batchprocessor v0.88.0/go.mod h1:SQhHxRcZ92/DLufTYzb4xnxnR/uuW5makoqezBlJgJ4=
go.opentelemetry.io/collector/processor/memorylimiterprocessor v0.88.0 h1:m37nR0I1F7ao3qAJtzHB4GwBr1qrtDaPKNfeBOYzDF0=
go.opentelemetry.io/collector/processor/memorylimiterprocessor v0.88.0/go.mod h1:p5+E4COkqfUPip3q2HFGuieBZbryI0h8KPP7jNYZvsY=
go.opentelemetry.io/collector/receiver v0.88.0 h1:MPvVAFOfjl0+Ylka7so8QoK8T2Za2471rv5t3sqbbSY=
Expand Down
1 change: 1 addition & 0 deletions collector/lambdalifecycle/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ../Makefile.Common
3 changes: 3 additions & 0 deletions collector/lambdalifecycle/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/open-telemetry/opentelemetry-lambda/collector/lambdalifecycle

go 1.20
43 changes: 43 additions & 0 deletions collector/lambdalifecycle/notifier.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright The OpenTelemetry Authors
//
// Licensed 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 lambdalifecycle

// Listener interface used to notify objects of Lambda lifecycle events.
type Listener interface {
// FunctionInvoked is called after the extension receives a "Next" notification.
FunctionInvoked()
// FunctionFinished is called after the extension is notified that the function has completed, but before the environment is frozen.
// The environment is only frozen once all listeners have returned.
FunctionFinished()
// EnvironmentShutdown is called when the extension is notified that the environment is about to shut down.
// Shutting down of the collector components only happens after all listeners have returned.
EnvironmentShutdown()
}

type Notifier interface {
AddListener(listener Listener)
}

var (
notifier Notifier
)

func SetNotifier(n Notifier) {
notifier = n
}

func GetNotifier() Notifier {
return notifier
}
4 changes: 4 additions & 0 deletions collector/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"flag"
"fmt"
"github.com/open-telemetry/opentelemetry-lambda/collector/lambdalifecycle"
"os"

"go.uber.org/zap"
Expand Down Expand Up @@ -47,6 +48,9 @@ func main() {

ctx, lm := lifecycle.NewManager(context.Background(), logger, Version)

// Set the new lifecycle manager as the lifecycle notifier for all other components.
lambdalifecycle.SetNotifier(lm)

// Will block until shutdown event is received or cancelled via the context.
logger.Info("done", zap.Error(lm.Run(ctx)))
}
Expand Down
1 change: 1 addition & 0 deletions collector/processor/decoupleprocessor/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ../../Makefile.Common
28 changes: 28 additions & 0 deletions collector/processor/decoupleprocessor/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Decouple Processor

| Status | |
| ------------------------ |-----------------------|
| Stability | [in development] |
| Supported pipeline types | traces, metrics, logs |
| Distributions | [extension] |

This processor decouples the receiver and exporter ends of the pipeline. This allows the lambda function to finish before traces/metrics/logs have been exported by the collector. The processor is aware of the Lambda [lifecycle] and will prevent the environment from being frozen or shutdown until any pending traces/metrics/logs have been exported.
In this way the response times of the Lambda function is not impacted by the need to export data, however the billed duration will include the time taken to export data as well as runtime of the lambda function.

The decouple processor should always be the last processor in the list to ensure that there are no issues with data being sent while the environment is about to be frozen, which could result in lost data.

When combined with the batch processor, the number of exports required can be significantly reduced and therefore the cost of running the lambda. This is with the trade-off that the data will not be available at your chosen endpoint until some time after the invocation, up to a maximum of 5 minutes (the timeout that the environment is shutdown when no further invocations are received).

## Processor Configuration

```yaml
processors:
decouple:
# max_queue_size allows you to control how many spans etc. are accepted before the pipeline blocks
# until an export has been completed. Default value is 200.
max_queue_size: 20
```
[in development]: https://github.com/open-telemetry/opentelemetry-collector#development
[extension]: https://github.com/open-telemetry/opentelemetry-lambda/collector
[lifecycle]: https://docs.aws.amazon.com/lambda/latest/dg/runtimes-extensions-api.html#runtimes-extensions-api-lifecycle
31 changes: 31 additions & 0 deletions collector/processor/decoupleprocessor/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright The OpenTelemetry Authors
//
// Licensed 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 decoupleprocessor // import "github.com/open-telemetry/opentelemetry-lambda/collector/processor/decoupleprocessor"
import "errors"

// Config defines the configuration for the various elements of the processor.
type Config struct {
MaxQueueSize uint32 `mapstructure:"max_queue_size"`
}

var invalidMaxQueueSizeError = errors.New("max_queue_size must be greater than 0")

// Validate validates the configuration by checking for missing or invalid fields
func (cfg *Config) Validate() error {
if cfg.MaxQueueSize == 0 {
return invalidMaxQueueSizeError
}
return nil
}
Loading

0 comments on commit 2027890

Please sign in to comment.