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

Porting redis instrumentation from contrib repo #595

Merged
merged 39 commits into from
Apr 27, 2020

Conversation

codeboten
Copy link
Contributor

Porting the existing redis instrumentation from the contrib repo to using the OpenTelemetry API and the OpenTelemetry Auto-instrumentation Instrumentor interface.

Similiar to the sqlalchemy PR, the main thing that will need updating is to remove the patch/unpatch methods once the instrumentor interface changes have been merged.

This is replacing open-telemetry/opentelemetry-python-contrib#21

@codeboten codeboten added the instrumentation Related to the instrumentation of third party libraries or frameworks label Apr 17, 2020
Copy link
Member

@mauriciovasquezbernal mauriciovasquezbernal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very minor details.
I only concerned about the functional tests, I'm not 100% convinced we should we all functional testing in the same place.

tox.ini Outdated Show resolved Hide resolved
@@ -23,4 +23,8 @@ services:
POSTGRES_USER: testuser
POSTGRES_PASSWORD: testpassword
POSTGRES_DB: opentelemetry-tests
otredis:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if it is a really good approach to keep the tests of the different frameworks under the same test. What about if I only want to test redis?
Is there a technical limitation to do that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's no technical limitation. it's not the path currently but we can split them if it makes more sense (i suspect as we have more integration tests, we will need to do this to run tests in parallel)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Besides parallel tests, there is also the case when you only want to run a specific test without downloading and starting the other containers that you don't need.
This could be a little bit complicated to set up, so we can ignore and handle in a follow up PR.

@codeboten codeboten added the needs reviewers PRs with this label are ready for review and needs people to review to move forward. label Apr 20, 2020
@mauriciovasquezbernal
Copy link
Member

I forgot to say, an entry in the documentation is probably missing as well.

Copy link
Member

@mauriciovasquezbernal mauriciovasquezbernal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's good to go. Just minor details on the comments, we can ignore the issue about putting all functional tests together and handle it later on.

I installed it, used docker-compose up in the integration tests folder and run the following script with the auto instrumentation command. I was able to see one span was printed to the console.

opentelemetry-auto-instrumentation redisexample.py

# configure otel related things
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import (
    BatchExportSpanProcessor,
    ConsoleSpanExporter,
)

tracer_provider = TracerProvider()
trace.set_tracer_provider(tracer_provider)
exporter = ConsoleSpanExporter()
span_processor = BatchExportSpanProcessor(exporter)
tracer_provider.add_span_processor(span_processor)

# actual example starts here
import random
import redis
random.seed(444)
hats = {f"hat:{random.getrandbits(32)}": i for i in (
    {
        "color": "black",
        "price": 49.99,
        "style": "fitted",
        "quantity": 1000,
        "npurchased": 0,
    },
    {
        "color": "maroon",
        "price": 59.99,
        "style": "hipster",
        "quantity": 500,
        "npurchased": 0,
    },
    {
        "color": "green",
        "price": 99.99,
        "style": "baseball",
        "quantity": 200,
        "npurchased": 0,
    })
}

r = redis.Redis(db=1)

with r.pipeline() as pipe:
  for h_id, hat in hats.items():
    pipe.hmset(h_id, hat)
  pipe.execute()

@mauriciovasquezbernal
Copy link
Member

Regarding integration tests, shouldn't a check for redis be added to ext/opentelemetry-ext-docker-tests/tests/check_availability.py?

@codeboten
Copy link
Contributor Author

Yes, it probably should. I didn't add it because in practice redis is basically available as soon as the container's up

trace.set_tracer_provider(TracerProvider())

# You can patch redis specifically
RedisInstrumentor().instrument(tracer_provider=trace.get_tracer_provider())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the general idea is to omit the tracer_provider parameter in the basic examples and let it as an advance use case. By default all the integrations should default to trace.get_tracer_provider() when the tracer provider is not passed.

Copy link
Member

@c24t c24t left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see this is the first package to use opentelemetry-instrumentation- for the package/distribution name. Is there a discussion for this somewhere? I see #283, but that's months out of date.

@codeboten
Copy link
Contributor Author

I see there's a mention of this in the SIG notes from 04-09 https://docs.google.com/document/d/1CIMGoIOZ-c3-igzbd6_Pnxx1SjAkjwqoYSUWxPY8XIs/view#heading=h.ul61m6gnh8sm but nothing else. I can change it back if that's preferable.

