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

Use user URL if validation returns empty endpoint #2382

Conversation

NathanielRN
Copy link
Contributor

Which problem is this PR solving?

If a user sets the exporter URL to be something like localhost:8080, then we will run their string through the validateAndNormalizeUrl(url: string) function.

However, the new URL(url) constructor used in validateAndNormalizeUrl will return the following:

$ node
Welcome to Node.js v14.17.1.
Type ".help" for more information.
> const URL = require('url').URL;
undefined
> new URL('localhost:8080')
URL {
  href: 'localhost:8080',
  origin: 'null',
  protocol: 'localhost:',
  username: '',
  password: '',
  host: '', # Empty string!
  hostname: '',
  port: '',
  pathname: '8080',
  search: '',
  searchParams: URLSearchParams {},
  hash: ''
}
>

validateAndNormalizeUrl does not check the result of the parsing before it returns target.host which in the case above is just ''.

The correct URL would have been http://localhost:8080 which would have provided the correct target.host value:

> new URL('http://localhost:8080')
URL {
  href: 'http://localhost:8080/',
  origin: 'http://localhost:8080',
  protocol: 'http:',
  username: '',
  password: '',
  host: 'localhost:8080', # The correct value!
  hostname: 'localhost',
  port: '8080',
  pathname: '/',
  search: '',
  searchParams: URLSearchParams {},
  hash: ''
}
>

Short description of the changes

In Python we only return the .host (called .netloc) if it is not the empty string. Otherwise, we will try to access the exporter at the raw URL that the user provided.

These changes does the same: Try to validate and normalize the URL, but if the URL is the empty string, use the raw URL instead.

@codecov
Copy link

codecov bot commented Jul 28, 2021

Codecov Report

Merging #2382 (62694d1) into main (fd2410c) will increase coverage by 0.80%.
The diff coverage is n/a.

❗ Current head 62694d1 differs from pull request most recent head b7bc7df. Consider uploading reports for the commit b7bc7df to get more accurate results

@@            Coverage Diff             @@
##             main    #2382      +/-   ##
==========================================
+ Coverage   92.80%   93.61%   +0.80%     
==========================================
  Files         145       55      -90     
  Lines        5226     1753    -3473     
  Branches     1071      359     -712     
==========================================
- Hits         4850     1641    -3209     
+ Misses        376      112     -264     
Impacted Files Coverage Δ
...s/opentelemetry-core/src/platform/node/sdk-info.ts 0.00% <0.00%> (-100.00%) ⬇️
...pentelemetry-core/src/platform/node/performance.ts 0.00% <0.00%> (-100.00%) ⬇️
...emetry-api-metrics/src/platform/node/globalThis.ts 0.00% <0.00%> (-100.00%) ⬇️
...ckages/opentelemetry-exporter-zipkin/src/zipkin.ts 83.92% <0.00%> (-16.08%) ⬇️
...ackages/opentelemetry-shim-opentracing/src/shim.ts
...sync-hooks/src/AbstractAsyncHooksContextManager.ts
...elemetry-tracing/src/export/ConsoleSpanExporter.ts
...pentelemetry-core/src/platform/node/environment.ts
...ges/opentelemetry-metrics/src/SumObserverMetric.ts
...elemetry-exporter-zipkin/src/platform/node/util.ts
... and 84 more

@NathanielRN NathanielRN force-pushed the use-provided-url-if-parse-fails branch from eaa3993 to 2dba5e6 Compare July 28, 2021 19:38
@NathanielRN NathanielRN marked this pull request as draft July 28, 2021 19:49
@NathanielRN
Copy link
Contributor Author

Just noticed that #2322 resolves a similar issue. Will update this PR after looking at it more closely.

@NathanielRN NathanielRN force-pushed the use-provided-url-if-parse-fails branch from 2dba5e6 to b7bc7df Compare July 28, 2021 20:00
@NathanielRN
Copy link
Contributor Author

Never mind, I think #2322 solves my issue 🙂

@NathanielRN NathanielRN deleted the use-provided-url-if-parse-fails branch July 28, 2021 20:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant