Skip to content

Commit

Permalink
Add deprecated emit_metric() helper to cog interface (#2068)
Browse files Browse the repository at this point in the history
This helps with the transition between the main cog branch and the
experimental `async` branch. Models built for the `async` branch
can be run on either without code changes.
  • Loading branch information
aron authored Nov 29, 2024
1 parent ed72ad5 commit ed0fb02
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 2 deletions.
5 changes: 3 additions & 2 deletions python/cog/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from .base_predictor import BasePredictor
from .mimetypes_ext import install_mime_extensions
from .server.scope import current_scope
from .server.scope import current_scope, emit_metric
from .types import (
ConcatenateIterator,
ExperimentalFeatureWarning,
Expand All @@ -24,10 +24,11 @@

__all__ = [
"__version__",
"current_scope",
"emit_metric",
"BaseModel",
"BasePredictor",
"ConcatenateIterator",
"current_scope",
"ExperimentalFeatureWarning",
"File",
"Input",
Expand Down
18 changes: 18 additions & 0 deletions python/cog/server/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,21 @@ def scope(sc: Scope) -> Generator[None, None, None]:
yield
finally:
_current_scope.reset(s)


def emit_metric(name: str, value: Union[float, int]) -> None:
"""
DEPRECATED: This function will be removed in a future version of cog.
Records a metric event from the model. Intended to be called from
within the `predict` function.
This allows older models using an experimental `cog.emit_metric` function
to run using newer releases without requiring code changes.
"""
warnings.warn(
"emit_metric is deprecated and will be removed in a future version. Use `current_scope().record_metric()` instead",
category=DeprecationWarning,
stacklevel=1,
)
current_scope().record_metric(name, value)
13 changes: 13 additions & 0 deletions python/tests/server/fixtures/emit_metric.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from cog import emit_metric


class Predictor:
def setup(self):
print("did setup")

def predict(self, *, name: str) -> str:
print(f"hello, {name}")

emit_metric("foo", 123)

return f"hello, {name}"
13 changes: 13 additions & 0 deletions python/tests/server/fixtures/emit_metric_async.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from cog import emit_metric


class Predictor:
def setup(self):
print("did setup")

async def predict(self, *, name: str) -> str:
print(f"hello, {name}")

emit_metric("foo", 123)

return f"hello, {name}"
14 changes: 14 additions & 0 deletions python/tests/server/test_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,20 @@
"foo": 123,
},
),
(
WorkerConfig("emit_metric"),
{"name": ST_NAMES},
{
"foo": 123,
},
),
(
WorkerConfig("emit_metric_async", is_async=True),
{"name": ST_NAMES},
{
"foo": 123,
},
),
]

OUTPUT_FIXTURES = [
Expand Down

0 comments on commit ed0fb02

Please sign in to comment.