-
Notifications
You must be signed in to change notification settings - Fork 33
Kafka FUP Final #790
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
base: main
Are you sure you want to change the base?
Kafka FUP Final #790
Conversation
a72d810 to
ea9c383
Compare
Signed-off-by: Cagri Yonca <[email protected]>
Signed-off-by: Cagri Yonca <[email protected]>
3a5c83f to
4dd204f
Compare
|
A couple of words behind the need for the changes. Let's say we have two distributed environments: So the pseudo code is here, trace IDs are given as examples: env A - producer
env B - consumer
What we want is whether there is a parent span or not, to keep the incoming trace ID alive. So after this PR, what will happen in the example above is as follows: env B - consumer
|
| def trace_kafka_close( | ||
| wrapped: Callable[..., InstanaConfluentKafkaConsumer.close], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You have to add this function to the class InstanaConfluentKafkaConsumer to wrap it correctly.
| from instana.util.traceutils import ( | ||
| get_tracer_tuple, | ||
| tracing_is_off, | ||
| ) | ||
| from instana.span.span import InstanaSpan | ||
|
|
||
| consumer_token: Dict[str, Any] = {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this variable necessary to be a dictionary? I saw that only one value to the unique key token is assigned to it, so I think a simple variable is sufficient, don't you think?
BTW, we are not using type annotations for variables in our code.
| consumer_token: Dict[str, Any] = {} | |
| consumer_token = None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I followed the same way we used for the celery. Let me change them both
| token = context.attach(ctx) | ||
| consumer_token["token"] = token |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If changing the consumer_token variable as commented here, you can do the following:
| token = context.attach(ctx) | |
| consumer_token["token"] = token | |
| consumer_token = context.attach(ctx) |
| if "token" in consumer_token: | ||
| context.detach(consumer_token.pop("token", None)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If changing the consumer_token variable as commented here, you can do the following:
| if "token" in consumer_token: | |
| context.detach(consumer_token.pop("token", None)) | |
| if consumer_token: | |
| context.detach(consumer_token) |
|
|
||
| def clear_context() -> None: | ||
| context.attach(trace.set_span_in_context(None)) | ||
| consumer_token.clear() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If changing the consumer_token variable as commented here, you can do the following:
| consumer_token.clear() | |
| consumer_token = None |
|
|
||
| if TYPE_CHECKING: | ||
| from kafka.producer.future import FutureRecordMetadata | ||
|
|
||
| consumer_token: Dict[str, Any] = {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The same comment as here.
BTW, there was too much repetitive code in those two files. It would be interesting to see how to reduce that.
| except Exception as exc: | ||
| exception = exc | ||
| finally: | ||
| if res: | ||
| create_span("poll", res.topic(), res.headers()) | ||
| else: | ||
| create_span( | ||
| "poll", | ||
| next(iter(instance.list_topics().topics)), | ||
| exception=exception, | ||
| ) | ||
|
|
||
| return res | ||
| create_span( | ||
| "poll", | ||
| next(iter(instance.list_topics().topics)), | ||
| exception=exception, | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as commented here. Please verify the rest of the code and the kafka_python.py file to apply the same changes, including necessary tests.
| except Exception as exc: | |
| exception = exc | |
| finally: | |
| if res: | |
| create_span("poll", res.topic(), res.headers()) | |
| else: | |
| create_span( | |
| "poll", | |
| next(iter(instance.list_topics().topics)), | |
| exception=exception, | |
| ) | |
| return res | |
| create_span( | |
| "poll", | |
| next(iter(instance.list_topics().topics)), | |
| exception=exception, | |
| ) | |
| except Exception as exc_error: | |
| create_span( | |
| "poll", | |
| next(iter(instance.list_topics().topics)), | |
| exception=exc_error, | |
| ) |
Fixed trace correlation issues, aligned with other tracers through the tracer-test-suite.