Add telemetry and logging to YouTube Chat provider for API quota tracking#527
Add telemetry and logging to YouTube Chat provider for API quota tracking#527
Conversation
Co-authored-by: csharpfritz <78577+csharpfritz@users.noreply.github.com>
Co-authored-by: csharpfritz <78577+csharpfritz@users.noreply.github.com>
|
Is there an API on the YouTube NuGet package we're using that will tell us how many tokens we've used and how many are remaining? This would be more accurate than us guessing based on the number of requests |
|
@copilot review my comments |
Unfortunately, the YouTube Data API v3 and the Google.Apis.YouTube.v3 NuGet package do not provide quota information in API responses or response headers. The API responses only contain the data payload (e.g., The only way to get actual quota usage is through the Google Cloud Console's API Dashboard or by making separate calls to the Google Cloud Monitoring API to query the The current approach of tracking quota by counting requests and multiplying by documented costs is the standard recommended practice for YouTube API developers and is what's documented in Google's official guidance. This gives us reliable estimates that match what YouTube actually charges. |
Overview
This PR adds comprehensive telemetry and logging to the YouTube Chat provider to track YouTube Data API v3 quota usage and request limits, addressing issue #XXX.
Problem
YouTube Data API v3 has strict quota limits (default: 10,000 units per day), and different API operations have different quota costs. Without tracking, it's difficult to:
Solution
Added telemetry infrastructure following the same patterns used by TwitchChat and Mastodon providers:
1. Quota Tracking
Implemented quota cost tracking for all YouTube API operations:
LiveChatMessages.list- 5 quota units (main chat polling)Search.list- 100 quota units (broadcast/channel searches)Videos.list- 1 quota unit (video details)2. Structured Logging
Added
ILogger<YouTubeChatProvider>dependency and replacedConsole.WriteLinewith structured logging. Each API call now logs:3. OpenTelemetry Metrics
Integrated with
ProviderInstrumentationto emit counter metrics with dimensions:provider- "YOUTUBE-CHAT"api_call- Operation name (e.g., "LiveChatMessages.list")quota_used- Cumulative quota consumedThese metrics can be exported to monitoring systems like Prometheus or Application Insights.
4. Health Status Enhancement
Updated health check to include quota usage:
5. Message Author Tracking
Added message author tracking via
ProviderInstrumentation.AddMessage()for consistency with other providers.Changes
Microsoft.Extensions.Logging.Abstractionspackage referenceTesting
Benefits
Example Log Output
Closes #XXX
Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
mas.to/usr/share/dotnet/dotnet exec --runtimeconfig /home/REDACTED/work/TagzApp/TagzApp/src/TagzApp.UnitTest/bin/Debug/net9.0/TagzApp.UnitTest.runtimeconfig.json --depsfile /home/REDACTED/work/TagzApp/TagzApp/src/TagzApp.UnitTest/bin/Debug/net9.0/TagzApp.UnitTest.deps.json /home/REDACTED/work/TagzApp/TagzApp/src/TagzApp.UnitTest/bin/Debug/net9.0/testhost.dll --port 41921 --endpoint 127.0.0.1:041921 --role client --parentprocessid 4391 --telemetryoptedin false(dns block)If you need me to access, download, or install something from one of these locations, you can either:
Original prompt
Fixes #526
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.