-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Add support for backend telemetry (tracing & metrics) #1013
Open
zacps
wants to merge
6
commits into
huggingface:main
Choose a base branch
from
zacps:opentelemetry-pr
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.
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6c6f991
Add support for backend telemetry (tracing & metrics)
zacps 02182d2
Merge branch 'main' into opentelemetry-pr
nsarrazin d01d180
Merge branch 'main' into opentelemetry-pr
nsarrazin 00ab11e
Ensure only one version of @opentelemetry/sdk-metrics is selected
zacps 336b892
Drop user labels
zacps 06a176e
Merge branch 'main' into opentelemetry-pr
zacps 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 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
This file contains 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 |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
"packageManager": "[email protected]", | ||
"scripts": { | ||
"dev": "vite dev", | ||
"build": "vite build", | ||
"build": "bash scripts/clean-opentelemetry.sh && vite build", | ||
"preview": "vite preview", | ||
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json", | ||
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch", | ||
|
@@ -53,6 +53,12 @@ | |
"@huggingface/hub": "^0.5.1", | ||
"@huggingface/inference": "^2.6.3", | ||
"@iconify-json/bi": "^1.1.21", | ||
"@opentelemetry/api": "^1.8.0", | ||
"@opentelemetry/auto-instrumentations-node": "^0.44.0", | ||
"@opentelemetry/exporter-metrics-otlp-proto": "^0.50.0", | ||
"@opentelemetry/sdk-metrics": "^1.23.0", | ||
"@opentelemetry/sdk-node": "^0.50.0", | ||
"@opentelemetry/sdk-trace-node": "^1.23.0", | ||
"@resvg/resvg-js": "^2.6.0", | ||
"@xenova/transformers": "^2.16.1", | ||
"autoprefixer": "^10.4.14", | ||
|
This file contains 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,7 @@ | ||
#!/bin/bash | ||
|
||
packages="./node_modules/@opentelemetry/*/package.json" | ||
|
||
for file in ${packages}; do | ||
sed -i '/"module": "build\/esm\/index\.js",/d' ${file} | ||
done |
This file contains 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
This file contains 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
This file contains 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,77 @@ | ||
// This file is built outside of sveltekit and cannot import from the rest of the application | ||
// or special imports like $env/dynamic/private. | ||
import { NodeSDK } from "@opentelemetry/sdk-node"; | ||
import { getNodeAutoInstrumentations } from "@opentelemetry/auto-instrumentations-node"; | ||
import { PeriodicExportingMetricReader } from "@opentelemetry/sdk-metrics"; | ||
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-proto"; | ||
import { OTLPMetricExporter } from "@opentelemetry/exporter-metrics-otlp-proto"; | ||
import { SEMRESATTRS_SERVICE_NAME } from "@opentelemetry/semantic-conventions"; | ||
import { AlwaysOnSampler } from "@opentelemetry/sdk-trace-base"; | ||
import { Resource } from "@opentelemetry/resources"; | ||
|
||
const TRACE_URL = | ||
process.env.OTEL_EXPORTER_OTLP_ENDPOINT + "/v1/traces" || "http://localhost:4318/v1/traces"; | ||
const METRICS_URL = | ||
process.env.OTEL_EXPORTER_OTLP_ENDPOINT + "/v1/metrics" || "http://localhost:4318/v1/metrics"; | ||
const SERVICE_NAME = process.env.OTEL_SERVICE_NAME || "huggingface/chat-ui"; | ||
|
||
const exporter = new OTLPTraceExporter({ | ||
url: TRACE_URL, | ||
headers: {}, | ||
}); | ||
|
||
const otelNodeSdk = new NodeSDK({ | ||
autoDetectResources: true, | ||
serviceName: SERVICE_NAME, | ||
traceExporter: exporter, | ||
metricReader: new PeriodicExportingMetricReader({ | ||
exporter: new OTLPMetricExporter({ | ||
url: METRICS_URL, | ||
headers: {}, | ||
}), | ||
}), | ||
sampler: new AlwaysOnSampler(), | ||
resource: new Resource({ | ||
[SEMRESATTRS_SERVICE_NAME]: SERVICE_NAME, | ||
}), | ||
instrumentations: [ | ||
getNodeAutoInstrumentations({ | ||
"@opentelemetry/instrumentation-http": { | ||
ignoreIncomingRequestHook: (request) => { | ||
// Don't trace static asset requests | ||
if ( | ||
request.url?.endsWith(".js") || | ||
request.url?.endsWith(".svg") || | ||
request.url?.endsWith(".css") | ||
) { | ||
return false; | ||
} | ||
return true; | ||
}, | ||
}, | ||
}), | ||
], | ||
}); | ||
|
||
export class Telemetry { | ||
private static instance: Telemetry; | ||
private initialized = false; | ||
|
||
private constructor() {} | ||
|
||
public static getInstance(): Telemetry { | ||
if (!Telemetry.instance) { | ||
Telemetry.instance = new Telemetry(); | ||
} | ||
return Telemetry.instance; | ||
} | ||
|
||
public start() { | ||
if (!this.initialized) { | ||
this.initialized = true; | ||
otelNodeSdk.start(); | ||
} | ||
} | ||
} | ||
|
||
Telemetry.getInstance().start(); |
This file contains 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,19 @@ | ||
import { defineConfig } from "vite"; | ||
|
||
export default defineConfig({ | ||
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. We unfortunately need a second build step because sveltekit overrides vite's |
||
build: { | ||
emptyOutDir: false, | ||
ssr: true, | ||
target: "node18", | ||
outDir: "build", | ||
rollupOptions: { | ||
input: { | ||
telemetry: "src/telemetry.ts", | ||
}, | ||
}, | ||
lib: { | ||
formats: ["cjs"], | ||
entry: "src/telemetry.ts", | ||
}, | ||
}, | ||
}); |
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.
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.
Workaround for open-telemetry/opentelemetry-js#3989