open-telemetry / open-telemetry/opentelemetry-python-contrib

Tracing from the grequests module doesn't have the correct context information

Open
#548 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

backlog bug
Dominant language
Python
Stars
1.1k
Forks
1.1k
Avg merge
4d 15h
Merged PRs (30d)
16

Description

Describe your environment
The grequests module is a wrapper around the requests module to parallelize requests. While using this module, the trace of the HTTP requests are not getting associated with the parent context. I tried with 4 parallel requests and all of them have different trace ids.

Steps to reproduce
Please see the following code snippet to reproduce the problem.

import grequests

from opentelemetry import trace
from opentelemetry.instrumentation.requests import RequestsInstrumentor
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import (
    ConsoleSpanExporter,
    SimpleSpanProcessor,
)
from opentelemetry.sdk.trace import sampling

tracer_provider = TracerProvider()
tracer_provider.sampler = sampling.ParentBased(sampling.ALWAYS_ON)

trace.set_tracer_provider(tracer_provider)
trace.get_tracer_provider().add_span_processor(
    SimpleSpanProcessor(ConsoleSpanExporter())
)

RequestsInstrumentor().instrument()

urls = [
    'http://www.heroku.com',
    'http://www.facebook.com',
    'http://www.instagram.com',
    'http://www.google.com',
]


def hello():
    tracer = trace.get_tracer(__name__)
    with tracer.start_as_current_span("example-request"):
        rs = (grequests.get(u) for u in urls)
        print(grequests.map(rs))

hello()

What is the expected behavior?
Since requests are created within the parent's context, I expect to see all requests to have the same trace id.

What is the actual behavior?

{
    "name": "HTTP GET",
    "context": {
        "trace_id": "0x4b1ec3b31c6450c0be8964c551b44c08",
        "span_id": "0x29c0d897f72a984a",
        "trace_state": "[]"
    },
    "kind": "SpanKind.CLIENT",
    "parent_id": null,
    "start_time": "2021-06-23T19:21:42.221516Z",
    "end_time": "2021-06-23T19:21:42.485323Z",
    "status": {
        "status_code": "UNSET"
    },
    "attributes": {
        "http.method": "GET",
        "http.url": "http://www.google.com",
        "http.status_code": 200,
        "http.status_text": "OK"
    },
    "events": [],
    "links": [],
    "resource": {
        "telemetry.sdk.language": "python",
        "telemetry.sdk.name": "opentelemetry",
        "telemetry.sdk.version": "1.0.0",
        "service.name": "unknown_service"
    }
}
{
    "name": "HTTP GET",
    "context": {
        "trace_id": "0x69ca27f26d5c8b03f8d55be73cb48fc4",
        "span_id": "0x56115bec426ad4af",
        "trace_state": "[]"
    },
    "kind": "SpanKind.CLIENT",
    "parent_id": null,
    "start_time": "2021-06-23T19:21:42.219740Z",
    "end_time": "2021-06-23T19:21:43.040657Z",
    "status": {
        "status_code": "UNSET"
    },
    "attributes": {
        "http.method": "GET",
        "http.url": "http://www.facebook.com",
        "http.status_code": 200,
        "http.status_text": "OK"
    },
    "events": [],
    "links": [],
    "resource": {
        "telemetry.sdk.language": "python",
        "telemetry.sdk.name": "opentelemetry",
        "telemetry.sdk.version": "1.0.0",
        "service.name": "unknown_service"
    }
}
{
    "name": "HTTP GET",
    "context": {
        "trace_id": "0x2276dda25bd767a001cd3e1c6c978684",
        "span_id": "0xcc7f68b04b3a6533",
        "trace_state": "[]"
    },
    "kind": "SpanKind.CLIENT",
    "parent_id": null,
    "start_time": "2021-06-23T19:21:42.220744Z",
    "end_time": "2021-06-23T19:21:43.443545Z",
    "status": {
        "status_code": "UNSET"
    },
    "attributes": {
        "http.method": "GET",
        "http.url": "http://www.instagram.com",
        "http.status_code": 200,
        "http.status_text": "OK"
    },
    "events": [],
    "links": [],
    "resource": {
        "telemetry.sdk.language": "python",
        "telemetry.sdk.name": "opentelemetry",
        "telemetry.sdk.version": "1.0.0",
        "service.name": "unknown_service"
    }
}
{
    "name": "HTTP GET",
    "context": {
        "trace_id": "0x990ec0f55354eedd5fc20d44419d45f9",
        "span_id": "0x048729577c83d30b",
        "trace_state": "[]"
    },
    "kind": "SpanKind.CLIENT",
    "parent_id": null,
    "start_time": "2021-06-23T19:21:42.205409Z",
    "end_time": "2021-06-23T19:21:43.943165Z",
    "status": {
        "status_code": "UNSET"
    },
    "attributes": {
        "http.method": "GET",
        "http.url": "http://www.heroku.com",
        "http.status_code": 200,
        "http.status_text": "OK"
    },
    "events": [],
    "links": [],
    "resource": {
        "telemetry.sdk.language": "python",
        "telemetry.sdk.name": "opentelemetry",
        "telemetry.sdk.version": "1.0.0",
        "service.name": "unknown_service"
    }
}
[<Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>]
{
    "name": "example-request",
    "context": {
        "trace_id": "0x8c0e7cfeb2f35ef8ac583f5ca4177b44",
        "span_id": "0x3098a0cd8e7eea49",
        "trace_state": "[]"
    },
    "kind": "SpanKind.INTERNAL",
    "parent_id": null,
    "start_time": "2021-06-23T19:21:42.196124Z",
    "end_time": "2021-06-23T19:21:43.946074Z",
    "status": {
        "status_code": "UNSET"
    },
    "attributes": {},
    "events": [],
    "links": [],
    "resource": {
        "telemetry.sdk.language": "python",
        "telemetry.sdk.name": "opentelemetry",
        "telemetry.sdk.version": "1.0.0",
        "service.name": "unknown_service"
    }
}

Additional context
Add any other context about the problem here.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the supplied grequests reproduction with RequestsInstrumentor and inspect how the requests instrumentation obtains context while grequests.map executes parallel requests. Confirm the fix by verifying that all HTTP spans share the example-request trace ID and are associated with that parent context.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
observability
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.