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

Cannot read properties of undefined. Reading OTEL_SDK_DISABLED #3613

Closed
ravindra-dyte opened this issue Feb 14, 2023 · 7 comments · Fixed by #3617
Closed

Cannot read properties of undefined. Reading OTEL_SDK_DISABLED #3613

ravindra-dyte opened this issue Feb 14, 2023 · 7 comments · Fixed by #3617
Assignees
Labels
bug Something isn't working priority:p1 Bugs which cause problems in end-user applications such as crashes, data inconsistencies, etc

Comments

@ravindra-dyte
Copy link
Contributor

ravindra-dyte commented Feb 14, 2023

What happened?

Users integrating our SDK (that internally uses Opentelemetry SDK) fails to initialize with the following error

Cannot read properties of undefined. Reading OTEL_SDK_DISABLED

Steps to Reproduce

We are an SDK company, and we have integrated Opentelemetry in our SDK. Clients of ours who are having bundler configs, such as webpack config, where node integration's globals are set to false (process in this case), not undefined but false, face this issue.

image

This is happening because

return typeof process !== 'undefined'

checks for undefined only whereas the value is false for them therefore it passes the check but fails afterward when it tries to pick environment variables from process.env. Because the process is false, process.env would be undefined and any key retrieval on process.env would throw this above error.

Expected Result

The default configuration should be picked.
A falsy check should be there to ensure that the process.env is also not falsy.

For example, we could replace,

return typeof process !== 'undefined'
        ? parseEnvironment(process.env)
        : parseEnvironment(globalThis_1._globalThis);

with

return typeof process !== 'undefined' && process && process.env
        ? parseEnvironment(process.env)
        : parseEnvironment(globalThis_1._globalThis);

to not allow null, undefine, or any other falsy value.

Or we can remove node globals from browser-based SDKs.

Actual Result

It fails, treats false as an object, and fails with an error.

Additional Details

We could ask our clients to make the change in their bundler configs, but it is less than ideal. Also, it gives a poor first impression.

OpenTelemetry Setup Code

import { WebTracerProvider, StackContextManager } from '@opentelemetry/sdk-trace-web';
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';
import { BatchSpanProcessor, Tracer } from '@opentelemetry/sdk-trace-base';
import { ZoneContextManager } from '@opentelemetry/context-zone-peer-dep';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import opentelemetry, {
    Span, ROOT_CONTEXT, PropagationAPI, propagation,
} from '@opentelemetry/api';
import { Resource } from '@opentelemetry/resources';        

const provider = new WebTracerProvider({ // fails here itself
    resource: new Resource({
      'service.name': 'our-service-name'
    }),
});
// Not adding the remaining code as might not be relevant

package.json

{
  "dependencies": {
    "@opentelemetry/api": "^1.4.0",
    "@opentelemetry/context-zone-peer-dep": "^1.9.0",
    "@opentelemetry/exporter-trace-otlp-http": "^0.35.0",
    "@opentelemetry/resources": "^1.9.0",
    "@opentelemetry/sdk-trace-base": "^1.9.0",
    "@opentelemetry/sdk-trace-web": "^1.9.0",
    "@opentelemetry/semantic-conventions": "^1.9.0",
  }
}

Relevant log output

No response

@ravindra-dyte ravindra-dyte added bug Something isn't working triage labels Feb 14, 2023
@Flarna
Copy link
Member

Flarna commented Feb 15, 2023

Why do they set process to false. I think including null to the check makes sense but any falsy value? I assume this just hides a problem which likely pops up at other places then.

What if someone sets process to a string not available?

@ravindra-dyte
Copy link
Contributor Author

ravindra-dyte commented Feb 15, 2023

Devs, when they face issues, flock to stack overflow, and end up on threads like https://stackoverflow.com/questions/64557638/how-to-polyfill-node-core-modules-in-webpack-5 where the most voted answer tells them to set it as false.

What if someone sets process to a string not available?

My suggested extra cautious null check would help in sorting this out. I would recommend using _.get method from lodash for the same instead of the below snippet.

return typeof process !== 'undefined' && process && process.env
        ? parseEnvironment(process.env)
        : parseEnvironment(globalThis_1._globalThis);

typeof process would be string. process would be this string and process.env would be undefined therefore not breaking the usual flow. If someone now puts process.env to true/random-stuff, then, anyway, not much can be done for such folks, apart from educating them about it.

What is your opinion on this?

@ravindra-dyte
Copy link
Contributor Author

For production-grade solution, I would recommend using _.get from lodash.
image

@Flarna
Copy link
Member

Flarna commented Feb 15, 2023

We tend to avoid dependencies to 3rd parties even to popular ones like lodash if possible.

If a falsy check solves problems I would vote for adding it. It's Javascript world here so types are not that strict.

Could you create a PR for this?

@ravindra-dyte
Copy link
Contributor Author

Sure. Would raise it, this week.

@pichlermarc
Copy link
Member

@ravindra-dyte would it be okay if I assign the issue to you? 🙂

@pichlermarc pichlermarc added priority:p1 Bugs which cause problems in end-user applications such as crashes, data inconsistencies, etc and removed triage labels Feb 15, 2023
@ravindra-dyte
Copy link
Contributor Author

Sure @pichlermarc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working priority:p1 Bugs which cause problems in end-user applications such as crashes, data inconsistencies, etc
Projects
None yet
3 participants