# -*- coding: utf-8 -*-
import json
import sys

from aws_xray_sdk.core import patch_all
from aws_xray_sdk.core import xray_recorder as xray
# Patch all supported libraries for X-Ray - More info: https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-python-patching.html


def xray_init():
    patch_all()


def put_metadata(key, value):
    sub = xray.current_subsegment()
    size = 0
    try:
        size = sys.getsizeof(json.dumps(value))
        if size < 64000:
            sub.put_metadata(key, value)
        else:
            sub.put_metadata(key, f'exceeded_64_kb_segment_size: {size}')
    except Exception as e:
        sub.put_metadata(key, f'cannot_determine_segment_size: {e}')


def annotate(key, value):
    sub = xray.current_subsegment()
    sub.put_annotation(key, value)
