Skip to content

core: isAnyValue rejects objects with undefined values, dropping the whole attribute #7207

Closed

Description

@overbalance

What happened?

Since #6780 (in 3.0.0-rc.0), isAnyValue() in @opentelemetry/core rejects a plain object when any of its values is undefined. maybeAddAttribute() then drops the whole attribute and logs Invalid attribute value set for key: ....

The same function already skips undefined items inside arrays:

// Special case: `undefined` in an array is allowed because it has
// meaning (the array index of subsequent items). ...
if (item !== undefined && !isAnyValueInternal(item, visited)) {

There is no matching skip for object values, so [undefined] is valid but { a: undefined } is not. With the 2.x / 0.2xx SDK the same log records kept the attribute.

This hits code that builds structured attribute values with optional fields. In opentelemetry-js-contrib, @opentelemetry/instrumentation-openai builds gen_ai.input.messages parts like { type: 'text', content: captureMessageContent ? text : undefined }. With content capture off (the default), the whole gen_ai.input.messages / gen_ai.output.messages attribute disappears, not only the content.

Expected: an undefined object value is skipped (omitted from the stored value), the same as setAttribute(key, undefined) at the top level and undefined array items. A nested undefined should not invalidate the parent value.

OpenTelemetry Setup Code

import { logs, SeverityNumber } from '@opentelemetry/api';
import {
  LoggerProvider,
  InMemoryLogRecordExporter,
  SimpleLogRecordProcessor,
} from '@opentelemetry/sdk-logs';

const exporter = new InMemoryLogRecordExporter();
const provider = new LoggerProvider({
  processors: [new SimpleLogRecordProcessor({ exporter })],
});
logs.setGlobalLoggerProvider(provider);

logs.getLogger('repro').emit({
  severityNumber: SeverityNumber.INFO,
  attributes: {
    ok: [{ type: 'text' }],
    dropped: [{ type: 'text', content: undefined }],
  },
});

console.log(exporter.getFinishedLogRecords()[0].attributes);
// actual:   { ok: [ { type: 'text' } ] }
// expected: { ok: [ { type: 'text' } ], dropped: [ { type: 'text' } ] }

package.json

{
  "dependencies": {
    "@opentelemetry/api": "1.10.0-rc.0",
    "@opentelemetry/core": "3.0.0-rc.0",
    "@opentelemetry/sdk-logs": "3.0.0-rc.0"
  }
}

Relevant log output

Invalid attribute value set for key: dropped

Runtime and Version

Node.js v26.9.0

Activity

  1. added
    bug
    on Oct 14, 2026
  2. added
    priority:p2
    on Oct 14, 2026
  3. self-assigned this
    on Oct 14, 2026
  4. trentm commented on Oct 14, 2026

    @trentm
    Contributor

    Getting a little deeper looking at the culprit functions: isLogAttributeValue in sdk-logs v2 was more permissive than the current isAnyValue in core v3.

    In the 2.x version of sdk-logs:

    > var validation = require('@opentelemetry/sdk-logs/build/src/utils/validation')
    > validation.isLogAttributeValue({foo:'bar', a:undefined})
    true

    but in 3.x core:

    > var core = require('@opentelemetry/core')
    > core.isAnyValue({foo: 'bar', a: undefined})
    false

    Another possible consideration, beyond saying "yes, this is an accepted AnyValue", is whether we normalize that attribute object.
    I.e. whether the added attribute is {foo: 'bar', a: undefined} or {foo: 'bar'}.

    In sdk-logs v2 it is the former:

    > var attrs = {}
    >   const NO_ATTR_LIMITS = {
    |     attributeCountLimit: Infinity,
    |     attributeValueLengthLimit: Infinity,
    |   };
    > validation.addAttribute(attrs, NO_ATTR_LIMITS, 0, 'myKey', {foo: 'bar', a: undefined});
    2
    > validation.AddAttributeDecision[2]
    'ADD_NEW'
    > attrs
    { myKey: { foo: 'bar', a: undefined } }
    

    A possible issue with that is that when sent via OTLP, both undefined and null are represented the same -- as AnyValue {} -- which ends up as null on the other end.
    In other words, in sdk-logs v2 this logger.emit:

    logger.emit({
      severityNumber: SeverityNumber.INFO,
      body: 'hello',
      attributes: {
        myKey: { foo: 'bar', attrIsUndefined: undefined, attrIsNull: null },
      }
    });

    Looks like this via ConsoleLogRecordExporter:

      body: 'hello',
      attributes: {
        myKey: { foo: 'bar', attrIsUndefined: undefined, attrIsNull: null }
      }
    

    but like this via OTLP:

      body: 'hello',
      attributes: {
        myKey: {
          foo: 'bar',
          attrIsUndefined: null,
          attrIsNull: null
        }
    

    This hits code that builds structured attribute values with optional fields. In opentelemetry-js-contrib, @opentelemetry/instrumentation-openai builds gen_ai.input.messages parts like { type: 'text', content: captureMessageContent ? text : undefined }.
    [...]

    Expected: an undefined object value is skipped (omitted from the stored value), the same as setAttribute(key, undefined) at the top level and undefined array items. A nested undefined should not invalidate the parent value.

    Note that this is not what the current sdk-logs v2 is doing (as I showed above).
    I agree that the "builds structured attribute values with optional fields" is convenient.

  5. trentm commented on Oct 14, 2026

    @trentm
    Contributor

    My play/test script for use with v3 (i.e. logs in the api package):

    // play-attr-obj-undefined-prop.js
    const { diag, DiagConsoleLogger, DiagLogLevel } = require('@opentelemetry/api');
    const { logs, SeverityNumber } = require('@opentelemetry/api');
    const {
      LoggerProvider,
      SimpleLogRecordProcessor,
      ConsoleLogRecordExporter,
    } = require('@opentelemetry/sdk-logs');
    // Try different OTLP flavours.
    const { OTLPLogExporter } = require('@opentelemetry/exporter-logs-otlp-proto');
    // const { OTLPLogExporter } = require('@opentelemetry/exporter-logs-otlp-http');
    // const { OTLPLogExporter } = require('@opentelemetry/exporter-logs-otlp-grpc');
    
    diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.WARN);
    
    const loggerProvider = new LoggerProvider({
      processors: [
        new SimpleLogRecordProcessor({ exporter: new ConsoleLogRecordExporter() }),
        new SimpleLogRecordProcessor({ exporter: new OTLPLogExporter() }),
      ],
    });
    logs.setGlobalLoggerProvider(loggerProvider);
    process.once('beforeExit', async () => {
      await loggerProvider.shutdown();
    });
    
    const log = logs.getLogger('play');
    log.emit({
      severityNumber: SeverityNumber.INFO,
      body: 'hello',
      attributes: {
        aStr: 'bob',
        myKey: { foo: 'bar', attrIsUndefined: undefined, attrIsNull: null },
      }
    });
  6. trentm commented on Oct 14, 2026

    @trentm
    Contributor

    To get the same behaviour the functional (no comments, tests yet) diff is this:

    --- a/packages/opentelemetry-core/src/common/attributes.ts
    +++ b/packages/opentelemetry-core/src/common/attributes.ts
    @@ -323,8 +323,9 @@ function isAnyValueInternal(
       }
    
       // Plain objects
    -  for (const key of Object.keys(obj)) {
    -    if (!isAnyValueInternal(obj[key], visited)) {
    +  for (const k of Object.keys(obj)) {
    +    const v = obj[k];
    +    if (v !== undefined && !isAnyValueInternal(v, visited)) {
           return false;
         }
       }

    To get the Expected: an undefined object value is skipped behaviour is a bit more work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugpriority:p2

Type

No type

Fields

Priority

None yet

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions