def task(self, name): with self.tracer.start_as_current_span(name): context.set_value("say", "bar")
def _instrumented_requests_call(method: str, url: str, call_wrapped, get_or_create_headers): if context.get_value("suppress_instrumentation") or context.get_value( _SUPPRESS_REQUESTS_INSTRUMENTATION_KEY): return call_wrapped() # See # https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/semantic_conventions/http.md#http-client method = method.upper() span_name = "HTTP {}".format(method) exception = None recorder = RequestsInstrumentor().metric_recorder labels = {} labels["http.method"] = method labels["http.url"] = url with get_tracer(__name__, __version__, tracer_provider).start_as_current_span( span_name, kind=SpanKind.CLIENT) as span: with recorder.record_duration(labels): if span.is_recording(): span.set_attribute("component", "http") span.set_attribute("http.method", method) span.set_attribute("http.url", url) headers = get_or_create_headers() propagators.inject(type(headers).__setitem__, headers) token = context.attach( context.set_value(_SUPPRESS_REQUESTS_INSTRUMENTATION_KEY, True)) try: result = call_wrapped() # *** PROCEED except Exception as exc: # pylint: disable=W0703 exception = exc result = getattr(exc, "response", None) finally: context.detach(token) if exception is not None and span.is_recording(): span.set_status( Status(_exception_to_canonical_code(exception))) span.record_exception(exception) if result is not None: if span.is_recording(): span.set_attribute("http.status_code", result.status_code) span.set_attribute("http.status_text", result.reason) span.set_status( Status( http_status_to_canonical_code( result.status_code))) labels["http.status_code"] = str(result.status_code) labels["http.status_text"] = result.reason if result.raw and result.raw.version: labels["http.flavor"] = (str(result.raw.version)[:1] + "." + str(result.raw.version)[:-1]) if span_callback is not None: span_callback(span, result) if exception is not None: raise exception.with_traceback(exception.__traceback__) return result
def _instrumented_open_call(opener, request, call_wrapped, get_or_create_headers): # pylint: disable=too-many-locals if context.get_value("suppress_instrumentation") or context.get_value( _SUPPRESS_URLLIB_INSTRUMENTATION_KEY): return call_wrapped() method = request.get_method().upper() url = request.full_url span_name = "" if name_callback is not None: span_name = name_callback(method, url) if not span_name or not isinstance(span_name, str): span_name = get_default_span_name(method) recorder = URLLibInstrumentor().metric_recorder labels = { "http.method": method, "http.url": url, } with get_tracer(__name__, __version__, tracer_provider).start_as_current_span( span_name, kind=SpanKind.CLIENT) as span: exception = None with recorder.record_client_duration(labels): if span.is_recording(): span.set_attribute("component", "http") span.set_attribute("http.method", method) span.set_attribute("http.url", url) headers = get_or_create_headers() propagators.inject(type(headers).__setitem__, headers) token = context.attach( context.set_value(_SUPPRESS_URLLIB_INSTRUMENTATION_KEY, True)) try: result = call_wrapped() # *** PROCEED except Exception as exc: # pylint: disable=W0703 exception = exc result = getattr(exc, "file", None) finally: context.detach(token) if result is not None: code_ = result.getcode() labels["http.status_code"] = str(code_) if span.is_recording(): span.set_attribute("http.status_code", code_) span.set_attribute("http.status_text", result.reason) span.set_status( Status(http_status_to_status_code(code_))) ver_ = str(getattr(result, "version", "")) if ver_: labels["http.flavor"] = "{}.{}".format( ver_[:1], ver_[:-1]) if span_callback is not None: span_callback(span, result) if exception is not None: raise exception.with_traceback(exception.__traceback__) return result
def on_end(self, span): token = attach(set_value("suppress_instrumentation", True)) self.export_status.append(self.span_exporter.export((span,))) detach(token)
async def suppressed_request(server: aiohttp.test_utils.TestServer): async with aiohttp.test_utils.TestClient(server) as client: token = context.attach( context.set_value("suppress_instrumentation", True)) await client.get(TestAioHttpClientInstrumentor.URL) context.detach(token)
def with_distributed_context(dctx: DistributedContext, context: typing.Optional[Context] = None) -> None: attach(set_value(_DISTRIBUTED_CONTEXT_KEY, dctx, context=context))
def __init__( self, manager: "ScopeManagerShim", span: SpanShim, span_cm=None ): super().__init__(manager, span) self._span_cm = span_cm self._token = attach(set_value("scope_shim", self))
async def suppressed_request(server: aiohttp.test_utils.TestServer): async with aiohttp.test_utils.TestClient(server) as client: token = context.attach( context.set_value(_SUPPRESS_INSTRUMENTATION_KEY, True)) await client.get(TestAioHttpClientInstrumentor.URL) context.detach(token)
def _instrumented_open_call( _, request, call_wrapped, get_or_create_headers ): # pylint: disable=too-many-locals if context.get_value( _SUPPRESS_INSTRUMENTATION_KEY ) or context.get_value(_SUPPRESS_HTTP_INSTRUMENTATION_KEY): return call_wrapped() method = request.get_method().upper() url = request.full_url span_name = f"HTTP {method}".strip() url = remove_url_credentials(url) labels = { SpanAttributes.HTTP_METHOD: method, SpanAttributes.HTTP_URL: url, } with tracer.start_as_current_span( span_name, kind=SpanKind.CLIENT ) as span: exception = None if callable(request_hook): request_hook(span, request) if span.is_recording(): span.set_attribute(SpanAttributes.HTTP_METHOD, method) span.set_attribute(SpanAttributes.HTTP_URL, url) headers = get_or_create_headers() inject(headers) token = context.attach( context.set_value(_SUPPRESS_HTTP_INSTRUMENTATION_KEY, True) ) try: result = call_wrapped() # *** PROCEED except Exception as exc: # pylint: disable=W0703 exception = exc result = getattr(exc, "file", None) finally: context.detach(token) if result is not None: code_ = result.getcode() labels[SpanAttributes.HTTP_STATUS_CODE] = str(code_) if span.is_recording(): span.set_attribute(SpanAttributes.HTTP_STATUS_CODE, code_) span.set_status(Status(http_status_to_status_code(code_))) ver_ = str(getattr(result, "version", "")) if ver_: labels[ SpanAttributes.HTTP_FLAVOR ] = f"{ver_[:1]}.{ver_[:-1]}" if callable(response_hook): response_hook(span, request, result) if exception is not None: raise exception.with_traceback(exception.__traceback__) return result
def set_span_in_context(span: trace_api.Span, context: Optional[Context] = None) -> Context: ctx = set_value(SPAN_KEY, span, context=context) return ctx
def _instrumented_requests_call( method: str, url: str, call_wrapped, get_or_create_headers ): if context.get_value("suppress_instrumentation") or context.get_value( _SUPPRESS_REQUESTS_INSTRUMENTATION_KEY ): return call_wrapped() # See # https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/http.md#http-client method = method.upper() span_name = "" if name_callback is not None: span_name = name_callback(method, url) if not span_name or not isinstance(span_name, str): span_name = get_default_span_name(method) labels = {} labels["http.method"] = method labels["http.url"] = url with get_tracer( __name__, __version__, tracer_provider ).start_as_current_span(span_name, kind=SpanKind.CLIENT) as span: exception = None if span.is_recording(): span.set_attribute("http.method", method) span.set_attribute("http.url", url) headers = get_or_create_headers() inject(headers) token = context.attach( context.set_value(_SUPPRESS_REQUESTS_INSTRUMENTATION_KEY, True) ) try: result = call_wrapped() # *** PROCEED except Exception as exc: # pylint: disable=W0703 exception = exc result = getattr(exc, "response", None) finally: context.detach(token) if isinstance(result, Response): if span.is_recording(): span.set_attribute("http.status_code", result.status_code) span.set_attribute("http.status_text", result.reason) span.set_status( Status(http_status_to_status_code(result.status_code)) ) labels["http.status_code"] = str(result.status_code) if result.raw and result.raw.version: labels["http.flavor"] = ( str(result.raw.version)[:1] + "." + str(result.raw.version)[:-1] ) if span_callback is not None: span_callback(span, result) if exception is not None: raise exception.with_traceback(exception.__traceback__) return result
def do_work() -> None: context.attach(context.set_value("say", "bar"))