Skip to content

[exporter/elasticsearch] fix panic when encoding scope attributes #40098

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

Conversation

AndersonQ
Copy link
Contributor

Description

Previously, the code attempted to cast scope attribute values to strings, leading to a panic when incompatible types were encountered.

When mapping scope to attributes, instead of trying to cast them to string now it just creates a new Map for the attributes and copies the cope entries to the new map. This avoids any cast and preserves the original types.

Link to tracking issue

Fixes #37701

Testing

Run the exporter with the config that originally causes the panic, see config below.
Running it on main will panic, running it on this PR will not panic.

./bin/otelcontribcol_linux_amd64 --config otel-test.yaml
receivers:
  hostmetrics:
    metadata_collection_interval: 1s
    scrapers:
      cpu: {}

exporters:
  elasticsearch:
    mapping:
      mode: none
    endpoint: <redacted>
    user: <redacted>
    password: <redacted>
    flush:
      interval: 1s

service:
  pipelines:
    logs:
      receivers: [hostmetrics]
      processors: []
      exporters: [elasticsearch]

Documentation

N/A

@AndersonQ AndersonQ requested a review from a team as a code owner May 15, 2025 17:40
@AndersonQ AndersonQ requested a review from ArthurSens May 15, 2025 17:40
@AndersonQ AndersonQ force-pushed the 37701-fix-es-exporter-scope-attr-panic branch 3 times, most recently from 6fd6d65 to 85b7f41 Compare May 15, 2025 18:16
Copy link
Contributor

@carsonip carsonip left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, code lgtm. One issue with the changelog

@AndersonQ AndersonQ force-pushed the 37701-fix-es-exporter-scope-attr-panic branch from 85b7f41 to 0b2be72 Compare May 15, 2025 18:35
@AndersonQ AndersonQ force-pushed the 37701-fix-es-exporter-scope-attr-panic branch from 0b2be72 to 56ef63b Compare May 15, 2025 18:37
Copy link
Member

@andrzej-stencel andrzej-stencel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to merge after a code owner reviews this.

@@ -449,11 +449,12 @@ func durationAsMicroseconds(start, end time.Time) int64 {

func scopeToAttributes(scope pcommon.InstrumentationScope) pcommon.Map {
attrs := pcommon.NewMap()

scope.Attributes().CopyTo(attrs)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This reordering has the consequence that if a scope attribute named "name" or "version" exists, it will be overridden by scope name or version. I believe in general this is the right thing to do, with the exception of when the scope name or version is empty.

I'd like to see a follow-up change to only set scope name and version as attributes if they aren't empty.

Copy link
Member

@lahsivjar lahsivjar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks for the contribution!

@andrzej-stencel andrzej-stencel merged commit e839598 into open-telemetry:main May 19, 2025
175 checks passed
@github-actions github-actions bot added this to the next release milestone May 19, 2025
dragonlord93 pushed a commit to dragonlord93/opentelemetry-collector-contrib that referenced this pull request May 23, 2025
…en-telemetry#40098)

<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->
#### Description

Previously, the code attempted to cast scope attribute values to
strings, leading to a panic when incompatible types were encountered.

When mapping scope to attributes, instead of trying to cast them to
string now it just creates a new Map for the attributes and copies the
cope entries to the new map. This avoids any cast and preserves the
original types.

<!-- Issue number (e.g. open-telemetry#1234) or full URL to issue, if applicable. -->
#### Link to tracking issue
Fixes open-telemetry#37701

<!--Describe what testing was performed and which tests were added.-->
#### Testing
Run the exporter with the config that originally causes the panic, see
config below.
Running it on main will panic, running it on this PR will not panic.

```
./bin/otelcontribcol_linux_amd64 --config otel-test.yaml
```

```
receivers:
  hostmetrics:
    metadata_collection_interval: 1s
    scrapers:
      cpu: {}

exporters:
  elasticsearch:
    mapping:
      mode: none
    endpoint: <redacted>
    user: <redacted>
    password: <redacted>
    flush:
      interval: 1s

service:
  pipelines:
    logs:
      receivers: [hostmetrics]
      processors: []
      exporters: [elasticsearch]

```

<!--Describe the documentation added.-->
#### Documentation

N/A
<!--Please delete paragraphs that you did not use before submitting.-->
dd-jasminesun pushed a commit to DataDog/opentelemetry-collector-contrib that referenced this pull request Jun 23, 2025
…en-telemetry#40098)

<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->
#### Description

Previously, the code attempted to cast scope attribute values to
strings, leading to a panic when incompatible types were encountered.

When mapping scope to attributes, instead of trying to cast them to
string now it just creates a new Map for the attributes and copies the
cope entries to the new map. This avoids any cast and preserves the
original types.

<!-- Issue number (e.g. #1234) or full URL to issue, if applicable. -->
#### Link to tracking issue
Fixes open-telemetry#37701

<!--Describe what testing was performed and which tests were added.-->
#### Testing
Run the exporter with the config that originally causes the panic, see
config below.
Running it on main will panic, running it on this PR will not panic.

```
./bin/otelcontribcol_linux_amd64 --config otel-test.yaml
```

```
receivers:
  hostmetrics:
    metadata_collection_interval: 1s
    scrapers:
      cpu: {}

exporters:
  elasticsearch:
    mapping:
      mode: none
    endpoint: <redacted>
    user: <redacted>
    password: <redacted>
    flush:
      interval: 1s

service:
  pipelines:
    logs:
      receivers: [hostmetrics]
      processors: []
      exporters: [elasticsearch]

```

<!--Describe the documentation added.-->
#### Documentation

N/A
<!--Please delete paragraphs that you did not use before submitting.-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[elasticsearchexporter] exporter panics when encoding non-string value scope attributes
5 participants