-
Notifications
You must be signed in to change notification settings - Fork 828
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: add OTEL_LOG_LEVEL env var #974
Changes from 3 commits
33f7a04
8b313d5
03bc84d
9934e58
878dc39
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,7 +28,6 @@ export interface ENVIRONMENT { | |
} | ||
|
||
const ENVIRONMENT_NUMBERS: Partial<keyof ENVIRONMENT>[] = [ | ||
'OTEL_LOG_LEVEL', | ||
'OTEL_SAMPLING_PROBABILITY', | ||
]; | ||
|
||
|
@@ -37,7 +36,7 @@ const ENVIRONMENT_NUMBERS: Partial<keyof ENVIRONMENT>[] = [ | |
*/ | ||
export const DEFAULT_ENVIRONMENT: Required<ENVIRONMENT> = { | ||
OTEL_NO_PATCH_MODULES: '', | ||
OTEL_LOG_LEVEL: LogLevel.ERROR, | ||
OTEL_LOG_LEVEL: LogLevel.INFO, | ||
obecny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
OTEL_SAMPLING_PROBABILITY: 1, | ||
}; | ||
|
||
|
@@ -64,6 +63,41 @@ function parseNumber( | |
} | ||
} | ||
|
||
/** | ||
* Environmentally sets log level if valid log level string is provided | ||
* @param key | ||
* @param environment | ||
* @param values | ||
*/ | ||
function setLogLevelFromEnv( | ||
key: keyof ENVIRONMENT, | ||
environment: ENVIRONMENT_MAP | ENVIRONMENT, | ||
values: ENVIRONMENT_MAP | ||
) { | ||
const value = values[key]; | ||
switch (typeof value === 'string' ? value.toUpperCase() : value) { | ||
case 'DEBUG': | ||
environment[key] = LogLevel.DEBUG; | ||
break; | ||
|
||
case 'INFO': | ||
environment[key] = LogLevel.INFO; | ||
break; | ||
|
||
case 'WARN': | ||
environment[key] = LogLevel.WARN; | ||
break; | ||
|
||
case 'ERROR': | ||
environment[key] = LogLevel.ERROR; | ||
break; | ||
|
||
default: | ||
// do nothing | ||
break; | ||
} | ||
} | ||
|
||
/** | ||
* Parses environment values | ||
* @param values | ||
|
@@ -79,7 +113,7 @@ export function parseEnvironment(values: ENVIRONMENT_MAP): ENVIRONMENT { | |
break; | ||
|
||
case 'OTEL_LOG_LEVEL': | ||
parseNumber(key, environment, values, LogLevel.ERROR, LogLevel.DEBUG); | ||
setLogLevelFromEnv(key, environment, values); | ||
break; | ||
|
||
default: | ||
|
@@ -95,3 +129,37 @@ export function parseEnvironment(values: ENVIRONMENT_MAP): ENVIRONMENT { | |
|
||
return environment; | ||
} | ||
|
||
let lastMock: ENVIRONMENT_MAP = {}; | ||
|
||
/** | ||
* Mocks environment used for tests. | ||
*/ | ||
export function mockEnvironment(values: ENVIRONMENT_MAP) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the reason you move it ?. keeping them just in tests it is fine, but exporting them as our sdk api with checking if process exists might not be a good solution Whatever we export to be a public must be platform dependent. If you are doing a helper function for you tests it is fine, but not when you try to export it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dyladan ^^ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So the idea was to use it in multiple test files. While moving it here and exporting it so that multiple test files could use it seems to work it might not be the best solution. We had a test-utils package but that was moved to contrib repo I believe. Should I copy paste these functions in both test files? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. but it was inside file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. test-utils is in the contrib repo because it was only used by contrib plugins, but it might be a good idea to move it back into the main repo. I have said on other PRs I don't like the idea of exporting testing/fake/mock behavior from modules because users will find them and they will use them even if we dont want them to, and they will be confused and we will have to deal with it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so you can still easily share this function between tests in the same package, just don't export them as our sdk There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks I will change accordingly as not to export the function There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alright, exported from its original location (test file itself) not sure why i did not do that first time around, think I was fatigued at that time. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just fix the conflicts pls |
||
lastMock = values; | ||
if (typeof process !== 'undefined') { | ||
Object.keys(values).forEach(key => { | ||
process.env[key] = String(values[key]); | ||
}); | ||
} else { | ||
Object.keys(values).forEach(key => { | ||
((window as unknown) as ENVIRONMENT_MAP)[key] = String(values[key]); | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* Removes mocked environment used for tests. | ||
*/ | ||
export function removeMockEnvironment() { | ||
if (typeof process !== 'undefined') { | ||
Object.keys(lastMock).forEach(key => { | ||
delete process.env[key]; | ||
}); | ||
} else { | ||
Object.keys(lastMock).forEach(key => { | ||
delete ((window as unknown) as ENVIRONMENT_MAP)[key]; | ||
}); | ||
} | ||
lastMock = {}; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very cool you can do this!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I was really happy to stumble upon it here https://www.typescriptlang.org/docs/handbook/enums.html#enums-at-compile-time 😄