@c24t
Copy link
Member

c24t commented Apr 24, 2020

I can change it back if that's preferable.

Only a slight preference as far as I'm concerned. I'd like to keep the names consistent and either rename all the instrumentation-extensions to -instrumentation or none of them.

@codeboten
Copy link
Contributor Author

Only a slight preference as far as I'm concerned. I'd like to keep the names consistent and either rename all the instrumentation-extensions to -instrumentation or none of them.

I renamed it to ext

Copy link
Member

@c24t c24t left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines 18 to 19
There are two options for instrumenting code. The first option is to use
the `opentelemetry-auto-instrumentation` executable which will automatically
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
There are two options for instrumenting code. The first option is to use
the `opentelemetry-auto-instrumentation` executable which will automatically
There are two options for instrumenting code. The first option is to use the
``opentelemetry-auto-instrumentation`` executable which will automatically

To fix the sphinx target

super().setUp()
self.redis_client = redis.Redis(port=6379)
self.redis_client.flushall()
RedisInstrumentor().instrument(tracer_provider=self.tracer_provider)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You want to call instrument/uninstrument once per test instead of once for the class?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this way its not instrumenting the call to flushall which resets the redis data after each test.

Copy link
Contributor

@ocelotl ocelotl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved! Just some minor nits/suggestions. 👍

ext/opentelemetry-ext-redis/tests/test_redis.py Outdated Show resolved Hide resolved
ext/opentelemetry-ext-redis/tests/test_redis.py Outdated Show resolved Hide resolved
ext/opentelemetry-ext-redis/tests/test_redis.py Outdated Show resolved Hide resolved
Alex Boten and others added 2 commits April 24, 2020 16:48
Copy link
Member

@toumorokoshi toumorokoshi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding the "instrumentation" convention, here's the PR for that:

open-telemetry/opentelemetry-specification#539

I believe it will be approved, just a matter of some work on my part to fix it up :)

Although if we're going to do this, I wonder if we just shouldn't rename them all at once.

)

self.assertEqual(span.attributes.get("db.instance"), 0)
self.assertEqual(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it worth refactoring some of these assertions into a utility method? there's a lot of identical code with regards to db.instance / url.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good call, refactored.

),
"db.instance": conn_kwargs["db"] or 0,
}
except KeyError:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it worth logging this condition? I guess this is just a quick way of writing that if any params or missing, don't include any of these.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure about logging there, i haven't looked into what conditions would cause those keys to be missing

""" Transform redis conn info into dict """
try:
return {
"db.type": "redis",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be added regardless?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, updated the code to set available attributes

toumorokoshi and others added 5 commits April 26, 2020 14:48
Exporter and span handling complexity was increasing due to the
Span.parent attribute being either a Span or a SpanContext.

As there is no requirement in the specification to have the parent
attribute be a Span, removing this complexity and handling.

Co-authored-by: Chris Kleinknecht <[email protected]>
Co-authored-by: Alex Boten <[email protected]>
@toumorokoshi toumorokoshi merged commit a832bb0 into open-telemetry:master Apr 27, 2020
srikanthccv pushed a commit to srikanthccv/opentelemetry-python that referenced this pull request Nov 1, 2020
* feat(xml-http-request): new plugin for auto instrumentation for xml-http-request

* chore: new example for xml-http-request and updated examples structure for web

* chore: updating readme

* chore: linting

* chore: fixing origin for tests

* chore: linting

* chore: updating to use b3 format from core

* chore: updates after reviews

* chore: wrong function call

* chore: updating attribute names

* chore: linting

* chore: adding preflight requests, fixing few other issues

* chore: adding image to examples, updating readme

* chore: forcing async to be true, but propagate rest params

* chore: fixing type for open and send function

* chore: fixing format for headers

* chore: reviews

* chore: decrement task count when span exists

* chore: changes after review

* chore: adding weakmap for keeping information connected with xhr

* chore: renaming config param

* chore: not needed cast

* chore: updating title

* chore: refactored xhr, removed tracing dependency, few other issues fixed

* chore: reviews

* chore: refactored for collecting timing resources

* chore: fixes after merging

* chore: reviews

* chore: reviews

Co-authored-by: Mayur Kale <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
instrumentation Related to the instrumentation of third party libraries or frameworks needs reviewers PRs with this label are ready for review and needs people to review to move forward.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants