Skip to content
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

feat(metrics): add option to specify dataset. #271

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ Changes are grouped as follows
- `Fixed` for any bug fixes.
- `Security` in case of vulnerabilities.

## [6.1.0]

### Added

* Added ability to specify dataset under which metrics timeseries are created

## [6.0.1]

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion cognite/extractorutils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
Cognite extractor utils is a Python package that simplifies the development of new extractors.
"""

__version__ = "6.0.1"
__version__ = "6.1.0"
from .base import Extractor
2 changes: 2 additions & 0 deletions cognite/extractorutils/configtools/elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@ class _CogniteMetricsConfig:
external_id_prefix: str
asset_name: Optional[str]
asset_external_id: Optional[str]
data_set: Optional[EitherId]

push_interval: TimeIntervalConfig = TimeIntervalConfig("30s")

Expand Down Expand Up @@ -485,6 +486,7 @@ def start_pushers(self, cdf_client: CogniteClient, cancellation_token: Event = E
external_id_prefix=self.cognite.external_id_prefix,
push_interval=self.cognite.push_interval.seconds,
asset=asset,
data_set=self.cognite.data_set,
thread_name="CogniteMetricsPusher", # There is only one Cognite project as a target
cancellation_token=cancellation_token,
)
Expand Down
13 changes: 13 additions & 0 deletions cognite/extractorutils/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def __init__(self):
from cognite.client import CogniteClient
from cognite.client.data_classes import Asset, Datapoints, DatapointsArray, TimeSeries
from cognite.client.exceptions import CogniteDuplicatedError
from cognite.extractorutils.util import EitherId

from .util import ensure_time_series

Expand Down Expand Up @@ -330,6 +331,7 @@ class CognitePusher(AbstractMetricsPusher):
external_id_prefix: Unique external ID prefix for this pusher.
push_interval: Seconds between each upload call
asset: Optional contextualization.
data_set: Data set the metrics timeseries created under.
thread_name: Name of thread to start. If omitted, a standard name such as Thread-4 will be generated.
cancellation_token: Event object to be used as a thread cancelation event
"""
Expand All @@ -340,6 +342,7 @@ def __init__(
external_id_prefix: str,
push_interval: int,
asset: Optional[Asset] = None,
data_set: Optional[EitherId] = None,
thread_name: Optional[str] = None,
cancellation_token: Event = Event(),
):
Expand All @@ -348,6 +351,7 @@ def __init__(
self.cdf_client = cdf_client
self.asset = asset
self.external_id_prefix = external_id_prefix
self.data_set = data_set

self._init_cdf()

Expand All @@ -372,6 +376,14 @@ def _init_cdf(self) -> None:
else:
asset_id = None

data_set_id = None
if self.data_set:
dataset = self.cdf_client.data_sets.retrieve(
id=self.data_set.internal_id, external_id=self.data_set.external_id
)
if dataset:
data_set_id = dataset.id

for metric in REGISTRY.collect():
if type(metric) == Metric and metric.type in ["gauge", "counter"]:
external_id = self.external_id_prefix + metric.name
Expand All @@ -383,6 +395,7 @@ def _init_cdf(self) -> None:
legacy_name=external_id,
description=metric.documentation,
asset_id=asset_id, # type: ignore # this is optional. Type hint in SDK is wrong
data_set_id=data_set_id, # type: ignore # this is optional. Type hint in SDK is wrong
)
)

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "cognite-extractor-utils"
version = "6.0.1"
version = "6.1.0"
description = "Utilities for easier development of extractors for CDF"
authors = ["Mathias Lohne <[email protected]>"]
license = "Apache-2.0"
Expand Down
13 changes: 11 additions & 2 deletions schema/metrics_config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
"description": "PushGateway password"
},
"clear-after": {
"type": ["null", "integer"],
"type": [
"null",
"integer"
],
"description": "Clear metrics after this many seconds when the extractor stops. Default is disabled"
},
"push-interval": {
Expand All @@ -44,7 +47,9 @@
"type": "object",
"description": "Push metrics to CDF timeseries. Requires CDF credentials to be configured",
"unevaluatedProperties": false,
"required": ["external-id-prefix"],
"required": [
"external-id-prefix"
],
"properties": {
"external-id-prefix": {
"type": "string",
Expand All @@ -62,6 +67,10 @@
"type": "integer",
"description": "Interval in seconds between each push to CDF",
"default": 30
},
"data-set": {
"type": "string",
"description": "Data set the metrics timeseries created under"
}
}
},
Expand Down
Loading