[8.x](backport #41516) x-pack/metricbeat/module/openai: Add new module#42033
Merged
[8.x](backport #41516) x-pack/metricbeat/module/openai: Add new module#42033
Conversation
(cherry picked from commit 93b018a)
|
This pull request doesn't have a |
Contributor
Author
|
This pull request has not been merged yet. Could you please review and merge it @shmsr? 🙏 |
shmsr
approved these changes
Dec 16, 2024
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Proposed commit message
Implement a new module for OpenAI usage collection. This module operates on
https://api.openai.com/v1/usage(by default; also configurable for Proxy URLs, etc.) and collects the limited set of usage metrics emitted from the undocumented endpoint.Example how the usage endpoints emits metrics:
Given timestamps
t0,t1,t2, ...tnin ascending order:t0(first collection):t1(after new API usage):t2(continuous collection):and so on.
Example response:
{ "object": "list", "data": [ { "organization_id": "org-xxx", "organization_name": "Personal", "aggregation_timestamp": 1725389580, "n_requests": 1, "operation": "completion", "snapshot_id": "gpt-4o-mini-2024-07-18", "n_context_tokens_total": 62, "n_generated_tokens_total": 21, "email": null, "api_key_id": null, "api_key_name": null, "api_key_redacted": null, "api_key_type": null, "project_id": null, "project_name": null, "request_type": "" }, { "organization_id": "org-xxx", "organization_name": "Personal", "aggregation_timestamp": 1725389640, "n_requests": 1, "operation": "completion", "snapshot_id": "gpt-4o-mini-2024-07-18", "n_context_tokens_total": 97, "n_generated_tokens_total": 17, "email": null, "api_key_id": null, "api_key_name": null, "api_key_redacted": null, "api_key_type": null, "project_id": null, "project_name": null, "request_type": "" } ], "tpm_data": [ { "organization_id": "org-xxx", "organization_name": "Personal", "day_timestamp": 1725321600, "snapshot_id": "gpt-4o-mini-2024-07-18", "operation": "completion", "p90_context_tpm": 97, "p90_generated_tpm": 21, "p90_provisioned_context_tpm": 0, "p90_provisioned_generated_tpm": 0, "max_context_tpm": 97, "max_generated_tpm": 21, "max_provisioned_context_tpm": 0, "max_provisioned_generated_tpm": 0 } ], "ft_data": [], "dalle_api_data": [], "whisper_api_data": [], "tts_api_data": [], "assistant_code_interpreter_data": [], "retrieval_storage_data": [] }As soon as the API is used, usage is generated after a few times. So, if collecting using the module real-time and that too multiple times of the day, it would collect duplicates and it is not good for storage as well as analytics of the usage data.
It's better to collect
time.Now() (in UTC) - 24hso that we get full usage collection of the past day (in UTC) and it avoids duplication. So that's why I have introduced a configrealtimeand set it tofalseas the collection is 24h delayed; we are now getting daily data.realtime: truewill work as any other normal collection where metrics are fetched in set intervals. Our recommendation is to keeprealtime: false.As this is a metricbeat module, we do not have existing package that gives us support to store the cursor. So, in order to avoid pulling already pulled data, timestamps are being stored per API key. Logic for the same is commented in the code on how it is stored. We are using a new custom code to store the state in order to store the cursor and begin from the next available date.
Checklist
CHANGELOG.next.asciidocorCHANGELOG-developer.next.asciidoc.Author's Checklist
How to test this PR locally
This is an automatic backport of pull request x-pack/metricbeat/module/openai: Add new module #41516 done by Mergify.