Skip to content

Conversation

@Kunals990
Copy link

Description

When base endpoint has a non-root path (e.g. /telemetry), default overrides for logs and metrics are now properly appended instead of replacing the base path.

Changes:

  • Add normalizeAndJoinPaths method to handle URI path concatenation
  • Fix path handling to preserve base path when joining URLs
  • Support both absolute (/v1/metrics) and relative (v1/metrics) path formats
  • Handle null base paths and trailing slashes correctly

Tests:

  • Add shouldAppendPathsWithNonRootBase to verify non-root base path handling
  • Add shouldHandlePathsWithoutLeadingSlash to verify relative path support

Fixes #1602

Comment on lines 63 to 95

this.metrics = normalizeAndJoinPaths(location, overrides.metrics);
this.logs = normalizeAndJoinPaths(location, overrides.logs);
}

private URI normalizeAndJoinPaths(URI base, String path) {
String basePath = base.getPath();
if (basePath == null) {
basePath = "/";
}

String pathToAppend = path;
if (pathToAppend.startsWith("/")) {
pathToAppend = pathToAppend.substring(1);
}

if (!basePath.endsWith("/")) {
basePath += "/";
}

try {
return new URI(
base.getScheme(),
base.getUserInfo(),
base.getHost(),
base.getPort(),
basePath + pathToAppend,
base.getQuery(),
base.getFragment()
);
} catch (Exception e) {
throw new RuntimeException("Failed to join URI paths", e);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This approach loses the ability to fully override the endpoint with a different URI that includes scheme host and port as well as path.

Suggested change
this.metrics = normalizeAndJoinPaths(location, overrides.metrics);
this.logs = normalizeAndJoinPaths(location, overrides.logs);
}
private URI normalizeAndJoinPaths(URI base, String path) {
String basePath = base.getPath();
if (basePath == null) {
basePath = "/";
}
String pathToAppend = path;
if (pathToAppend.startsWith("/")) {
pathToAppend = pathToAppend.substring(1);
}
if (!basePath.endsWith("/")) {
basePath += "/";
}
try {
return new URI(
base.getScheme(),
base.getUserInfo(),
base.getHost(),
base.getPort(),
basePath + pathToAppend,
base.getQuery(),
base.getFragment()
);
} catch (Exception e) {
throw new RuntimeException("Failed to join URI paths", e);
}
this.metrics = resolveOverride(location, overrides.metrics);
this.logs = resolveOverride(location, overrides.logs);
}
private URI resolveOverride(
URI location,
String override)
{
URI normalized = Optional.ofNullable(location.getPath())
.map(p -> p.endsWith("/") ? p : p + "/")
.map(location::resolve)
.orElse(location.resolve("/"));
URI overrideURI = Optional.of(URI.create(override))
.map(...)
// TODO: check for missing authority to treat as path only
// TODO: if path has leading / then remove
.get();
// TODO: resolve overrideURI against normalized location to support absolute or relative overrides
}

assertThat(metrics, equalTo(URI.create("http://example.com/v42/metrix")));
assertThat(logs, equalTo(URI.create("http://example.com/v42/logz")));
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test for full URI override, including scheme host and port that differ from base location.

- Add resolveOverride() to handle path-only and full URI overrides
- Normalize base path and strip leading slash from override paths
- Add tests for non-root base path and full URI override scenarios
@Kunals990
Copy link
Author

@jfallows I've updated the PR based on your feedback:

  • Implemented resolveOverride() method that checks for full URI overrides (scheme/authority)
  • Preserves the ability to fully override endpoints with different scheme, host, and port
  • Normalizes base path and properly appends relative override paths
  • Added test shouldAllowFullUriOverrideWithNonRootBase() as requested

The implementation now supports both path appending for non-root bases and full URI overrides.

@jfallows
Copy link
Contributor

@Kunals990 the PR build is broken seems due to checkstyle issues.

Please run a full local build ./mvnw clean install from top level to verify successful build and then push any needed changes to this PR so that the PR build can also succeed.

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.

otlp exporter uses incorrect path when base endpoint path is non-root

2 participants