[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/cmrfrd/feast/master/sdk/python/feast/usage.py [Back]  [Original]

# 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, event) -> bool:
        raise NotImplementedError

    @property
    def priority(self):
        return 0


class AlwaysSampler(Sampler):
    def should_record(self, event) -> bool:
        return True


class RatioSampler(Sampler):
    MAX_COUNTER = (1  ctx.sampler.priority else ctx.sampler
                )

                if not ctx.call_stack:
                    # we reached the root of the stack
                    _context.set(UsageContext())  # reset context to default values
                    _produce_event(ctx)

        return wrapper

    if args:
        return decorator(args[0])

    return decorator


def log_exceptions(*args, **attrs):
    """
    Function decorator that track errors and send them to Feast Developers
    """

    def decorator(func):
        if not _is_enabled:
            return func

        @wraps(func)
        def wrapper(*args, **kwargs):
            if _context.get().call_stack:
                # we're already inside usage context
                # let it handle exception
                return func(*args, **kwargs)

            fn_call = FnCall(
                id=uuid.uuid4().hex, fn_name=_fn_fullname(func), start=datetime.utcnow()
            )
            try:
                return func(*args, **kwargs)
            except Exception:
                _, exc, traceback = sys.exc_info()

                fn_call.end = datetime.utcnow()

                ctx = UsageContext()
                ctx.exception = exc
                ctx.traceback = _trace_to_log(traceback)
                ctx.attributes = attrs
                ctx.completed_calls.append(fn_call)
                _produce_event(ctx)

                if traceback:
                    raise exc.with_traceback(traceback)

                raise exc

        return wrapper

    if args:
        return decorator(args[0])

    return decorator


def set_usage_attribute(name, value):
    """
    Extend current context with custom attribute
    """
    ctx = _context.get()
    ctx.attributes[name] = value


def _trim_filename(filename: str) -> 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

Web Proxy Viewer  |  New URL  |  Original Page