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

[processor/resourcedetection] introduce retry mechanism for detectors #37506

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
27 changes: 27 additions & 0 deletions .chloggen/docker-resource-failure.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# 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: processor/resourcedetection

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: "Introduce retry logic for failed resource detection."

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

# (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:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
13 changes: 12 additions & 1 deletion processor/resourcedetectionprocessor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ resourcedetection:

## Ordering

Note that if multiple detectors are inserting the same attribute name, the first detector to insert wins. For example if you had `detectors: [eks, ec2]` then `cloud.platform` will be `aws_eks` instead of `ec2`. The below ordering is recommended.
By default, if multiple detectors are inserting the same attribute name, the first detector to insert wins. For example if you had `detectors: [eks, ec2]` then `cloud.platform` will be `aws_eks` instead of `ec2`. The below ordering is recommended.

### AWS

Expand All @@ -627,3 +627,14 @@ Note that if multiple detectors are inserting the same attribute name, the first

The full list of settings exposed for this extension are documented in [config.go](./config.go)
with detailed sample configurations in [testdata/config.yaml](./testdata/config.yaml).

**Note:**

If you want to disable the ordering of the detectors and instead have a non-blocking resource detection in case of a detection failure, set the `order` parameter to `false`. For example:

```yaml
processors:
resourcedetection:
detectors: [docker]
order: false
```
3 changes: 3 additions & 0 deletions processor/resourcedetectionprocessor/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ type Config struct {
// If a supplied attribute is not a valid attribute of a supplied detector it will be ignored.
// Deprecated: Please use detector's resource_attributes config instead
Attributes []string `mapstructure:"attributes"`
// Order is a parameter which perserves the order of detectors and the detection of data
// This also introduces a blocking behavior, if one of the detectors cannot detect the resource
Order bool `mapstructure:"order"`
}

