Skip to content

Commit

Permalink
Merge pull request #228 from lsst/tickets/DM-43431
Browse files Browse the repository at this point in the history
DM-43431: In SasquatchDatastore allow namespace and extra fields to come from environment
  • Loading branch information
timj committed Mar 22, 2024
2 parents 9115098 + 7e230db commit df12eed
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
Expand Up @@ -60,6 +60,8 @@
"instrument",
"band",
"exposure",
"group",
"day_obs",
]


Expand Down
Expand Up @@ -25,6 +25,7 @@

"""Sasquatch datastore"""
import logging
import os
from collections.abc import Iterable, Mapping, Sequence
from typing import TYPE_CHECKING, Any, ClassVar

Expand Down Expand Up @@ -80,6 +81,22 @@ class SasquatchDatastore(GenericBaseDatastore):
namespace: str
"""The namespace in Sasquatch where the uploaded metrics will be
dispatched.
The namespace can be read from the environment using the
``$DAF_BUTLER_SASQUATCH_NAMESPACE`` environment variable. If that is not
set the datastore config ``"namespace"`` field will be checked. A default
value of "lsst.dm" is used if no other value can be obtained.
"""

extra_fields: dict[str, str | int | float] | None
"""Extra key/value pairs that should be passed along with the metric
when storing in Sasquatch.
Extra fields can be obtained both from the ``$SASQUATCH_EXTRAS``
environment variable and the `"extra_fields"` entry in the datastore
config. The two sources of information are merged with the environment
variable taking priority. The environment variable must have the form of
"k1=v1;k2=v2".
"""

def __init__(
Expand All @@ -101,7 +118,20 @@ def __init__(

self.accessToken = self.config.get("accessToken", "na")

self.namespace = self.config.get("namespace", "lsst.dm")
self.namespace = os.environ.get(
"DAF_BUTLER_SASQUATCH_NAMESPACE", # Prioritize the environment
self.config.get(
"namespace", # Fallback to datastore config
"lsst.dm",
),
)

extra_fields: dict[str, str | int | float] | None = self.config.get("extra_fields", {})
if extras_str := os.environ.get("SASQUATCH_EXTRAS"):
for item in extras_str.split(";"):
k, v = item.split("=")
extra_fields[k] = v
self.extra_fields = extra_fields if extra_fields else None

self._dispatcher = SasquatchDispatcher(self.restProxyUrl, self.accessToken, self.namespace)

Expand All @@ -123,7 +153,7 @@ def bridge(self) -> DatastoreRegistryBridge:

def put(self, inMemoryDataset: Any, ref: DatasetRef) -> None:
if self.constraints.isAcceptable(ref):
self._dispatcher.dispatchRef(inMemoryDataset, ref)
self._dispatcher.dispatchRef(inMemoryDataset, ref, extraFields=self.extra_fields)
else:
log.debug("Could not put dataset type %s with Sasquatch datastore", ref.datasetType)
raise DatasetTypeNotSupportedError(
Expand Down

0 comments on commit df12eed

Please sign in to comment.