-
Couldn't load subscription status.
- Fork 148
feat: openai-js url path #2085
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
Draft
GeLi2001
wants to merge
15
commits into
main
Choose a base branch
from
feat/openai-url-path
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
feat: openai-js url path #2085
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
2f2e266
feat: openai-js url path
GeLi2001 882f78a
wip
GeLi2001 b19770a
wip
GeLi2001 4f6320e
wi[
GeLi2001 fadb104
wip
GeLi2001 1ae0a84
cleanup
GeLi2001 cb195e4
wip
GeLi2001 f0b2cf5
wip
GeLi2001 dc48f40
prettier
GeLi2001 9620b9f
wip
GeLi2001 f1c7374
wip
GeLi2001 145a900
wip
GeLi2001 b8eb005
Add URL extraction for Azure OpenAI debugging
GeLi2001 d4f3297
wip
GeLi2001 3410a19
wip
RogerHYang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@arizeai/openinference-instrumentation-openai": minor | ||
| --- | ||
|
|
||
| extract url for span attributes |
91 changes: 91 additions & 0 deletions
91
js/packages/openinference-instrumentation-openai/src/httpUtils.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| /** | ||
| * HTTP utilities for OpenTelemetry instrumentation | ||
| * JavaScript equivalent of opentelemetry-python-contrib/util/opentelemetry-util-http | ||
| * Minimal version containing only URL redaction functionality | ||
| */ | ||
|
|
||
| /** | ||
| * List of query parameters that should be redacted for security | ||
| */ | ||
| const PARAMS_TO_REDACT = [ | ||
| "AWSAccessKeyId", | ||
| "Signature", | ||
| "sig", | ||
| "X-Goog-Signature", | ||
| ]; | ||
|
|
||
| /** | ||
| * Replaces the username and password with the keyword `REDACTED` in a URL | ||
| * Only modifies the URL if it is valid and contains credentials | ||
| * @param url The URL string to process | ||
| * @returns The URL with credentials redacted, or original URL if invalid | ||
| */ | ||
| export function removeUrlCredentials(url: string): string { | ||
| try { | ||
| const parsed = new URL(url); | ||
|
|
||
| // Check if URL has credentials | ||
| if (parsed.username || parsed.password) { | ||
| // Create new URL with redacted credentials | ||
| const newUrl = new URL(url); | ||
| newUrl.username = "REDACTED"; | ||
| newUrl.password = "REDACTED"; | ||
| return newUrl.toString(); | ||
| } | ||
|
|
||
| return url; | ||
| } catch (error) { | ||
| // If URL parsing fails, return original URL | ||
| return url; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Redacts sensitive query parameter values in a URL | ||
| * @param url The URL string to process | ||
| * @returns The URL with sensitive query parameters redacted, or original URL if no changes needed | ||
| */ | ||
| export function redactQueryParameters(url: string): string { | ||
| try { | ||
| const parsed = new URL(url); | ||
|
|
||
| if (!parsed.search) { | ||
| // No query parameters to redact | ||
| return url; | ||
| } | ||
|
|
||
| const searchParams = new URLSearchParams(parsed.search); | ||
| let hasRedactedParams = false; | ||
|
|
||
| // Check if any parameters need redaction | ||
| for (const param of PARAMS_TO_REDACT) { | ||
| if (searchParams.has(param)) { | ||
| searchParams.set(param, "REDACTED"); | ||
| hasRedactedParams = true; | ||
| } | ||
| } | ||
|
|
||
| if (!hasRedactedParams) { | ||
| return url; | ||
| } | ||
|
|
||
| // Reconstruct URL with redacted parameters | ||
| const newUrl = new URL(url); | ||
| newUrl.search = searchParams.toString(); | ||
| return newUrl.toString(); | ||
| } catch (error) { | ||
| // If URL parsing fails, return original URL | ||
| return url; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Redacts sensitive data from the URL, including credentials and query parameters | ||
| * @param url The URL string to process | ||
| * @returns The URL with all sensitive data redacted | ||
| */ | ||
| export function redactUrl(url: string): string { | ||
| let redactedUrl = removeUrlCredentials(url); | ||
| redactedUrl = redactQueryParameters(redactedUrl); | ||
| return redactedUrl; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
js/packages/openinference-instrumentation-openai/test/httpUtils.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import { redactUrl } from "../src/httpUtils"; | ||
|
|
||
| describe("httpUtils", () => { | ||
| describe("redactUrl", () => { | ||
| it("should redact credentials from URLs", () => { | ||
| const url = "https://user:pass@api.openai.com/v1/chat/completions"; | ||
| const result = redactUrl(url); | ||
| expect(result).toBe( | ||
| "https://REDACTED:REDACTED@api.openai.com/v1/chat/completions", | ||
| ); | ||
| }); | ||
|
|
||
| it("should redact sensitive query parameters", () => { | ||
| const url = | ||
| "https://api.example.com/chat?AWSAccessKeyId=secret&Signature=secret&model=gpt-4"; | ||
| const result = redactUrl(url); | ||
| expect(result).toBe( | ||
| "https://api.example.com/chat?AWSAccessKeyId=REDACTED&Signature=REDACTED&model=gpt-4", | ||
| ); | ||
| }); | ||
|
|
||
| it("should redact both credentials and query parameters", () => { | ||
| const url = | ||
| "https://user:pass@api.example.com/chat?AWSAccessKeyId=secret&model=gpt-4"; | ||
| const result = redactUrl(url); | ||
| expect(result).toBe( | ||
| "https://REDACTED:REDACTED@api.example.com/chat?AWSAccessKeyId=REDACTED&model=gpt-4", | ||
| ); | ||
| }); | ||
|
|
||
| it("should handle malformed URLs gracefully", () => { | ||
| const malformedUrl = "not-a-valid-url"; | ||
| const result = redactUrl(malformedUrl); | ||
| expect(result).toBe(malformedUrl); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.