async def set_context(data, key="custom"): """ Asynchronous copy of elasticapm.traces.set_context(). Attach contextual data to the current transaction and errors that happen during the current transaction. If the transaction is not sampled, this function becomes a no-op. :param data: a dictionary, or a callable that returns a dictionary :param key: the namespace for this data """ transaction = execution_context.get_transaction() if not (transaction and transaction.is_sampled): return if callable(data): data = await data() # remove invalid characters from key names for k in list(data.keys()): if LABEL_RE.search(k): data[LABEL_RE.sub("_", k)] = data.pop(k) if key in transaction.context: transaction.context[key].update(data) else: transaction.context[key] = data
def tag(self, **tags): """ This method is deprecated, please use "label()" instead. Tag this span with one or multiple key/value tags. Both the values should be strings span_obj.tag(key1="value1", key2="value2") Note that keys will be dedotted, replacing dot (.), star (*) and double quote (") with an underscore (_) :param tags: key/value pairs of tags :return: None """ for key in tags.keys(): self.labels[LABEL_RE.sub("_", compat.text_type(key))] = encoding.keyword_field(compat.text_type(tags[key]))
def enforce_label_format(labels): """ Enforces label format: * dots, double quotes or stars in keys are replaced by underscores * string values are limited to a length of 1024 characters * values can only be of a limited set of types :param labels: a dictionary of labels :return: a new dictionary with sanitized keys/values """ new = {} for key, value in compat.iteritems(labels): if not isinstance(value, LABEL_TYPES): value = keyword_field(compat.text_type(value)) new[LABEL_RE.sub("_", compat.text_type(key))] = value return new