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

feat: Allow flattening of OTLP key-value lists #240

Merged
merged 2 commits into from
Feb 22, 2024

Conversation

MikeGoldsmith
Copy link
Contributor

@MikeGoldsmith MikeGoldsmith commented Feb 16, 2024

Which problem is this PR solving?

When translating OTLP kv lists we currently just encode as a JSON string which isn't particularly useful. This PR extends the extraction behaviour to traverse kv lists to generate multiple event fields. The max depth is set to 5 levels.

For example, this kv attribute:

key: "data"
value: {
  "key1": "val1",
  "key2": true,
}

Previously would be stored like this:

data: "{\"key1\": \"val1\","key2":true}"

After the change, the same kv list would be stored like this:

data.key1: "val1"
data.key2: true

This change exposes the AddAttributesToMap func for consumers to use directly. This will be useful until metrics translation can be moved into this library (eg from shepherd).

Short description of the changes

  • Update logic to extract OTLP data types to traverse kv lists instead of always encoding as JSON string, with a max depth of 5
  • Expose AddAttributesToMap as a public function so consumers can use directly
  • Update log translation to set the body field with a JSON string of the attribute to preserve backwards compatibility
  • Removes unused & broken truncation event fields

@MikeGoldsmith MikeGoldsmith added the type: enhancement New feature or request label Feb 16, 2024
@MikeGoldsmith MikeGoldsmith self-assigned this Feb 16, 2024
otlp/common.go Outdated Show resolved Hide resolved
otlp/common.go Outdated Show resolved Hide resolved
otlp/common.go Outdated Show resolved Hide resolved
otlp/common_test.go Show resolved Hide resolved
otlp/common.go Outdated Show resolved Hide resolved
otlp/common.go Outdated Show resolved Hide resolved
@MikeGoldsmith
Copy link
Contributor Author

@kentquirk @cartermp I've added an example options struct in 52c5529 to be passed into translate calls (trace and logs) that can control some of the behaviours you've pointed out as concerning (max depth, array & map flattening and body prefix).

I think this has has two big benefits:

  1. we can opt into the new behaviour by utilising launch darkly flags in Shepherd or expose via refinery config, defaulting to current behaviour then flip the switch when we want to
  2. paves the way to move metric translation into husky, as it requires customer level controls for some things

Note: this isn't complete, eg hasn't been added to public API calls yet, but shows the intention of the options

@kentquirk
Copy link
Contributor

I like the options pattern, but instead of passing around the options as an extra parameter on all these function calls, what do you think of the idea of making these functions be methods on a type that carries the options? It's not significantly more efficient from a performance standpoint but it neatly separates the function signatures and also helps organize the code a bit.

@MikeGoldsmith
Copy link
Contributor Author

I like the options pattern, but instead of passing around the options as an extra parameter on all these function calls, what do you think of the idea of making these functions be methods on a type that carries the options? It's not significantly more efficient from a performance standpoint but it neatly separates the function signatures and also helps organize the code a bit.

Interesting - so create a version of the translator with these options set. I'll try it out.

otlp/logs.go Outdated Show resolved Hide resolved
Copy link
Contributor

@TylerHelmuth TylerHelmuth left a comment

Choose a reason for hiding this comment

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

In case it is useful, here is the flatten function we added recently for OTTL: https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/pkg/ottl/ottlfuncs/func_flatten.go

@MikeGoldsmith MikeGoldsmith force-pushed the mike/flatten-otlp-arrays-maps branch from 75063a9 to a7bfd96 Compare February 21, 2024 19:19
@MikeGoldsmith MikeGoldsmith force-pushed the mike/flatten-otlp-arrays-maps branch from a7bfd96 to 92c8d7c Compare February 21, 2024 19:19
@MikeGoldsmith MikeGoldsmith changed the title feat: Flatten OTLP arrays and maps feat: Flatten OTLP kv lists Feb 21, 2024
@MikeGoldsmith MikeGoldsmith marked this pull request as ready for review February 21, 2024 19:37
@MikeGoldsmith MikeGoldsmith requested a review from a team as a code owner February 21, 2024 19:37
Copy link
Contributor

@kentquirk kentquirk left a comment

Choose a reason for hiding this comment

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

Back to basics! This seems correct to me.

Copy link
Contributor

@TylerHelmuth TylerHelmuth left a comment

Choose a reason for hiding this comment

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

I like this simplified version

Copy link
Member

@cartermp cartermp left a comment

Choose a reason for hiding this comment

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

Awesome work.

@MikeGoldsmith MikeGoldsmith changed the title feat: Flatten OTLP kv lists feat: Allow flattening of OTLP key-value lists Feb 22, 2024
@MikeGoldsmith MikeGoldsmith merged commit 4d3dc8e into main Feb 22, 2024
4 checks passed
@MikeGoldsmith MikeGoldsmith deleted the mike/flatten-otlp-arrays-maps branch February 22, 2024 09:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unpack and flatten array and key-value structures when feasible
4 participants