// DetectorConfig contains user-specified configurations unique to all individual detectors
Expand Down
7 changes: 7 additions & 0 deletions processor/resourcedetectionprocessor/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func TestLoadConfig(t *testing.T) {
DetectorConfig: openshiftConfig,
ClientConfig: cfg,
Override: false,
Order: true,
},
},
{
Expand All @@ -82,6 +83,7 @@ func TestLoadConfig(t *testing.T) {
ClientConfig: cfg,
Override: false,
DetectorConfig: detectorCreateDefaultConfig(),
Order: true,
},
},
{
Expand All @@ -91,6 +93,7 @@ func TestLoadConfig(t *testing.T) {
DetectorConfig: ec2Config,
ClientConfig: cfg,
Override: false,
Order: true,
},
},
{
Expand All @@ -101,6 +104,7 @@ func TestLoadConfig(t *testing.T) {
ClientConfig: cfg,
Override: false,
Attributes: []string{"a", "b"},
Order: true,
},
},
{
Expand All @@ -110,6 +114,7 @@ func TestLoadConfig(t *testing.T) {
ClientConfig: cfg,
Override: false,
DetectorConfig: detectorCreateDefaultConfig(),
Order: true,
},
},
{
Expand All @@ -119,6 +124,7 @@ func TestLoadConfig(t *testing.T) {
ClientConfig: cfg,
Override: false,
DetectorConfig: detectorCreateDefaultConfig(),
Order: true,
},
},
{
Expand All @@ -128,6 +134,7 @@ func TestLoadConfig(t *testing.T) {
ClientConfig: cfg,
Override: false,
DetectorConfig: resourceAttributesConfig,
Order: true,
},
},
{
Expand Down
6 changes: 4 additions & 2 deletions processor/resourcedetectionprocessor/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func createDefaultConfig() component.Config {
Override: true,
Attributes: nil,
DetectorConfig: detectorCreateDefaultConfig(),
Order: true,
// TODO: Once issue(https://github.com/open-telemetry/opentelemetry-collector/issues/4001) gets resolved,
// Set the default value of 'hostname_source' here instead of 'system' detector
}
Expand Down Expand Up @@ -199,7 +200,7 @@ func (f *factory) getResourceDetectionProcessor(
if oCfg.Attributes != nil {
params.Logger.Warn("You are using deprecated `attributes` option that will be removed soon; use `resource_attributes` instead, details on configuration: https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/processor/resourcedetectionprocessor#migration-from-attributes-to-resource_attributes")
}
provider, err := f.getResourceProvider(params, oCfg.ClientConfig.Timeout, oCfg.Detectors, oCfg.DetectorConfig, oCfg.Attributes)
provider, err := f.getResourceProvider(params, oCfg.ClientConfig.Timeout, oCfg.Detectors, oCfg.DetectorConfig, oCfg.Attributes, oCfg.Order)
if err != nil {
return nil, err
}
Expand All @@ -218,6 +219,7 @@ func (f *factory) getResourceProvider(
configuredDetectors []string,
detectorConfigs DetectorConfig,
attributes []string,
order bool,
) (*internal.ResourceProvider, error) {
f.lock.Lock()
defer f.lock.Unlock()
Expand All @@ -231,7 +233,7 @@ func (f *factory) getResourceProvider(
detectorTypes = append(detectorTypes, internal.DetectorType(strings.TrimSpace(key)))
}

provider, err := f.resourceProviderFactory.CreateResourceProvider(params, timeout, attributes, &detectorConfigs, detectorTypes...)
provider, err := f.resourceProviderFactory.CreateResourceProvider(params, timeout, attributes, &detectorConfigs, order, detectorTypes...)
if err != nil {
return nil, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func (f *ResourceProviderFactory) CreateResourceProvider(
timeout time.Duration,
attributes []string,
detectorConfigs ResourceDetectorConfig,
order bool,
detectorTypes ...DetectorType,
) (*ResourceProvider, error) {
detectors, err := f.getDetectors(params, detectorConfigs, detectorTypes)
Expand All @@ -59,7 +60,7 @@ func (f *ResourceProviderFactory) CreateResourceProvider(
}
}

provider := NewResourceProvider(params.Logger, timeout, attributesToKeep, detectors...)
provider := NewResourceProvider(params.Logger, timeout, attributesToKeep, order, detectors...)
return provider, nil
}

Expand Down Expand Up @@ -89,6 +90,7 @@ type ResourceProvider struct {
detectedResource *resourceResult
once sync.Once
attributesToKeep map[string]struct{}
order bool
}

type resourceResult struct {
Expand All @@ -97,26 +99,39 @@ type resourceResult struct {
err error
}

func NewResourceProvider(logger *zap.Logger, timeout time.Duration, attributesToKeep map[string]struct{}, detectors ...Detector) *ResourceProvider {
func NewResourceProvider(logger *zap.Logger, timeout time.Duration, attributesToKeep map[string]struct{}, order bool, detectors ...Detector) *ResourceProvider {
return &ResourceProvider{
logger: logger,
timeout: timeout,
detectors: detectors,
attributesToKeep: attributesToKeep,
order: order,
}
}

func (p *ResourceProvider) Get(ctx context.Context, client *http.Client) (resource pcommon.Resource, schemaURL string, err error) {
func (p *ResourceProvider) Get(ctx context.Context, _ *http.Client) (resource pcommon.Resource, schemaURL string, err error) {
p.once.Do(func() {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, client.Timeout)
defer cancel()
p.detectResource(ctx)
})

return p.detectedResource.resource, p.detectedResource.schemaURL, p.detectedResource.err
}

type detectResult struct {
r pcommon.Resource
schemaURL string
err error
}

func handleResult(res *pcommon.Resource, resultsChan chan detectResult, mergedSchemaURL string) string {
result := <-resultsChan
if result.err == nil {
mergedSchemaURL = MergeSchemaURL(mergedSchemaURL, result.schemaURL)
MergeResource(*res, result.r, false)
}
return mergedSchemaURL
}

func (p *ResourceProvider) detectResource(ctx context.Context) {
p.detectedResource = &resourceResult{}

Expand All @@ -125,13 +140,29 @@ func (p *ResourceProvider) detectResource(ctx context.Context) {

p.logger.Info("began detecting resource information")

resultsChan := make(chan detectResult, len(p.detectors))
for _, detector := range p.detectors {
r, schemaURL, err := detector.Detect(ctx)
if err != nil {
p.logger.Warn("failed to detect resource", zap.Error(err))
} else {
mergedSchemaURL = MergeSchemaURL(mergedSchemaURL, schemaURL)
MergeResource(res, r, false)
go func(detector Detector) {
sleep := 2 * time.Second
for {
r, schemaURL, err := detector.Detect(ctx)
if err != nil {
p.logger.Warn("failed to detect resource", zap.Error(err))
time.Sleep(sleep)
sleep *= 2
} else {
resultsChan <- detectResult{r: r, schemaURL: schemaURL, err: nil}
return
}
}
}(detector)
if p.order {
mergedSchemaURL = handleResult(&res, resultsChan, mergedSchemaURL)
}
}
if !p.order {
for range p.detectors {
mergedSchemaURL = handleResult(&res, resultsChan, mergedSchemaURL)
}
}

Expand Down
Loading
Loading