Skip to content

Commit

Permalink
[exporter/datadog] Run source providers in parallel (open-telemetry#2…
Browse files Browse the repository at this point in the history
…4234)

Make Datadog exporter source providers run in parallel to reduce start
times. With the new `Chain` implementation, we start checking all
sources in parallel instead of waiting for the previous one to fail.
This makes the Datadog exporter call all cloud provider endpoints in all
cloud providers, so it may increase spurious logs such as those reported
in open-telemetry#24072.

**Link to tracking Issue:** Updates open-telemetry#16442 (at least it should
substantially improve start time in some environments)

---------

Co-authored-by: Yang Song <[email protected]>
Co-authored-by: Alex Boten <[email protected]>
  • Loading branch information
3 people committed Jul 12, 2023
1 parent f88e0d6 commit 7f36419
Show file tree
Hide file tree
Showing 3 changed files with 192 additions and 7 deletions.
21 changes: 21 additions & 0 deletions .chloggen/mx-psi_parallel-source-resolution.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Use this changelog template to create an entry for release notes.
# If your change doesn't affect end users, such as a test fix or a tooling change,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: datadogexporter

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: "Source resolution logic now runs all source providers in parallel to improve start times."

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [24234]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext: |
All source providers now run in all environments so you may see more spurious logs from downstream dependencies when using the Datadog exporter. These logs should be safe to ignore.
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,47 @@ type chainProvider struct {
}

func (p *chainProvider) Source(ctx context.Context) (source.Source, error) {
for _, source := range p.priorityList {
zapProvider := zap.String("provider", source)
// Auxiliary type for storing source provider replies
type reply struct {
src source.Source
err error
}

// Cancel all providers when exiting
ctx, cancel := context.WithCancel(ctx)
defer cancel()

// Run all providers in parallel
replies := make([]chan reply, len(p.priorityList))
for i, source := range p.priorityList {
provider := p.providers[source]
src, err := provider.Source(ctx)
if err == nil {
p.logger.Info("Resolved source", zapProvider, zap.Any("source", src))
return src, nil
replies[i] = make(chan reply)

go func(i int, source string) {
zapProvider := zap.String("provider", source)
p.logger.Debug("Trying out source provider", zapProvider)

src, err := provider.Source(ctx)
if err != nil {
p.logger.Debug("Unavailable source provider", zapProvider, zap.Error(err))
}

replies[i] <- reply{src: src, err: err}
}(i, source)
}

// Check provider responses in order to ensure priority
for i, ch := range replies {
reply := <-ch
if reply.err != nil {
// Provider was unavailable, error was logged on goroutine
continue
}
p.logger.Debug("Unavailable source provider", zapProvider, zap.Error(err))

p.logger.Info("Resolved source",
zap.String("provider", p.priorityList[i]), zap.Any("source", reply.src),
)
return reply.src, nil
}

return source.Source{}, fmt.Errorf("no source provider was available")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package provider // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/hostmetadata/provider"

import (
"context"
"errors"
"testing"
"time"

"github.com/DataDog/opentelemetry-mapping-go/pkg/otlp/attributes/source"
"github.com/stretchr/testify/assert"
"go.uber.org/zap/zaptest"
)

var _ source.Provider = (*HostProvider)(nil)

type HostProvider string

func (p HostProvider) Source(context.Context) (source.Source, error) {
return source.Source{Kind: source.HostnameKind, Identifier: string(p)}, nil
}

var _ source.Provider = (*ErrorSourceProvider)(nil)

type ErrorSourceProvider string

func (p ErrorSourceProvider) Source(context.Context) (source.Source, error) {
return source.Source{}, errors.New(string(p))
}

var _ source.Provider = (*delayedProvider)(nil)

type delayedProvider struct {
provider source.Provider
delay time.Duration
}

func (p *delayedProvider) Source(ctx context.Context) (source.Source, error) {
time.Sleep(p.delay)
return p.provider.Source(ctx)
}

func withDelay(provider source.Provider, delay time.Duration) source.Provider {
return &delayedProvider{provider, delay}
}

func TestChain(t *testing.T) {
tests := []struct {
name string
providers map[string]source.Provider
priorityList []string

buildErr string

hostname string
queryErr string
}{
{
name: "missing provider in priority list",
providers: map[string]source.Provider{
"p1": HostProvider("p1SourceName"),
"p2": ErrorSourceProvider("errP2"),
},
priorityList: []string{"p1", "p2", "p3"},

buildErr: "\"p3\" source is not available in providers",
},
{
name: "all providers fail",
providers: map[string]source.Provider{
"p1": ErrorSourceProvider("errP1"),
"p2": ErrorSourceProvider("errP2"),
"p3": HostProvider("p3SourceName"),
},
priorityList: []string{"p1", "p2"},

queryErr: "no source provider was available",
},
{
name: "no providers fail",
providers: map[string]source.Provider{
"p1": HostProvider("p1SourceName"),
"p2": HostProvider("p2SourceName"),
"p3": HostProvider("p3SourceName"),
},
priorityList: []string{"p1", "p2", "p3"},

hostname: "p1SourceName",
},
{
name: "some providers fail",
providers: map[string]source.Provider{
"p1": ErrorSourceProvider("p1Err"),
"p2": HostProvider("p2SourceName"),
"p3": ErrorSourceProvider("p3Err"),
},
priorityList: []string{"p1", "p2", "p3"},

hostname: "p2SourceName",
},
{
name: "p2 takes longer than p3",
providers: map[string]source.Provider{
"p1": ErrorSourceProvider("p1Err"),
"p2": withDelay(HostProvider("p2SourceName"), 50*time.Millisecond),
"p3": HostProvider("p3SourceName"),
},
priorityList: []string{"p1", "p2", "p3"},

hostname: "p2SourceName",
},
}

for _, testInstance := range tests {
t.Run(testInstance.name, func(t *testing.T) {
provider, err := Chain(zaptest.NewLogger(t), testInstance.providers, testInstance.priorityList)
if err != nil || testInstance.buildErr != "" {
assert.EqualError(t, err, testInstance.buildErr)
return
}

src, err := provider.Source(context.Background())
if err != nil || testInstance.queryErr != "" {
assert.EqualError(t, err, testInstance.queryErr)
} else {
assert.Equal(t, testInstance.hostname, src.Identifier)
}
})
}
}

0 comments on commit 7f36419

Please sign in to comment.