Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aiohttp instrumentation: span for DNS resolution #3198

Open
dacevedo12 opened this issue Jan 20, 2025 · 1 comment
Open

aiohttp instrumentation: span for DNS resolution #3198

dacevedo12 opened this issue Jan 20, 2025 · 1 comment

Comments

@dacevedo12
Copy link

What problem do you want to solve?

aiohttp has hooks that would allow this instrumentation to report spans and/or dns related metrics.

this is useful to identify dns issues or bottlenecks on cluster instances

https://docs.aiohttp.org/en/stable/tracing_reference.html

Describe the solution you'd like

I'd like to have visibility over dns resolution for my http calls

Describe alternatives you've considered

extend the instrumentation in my own codebase

Additional Context

No response

Would you like to implement a fix?

None

@dacevedo12
Copy link
Author

In the meantime

from types import SimpleNamespace
from typing import Any

import aiohttp
from opentelemetry import context, trace
from opentelemetry.instrumentation.aiohttp_client import AioHttpClientInstrumentor
from opentelemetry.instrumentation.utils import is_instrumentation_enabled
from opentelemetry.trace import SpanKind, get_tracer


class AioHTTPInstrumentor(AioHttpClientInstrumentor):
    def _instrument(self, **kwargs: Any) -> None:
        tracer = get_tracer(__name__)

        async def on_dns_resolvehost_start(
            _session: aiohttp.ClientSession,
            trace_config_ctx: SimpleNamespace,
            params: aiohttp.TraceDnsResolveHostStartParams,
        ) -> None:
            if not is_instrumentation_enabled():
                trace_config_ctx.span = None
                return
            trace_config_ctx.span = tracer.start_span(
                "dns_resolvehost", kind=SpanKind.CLIENT, attributes={"host": params.host}
            )
            trace_config_ctx.token = context.attach(
                trace.set_span_in_context(trace_config_ctx.span)
            )

        async def on_dns_resolvehost_end(
            _session: aiohttp.ClientSession,
            trace_config_ctx: SimpleNamespace,
            _params: aiohttp.TraceDnsResolveHostEndParams,
        ) -> None:
            if trace_config_ctx.span is None:
                return
            context.detach(trace_config_ctx.token)
            trace_config_ctx.span.end()

        trace_config = aiohttp.TraceConfig()
        trace_config.on_dns_resolvehost_start.append(on_dns_resolvehost_start)
        trace_config.on_dns_resolvehost_end.append(on_dns_resolvehost_end)
        super()._instrument(**kwargs, trace_configs=[trace_config])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant