# Copyright 2019 The Feast Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import concurrent.futures
import contextlib
import contextvars
import dataclasses
import hashlib
import logging
import os
import platform
import sys
import typing
import uuid
from datetime import datetime
from functools import wraps
from os.path import expanduser, join
from pathlib import Path
import requests
from feast import flags_helper
from feast.constants import DEFAULT_FEAST_USAGE_VALUE, FEAST_USAGE
from feast.version import get_version
USAGE_ENDPOINT = "https://usage.feast.dev"
_logger = logging.getLogger(__name__)
_executor = concurrent.futures.ThreadPoolExecutor(max_workers=3)
_is_enabled = os.getenv(FEAST_USAGE, default=DEFAULT_FEAST_USAGE_VALUE) == "True"
_constant_attributes = {
"project_id": "",
"session_id": str(uuid.uuid4()),
"installation_id": None,
"version": get_version(),
"python_version": platform.python_version(),
"platform": platform.platform(),
"env_signature": hashlib.md5(
",".join(
sorted([k for k in os.environ.keys() if not k.startswith("FEAST")])
).encode()
).hexdigest(),
}
APPLICATION_NAME = "feast-dev/feast"
USER_AGENT = "{}/{}".format(APPLICATION_NAME, get_version())
def get_user_agent():
return USER_AGENT
def set_current_project_uuid(project_uuid: str):
_constant_attributes["project_id"] = project_uuid
@dataclasses.dataclass
class FnCall:
fn_name: str
id: str
start: datetime
end: typing.Optional[datetime] = None
parent_id: typing.Optional[str] = None
class Sampler:
def should_record(self) -> bool:
raise NotImplementedError
@property
def priority(self):
return 0
class AlwaysSampler(Sampler):
def should_record(self) -> bool:
return True
class RatioSampler(Sampler):
MAX_COUNTER = (1 str:
return filename.split("/")[-1]
def _fn_fullname(fn: typing.Callable):
return fn.__module__ + "." + fn.__qualname__
def _trace_to_log(traceback):
log = []
while traceback is not None:
log.append(
(
_trim_filename(traceback.tb_frame.f_code.co_filename),
traceback.tb_lineno,
traceback.tb_frame.f_code.co_name,
)
)
traceback = traceback.tb_next
return log