-
Notifications
You must be signed in to change notification settings - Fork 74
Add emitting token count metrics to datadog statsd #458
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
Merged
seanshi-scale
merged 13 commits into
main
from
seanshi/add-token-count-metrics-emitting
Mar 27, 2024
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
0574a2d
start for dd metrics gateway for public repo
seanshi-scale 0b2137a
DD monitoring metrics gateway
seanshi-scale fac0437
new token counts
seanshi-scale c432591
lap()
seanshi-scale 856e4c3
ordering
seanshi-scale 34ffc16
import in dependencies
seanshi-scale d8f229c
Merge branch 'main' into seanshi/add-token-count-metrics-emitting
seanshi-scale 00f2771
itl
seanshi-scale 9d4c415
tft + comment
seanshi-scale 5aadc29
slots
seanshi-scale 6142300
timer unit test
seanshi-scale 019ad9d
unit test for dd metrics gateway because of the coverage thing
seanshi-scale be785a0
respond to comment, add a comment on what the ITL value means
seanshi-scale 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
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
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
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
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
87 changes: 87 additions & 0 deletions
87
model-engine/model_engine_server/infra/gateways/datadog_monitoring_metrics_gateway.py
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,87 @@ | ||
| from typing import List, Optional | ||
|
|
||
| from datadog import statsd | ||
| from model_engine_server.common.dtos.llms import TokenUsage | ||
| from model_engine_server.core.config import infra_config | ||
| from model_engine_server.domain.gateways.monitoring_metrics_gateway import ( | ||
| MetricMetadata, | ||
| MonitoringMetricsGateway, | ||
| ) | ||
|
|
||
|
|
||
| def get_model_tags(model_name: Optional[str]) -> List[str]: | ||
| """ | ||
| Returns a tag for the model name and whether it is a finetuned model | ||
| """ | ||
| tags = [] | ||
| if model_name: | ||
| parts = model_name.split(".") | ||
| tags.extend([f"model_name:{parts[0]}"]) | ||
| return tags | ||
|
|
||
|
|
||
| class DatadogMonitoringMetricsGateway(MonitoringMetricsGateway): | ||
| def __init__(self, prefix: str = "model_engine"): | ||
| self.prefix = prefix | ||
| self.tags = [f"env:{infra_config().env}"] | ||
|
|
||
| def emit_attempted_build_metric(self): | ||
| statsd.increment("scale_launch.service_builder.attempt", tags=self.tags) | ||
|
|
||
| def emit_successful_build_metric(self): | ||
| statsd.increment("scale_launch.service_builder.success", tags=self.tags) | ||
|
|
||
| def emit_build_time_metric(self, duration_seconds: float): | ||
| statsd.distribution( | ||
| "scale_launch.service_builder.endpoint_build_time", duration_seconds, tags=self.tags | ||
| ) | ||
|
|
||
| def emit_image_build_cache_hit_metric(self, image_type: str): | ||
| statsd.increment( | ||
| f"scale_launch.service_builder.{image_type}_image_cache_hit", tags=self.tags | ||
| ) | ||
|
|
||
| def emit_image_build_cache_miss_metric(self, image_type: str): | ||
| statsd.increment( | ||
| f"scale_launch.service_builder.{image_type}_image_cache_miss", tags=self.tags | ||
| ) | ||
|
|
||
| def emit_docker_failed_build_metric(self): | ||
| statsd.increment("scale_launch.service_builder.docker_failed", tags=self.tags) | ||
|
|
||
| def emit_database_cache_hit_metric(self): | ||
| statsd.increment("scale_launch.database_cache.hit", tags=self.tags) | ||
|
|
||
| def emit_database_cache_miss_metric(self): | ||
| statsd.increment("scale_launch.database_cache.miss", tags=self.tags) | ||
|
|
||
| def _format_call_tags(self, metadata: MetricMetadata) -> List[str]: | ||
| tags = self.tags | ||
| tags.extend(get_model_tags(metadata.model_name)) | ||
| return tags | ||
|
|
||
| def emit_route_call_metric(self, route: str, metadata: MetricMetadata): | ||
| statsd.increment(f"{self.prefix}.{route}.call", tags=self._format_call_tags(metadata)) | ||
|
|
||
| def emit_token_count_metrics(self, token_usage: TokenUsage, metadata: MetricMetadata): | ||
| tags = self._format_call_tags(metadata) | ||
|
|
||
| token_count_metric = f"{self.prefix}.token_count" | ||
| statsd.increment( | ||
| f"{token_count_metric}.prompt", (token_usage.num_prompt_tokens or 0), tags=tags | ||
| ) | ||
| statsd.increment( | ||
| f"{token_count_metric}.completion", (token_usage.num_completion_tokens or 0), tags=tags | ||
| ) | ||
| statsd.increment(f"{token_count_metric}.total", token_usage.num_total_tokens, tags=tags) | ||
|
|
||
| total_tokens_per_second = f"{self.prefix}.total_tokens_per_second" | ||
| statsd.histogram(total_tokens_per_second, token_usage.total_tokens_per_second, tags=tags) | ||
|
|
||
| time_to_first_token = f"{self.prefix}.time_to_first_token" | ||
| if token_usage.time_to_first_token is not None: | ||
| statsd.distribution(time_to_first_token, token_usage.time_to_first_token, tags=tags) | ||
|
|
||
| inter_token_latency = f"{self.prefix}.inter_token_latency" | ||
| if token_usage.inter_token_latency is not None: | ||
| statsd.distribution(inter_token_latency, token_usage.inter_token_latency, tags=tags) | ||
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,15 @@ | ||
| import time | ||
|
|
||
| from model_engine_server.core.utils.timer import timer | ||
|
|
||
|
|
||
| def test_timer(): | ||
| with timer() as t: | ||
| time.sleep(0.1) | ||
| lap_time = t.lap() | ||
| time.sleep(0.01) | ||
| new_lap_time = t.lap() | ||
|
|
||
| assert new_lap_time >= 0.009 | ||
| assert lap_time >= 0.09 | ||
| assert t.duration >= 0.1 |
106 changes: 106 additions & 0 deletions
106
model-engine/tests/unit/infra/gateways/test_datadog_monitoring_metrics_gateway.py
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,106 @@ | ||
| from unittest.mock import Mock | ||
|
|
||
| import pytest | ||
| from datadog import statsd | ||
| from model_engine_server.common.dtos.llms import TokenUsage | ||
| from model_engine_server.core.auth.authentication_repository import User | ||
| from model_engine_server.domain.gateways.monitoring_metrics_gateway import MetricMetadata | ||
| from model_engine_server.infra.gateways import DatadogMonitoringMetricsGateway | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def mock_statsd(): | ||
| # https://github.com/DataDog/datadogpy/issues/183 for how dd mocks statsd | ||
| statsd.socket = Mock() | ||
| # also mock the methods we use or may use, there might be more | ||
| statsd.gauge = Mock() | ||
| statsd.increment = Mock() | ||
| statsd.decrement = Mock() | ||
| statsd.histogram = Mock() | ||
| statsd.distribution = Mock() | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def sync_token_count(): | ||
| return TokenUsage( | ||
| num_prompt_tokens=100, | ||
| num_completion_tokens=200, | ||
| total_duration=30, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def streaming_token_count(): | ||
| return TokenUsage( | ||
| num_prompt_tokens=100, | ||
| num_completion_tokens=200, | ||
| total_duration=30, | ||
| time_to_first_token=5, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def datadog_monitoring_metrics_gateway(): | ||
| gateway = DatadogMonitoringMetricsGateway(prefix="model_engine_unit_test") | ||
| return gateway | ||
|
|
||
|
|
||
| def test_datadog_monitoring_metrics_gateway_build_metrics(datadog_monitoring_metrics_gateway): | ||
| datadog_monitoring_metrics_gateway.emit_attempted_build_metric() | ||
| statsd.increment.assert_called_once() | ||
| statsd.increment.reset_mock() | ||
| datadog_monitoring_metrics_gateway.emit_successful_build_metric() | ||
| statsd.increment.assert_called_once() | ||
| statsd.increment.reset_mock() | ||
| datadog_monitoring_metrics_gateway.emit_build_time_metric(300) | ||
| statsd.distribution.assert_called_once() | ||
| statsd.distribution.reset_mock() | ||
| datadog_monitoring_metrics_gateway.emit_image_build_cache_hit_metric("test_image") | ||
| statsd.increment.assert_called_once() | ||
| statsd.increment.reset_mock() | ||
| datadog_monitoring_metrics_gateway.emit_image_build_cache_miss_metric("test_image_2") | ||
| statsd.increment.assert_called_once() | ||
| statsd.increment.reset_mock() | ||
| datadog_monitoring_metrics_gateway.emit_docker_failed_build_metric() | ||
| statsd.increment.assert_called_once() | ||
| statsd.increment.reset_mock() | ||
|
|
||
|
|
||
| def test_datadog_monitoring_metrics_gateway_db_metrics(datadog_monitoring_metrics_gateway): | ||
| datadog_monitoring_metrics_gateway.emit_database_cache_hit_metric() | ||
| statsd.increment.assert_called_once() | ||
| statsd.increment.reset_mock() | ||
| datadog_monitoring_metrics_gateway.emit_database_cache_miss_metric() | ||
| statsd.increment.assert_called_once() | ||
| statsd.increment.reset_mock() | ||
|
|
||
|
|
||
| def test_datadog_monitoring_metrics_gateway_route_call_metrics(datadog_monitoring_metrics_gateway): | ||
| metadata = MetricMetadata( | ||
| user=User(user_id="test_user", team_id="test_team", email="test_email"), | ||
| model_name="test_model", | ||
| ) | ||
| datadog_monitoring_metrics_gateway.emit_route_call_metric("test_route", metadata) | ||
| statsd.increment.assert_called_once() | ||
| statsd.increment.reset_mock() | ||
|
|
||
|
|
||
| def test_datadog_monitoring_metrics_gateway_token_count_metrics( | ||
| datadog_monitoring_metrics_gateway, sync_token_count, streaming_token_count | ||
| ): | ||
| metadata = MetricMetadata( | ||
| user=User(user_id="test_user", team_id="test_team", email="test_email"), | ||
| model_name="test_model", | ||
| ) | ||
| datadog_monitoring_metrics_gateway.emit_token_count_metrics(sync_token_count, metadata) | ||
| statsd.increment.assert_called() | ||
| statsd.increment.reset_mock() | ||
| statsd.histogram.assert_called() | ||
| statsd.histogram.reset_mock() | ||
| datadog_monitoring_metrics_gateway.emit_token_count_metrics(streaming_token_count, metadata) | ||
| statsd.increment.assert_called() | ||
| statsd.increment.reset_mock() | ||
| statsd.histogram.assert_called() | ||
| statsd.histogram.reset_mock() | ||
| statsd.distribution.assert_called() | ||
| statsd.distribution.reset_mock() |
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.