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

Attributes helper function - convert a deep object to an attribute compliant single-depth kv object #1445

Open
1 of 2 tasks
naseemkullah opened this issue Aug 18, 2020 · 4 comments

Comments

@naseemkullah
Copy link
Member

naseemkullah commented Aug 18, 2020

  • This only affects the JavaScript OpenTelemetry library
  • This may affect other libraries, but I would like to get opinions here first

tracer.getCurrentSpan()?.setAttributes({..anObject}); works well if the depth is 1, but if it is beyond that we will get [object Object] as an attribute value.

It would be nice to have a function to run on an object to get an atrribute friendly single-depth kv object.

tracer.getCurrentSpan()?.setAttributesFromObject({..anObject});

tl;dr

// take something like this
{ a: { b: 'c' } }
// turn it into this and add as attributes
{ 'a.b': 'c' }

edit: as @sergioregueira mentioned, https://github.com/hughsk/flat exists

@sergioregueira
Copy link
Contributor

According to the specification, the attribute value must be:

  • A primitive type: string, boolean, double precision floating point (IEEE 754-1985) or signed 64 bit integer.
  • An array of primitive type values. The array MUST be homogeneous, i.e. it MUST NOT contain values of different types. For protocols that do not natively support array values such values SHOULD be represented as JSON strings.

An object (flat or nested) should not be accepted, so I suggest to apply what it says about arrays and serialize it using JSON.stringify() (supported by both node and browser, so you do not need additional dependencies such as flat).

Nevertheless, who is responsible of serializing the value? The developer that sets it, the otel library that receives it or the exporter that propagates it?

@naseemkullah
Copy link
Member Author

Not saying an object would be the attribute, but rather a helper function could convert an object into as many valid attributes as there are nested key values in the object for optimum queryability. So basically flat built into the api and provided to both node and browser. This would avoid users having to minutiously transform objects to ensure the KVs are compliant and would provide an experience similar to pino's mergingObject, where you just shove the object in there and profit.

@pichlermarc
Copy link
Member

While I understand the frustration, I think adding such a helper is not in scope for the API package. 🤔

We'd be working around something that may be added to the spec in the future (see open-telemetry/opentelemetry-specification#2888), and if a future spec adds maps as attribute values, we'd also have to maintain our workaround for a long time.

@naseemkullah
Copy link
Member Author

Thanks @pichlermarc let's see how open-telemetry/opentelemetry-specification#2888 and open-telemetry/oteps#219 play out.

pichlermarc pushed a commit to dynatrace-oss-contrib/opentelemetry-js that referenced this issue Dec 15, 2023
* chore(mysql): remove mysql from dependencies

* fix: import type

---------

Co-authored-by: Amir Blum <amirgiraffe@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants