feat(client): add typed Metrics API wrapper (v1/v2) #724
+490
−0
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.
Thanks for creating a great tool!
Problem
I use Langfuse tracing regularly and occasionally rely on the Metrics API for aggregated analytics.
While the generated API client already exposes
api.metricsandapi.metricsV2, both endpoints accept the query payload as a JSON string (queryURL param). In TS/JS this ends up asquery: string, which lacks type safety and makes constructing and maintaining metrics queries cumbersome and error-prone.This PR is intended as an ergonomic improvement proposal for Metrics queries. If there is a preferred direction for the SDK, feel free to close/supersede it.
Changes
MetricsManagerto@langfuse/clientand expose it aslangfuse.metricsMetricsQueryV1,MetricsQueryV2, etc.) and a serializermetrics.query(...)/metrics.queryRaw(...)for v1metrics.queryV2(...)/metrics.queryRawV2(...)for v2Release info Sub-libraries affected
Bump level
Libraries affected
Changelog notes
langfuse.metricstyped wrapper for Metrics API queries (v1/v2)Important
Adds
MetricsManagertoLangfuseClientfor typed Metrics API queries with v1/v2 support and integration tests.MetricsManagertoLangfuseClientfor typed Metrics API queries.metrics.query(...)andmetrics.queryV2(...)for v1 and v2 queries.metrics.queryRaw(...)andmetrics.queryRawV2(...).MetricsQueryV1,MetricsQueryV2, and related types intypes.ts.serializeMetricsQuery()for query serialization.metrics.integration.test.tsto validate query serialization and request options.This description was created by
for d9b9c4f. You can customize this summary. It will automatically update as commits are pushed.
Disclaimer: Experimental PR review
Greptile Overview
Greptile Summary
This PR adds a typed wrapper for the Langfuse Metrics API, providing type-safe query construction and improved developer experience. The implementation introduces
MetricsManagerwhich serializes typed query objects into the JSON string format required by the underlying API endpoints.Key Changes
MetricsQueryV1,MetricsQueryV2, filters, dimensions, etc.)MetricsManagerclass with convenience methods:query(),queryV2(),queryRaw(),queryRawV2()MetricsManagerintoLangfuseClientaslangfuse.metricspropertyDesign Quality
PromptManager,ScoreManager, etc.)Parameters<>utility type ensures automatic sync with underlying API client typesqueryRawmethods for advanced use casesConfidence Score: 5/5
Important Files Changed
Sequence Diagram
sequenceDiagram participant User participant LangfuseClient participant MetricsManager participant serializeMetricsQuery participant LangfuseAPIClient participant API as Metrics API User->>LangfuseClient: new LangfuseClient(params) LangfuseClient->>MetricsManager: new MetricsManager({ apiClient }) LangfuseClient-->>User: langfuse alt V1 Query (Typed) User->>MetricsManager: langfuse.metrics.query(queryObject, options) MetricsManager->>serializeMetricsQuery: serializeMetricsQuery(queryObject) serializeMetricsQuery-->>MetricsManager: JSON string MetricsManager->>MetricsManager: queryRaw(serializedQuery, options) MetricsManager->>LangfuseAPIClient: metrics.metrics({ query }, options) LangfuseAPIClient->>API: GET /api/public/metrics?query=... API-->>LangfuseAPIClient: MetricsResponse LangfuseAPIClient-->>MetricsManager: MetricsResponse MetricsManager-->>User: MetricsResponse else V2 Query (Typed) User->>MetricsManager: langfuse.metrics.queryV2(queryObject, options) MetricsManager->>serializeMetricsQuery: serializeMetricsQuery(queryObject) serializeMetricsQuery-->>MetricsManager: JSON string MetricsManager->>MetricsManager: queryRawV2(serializedQuery, options) MetricsManager->>LangfuseAPIClient: metricsV2.metrics({ query }, options) LangfuseAPIClient->>API: GET /api/public/v2/metrics?query=... API-->>LangfuseAPIClient: MetricsV2Response LangfuseAPIClient-->>MetricsManager: MetricsV2Response MetricsManager-->>User: MetricsV2Response else Raw Query (Advanced) User->>MetricsManager: langfuse.metrics.queryRaw(jsonString, options) MetricsManager->>LangfuseAPIClient: metrics.metrics({ query }, options) LangfuseAPIClient->>API: GET /api/public/metrics?query=... API-->>LangfuseAPIClient: MetricsResponse LangfuseAPIClient-->>MetricsManager: MetricsResponse MetricsManager-->>User: MetricsResponse end(2/5) Greptile learns from your feedback when you react with thumbs up/down!