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

Implement missing context methods #363

Merged
merged 3 commits into from
Apr 7, 2021
Merged

Conversation

ocelotl
Copy link
Contributor

@ocelotl ocelotl commented Mar 3, 2021

Fixes #321

Description

I am not an expert in gRPC, but I think this should fix this issue. I did this:

  1. I created a virtual environment with this:
backcall==0.2.0
decorator==4.4.2
grpcio==1.36.0
grpcio-tools==1.36.0
ipdb==0.13.5
ipython==7.21.0
ipython-genutils==0.2.0
jedi==0.18.0
-e git+git@github.com:ocelotl/opentelemetry-python.git@9bf28fb451a85fd9e9a4f2276c3eebd484e55d02#egg=opentelemetry_api&subdirectory=opentelemetry-api
-e git+git@github.com:ocelotl/opentelemetry-python.git@9bf28fb451a85fd9e9a4f2276c3eebd484e55d02#egg=opentelemetry_instrumentation&subdirectory=opentelemetry-instrumentation
-e git+git@github.com:ocelotl/opentelemetry-python-contrib.git@88d8d6e9cab0ce970758ff6acd256f637ebbc368#egg=opentelemetry_instrumentation_grpc&subdirectory=instrumentation/opentelemetry-instrumentation-grpc
-e git+git@github.com:ocelotl/opentelemetry-python.git@9bf28fb451a85fd9e9a4f2276c3eebd484e55d02#egg=opentelemetry_sdk&subdirectory=opentelemetry-sdk
parso==0.8.1
pexpect==4.8.0
pickleshare==0.7.5
prompt-toolkit==3.0.16
protobuf==3.15.3
ptyprocess==0.7.0
Pygments==2.8.0
six==1.15.0
traitlets==5.0.5
wcwidth==0.2.5
wrapt==1.12.1
  1. I cloned the gRPC repo as per this
  2. I created this 2 files in .../grpc/examples/python/helloworld (both these files are pretty much copy and paste of this:

server.py

import logging
from concurrent import futures

import grpc

from opentelemetry import trace
from opentelemetry.instrumentation.grpc import GrpcInstrumentorServer
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import (
    ConsoleSpanExporter,
    SimpleSpanProcessor,
)

import helloworld_pb2
import helloworld_pb2_grpc

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

grpc_server_instrumentor = GrpcInstrumentorServer()
grpc_server_instrumentor.instrument()


class Greeter(helloworld_pb2_grpc.GreeterServicer):
    def SayHello(self, request, context):
        return helloworld_pb2.HelloReply(message="Hello, %s!" % request.name)


def serve():

    server = grpc.server(futures.ThreadPoolExecutor())

    helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server)
    server.add_insecure_port("[::]:50051")
    server.start()
    server.wait_for_termination()


if __name__ == "__main__":
    logging.basicConfig()
    serve()

client.py

import logging

import grpc

from opentelemetry import trace
from opentelemetry.instrumentation.grpc import GrpcInstrumentorClient
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import (
    ConsoleSpanExporter,
    SimpleSpanProcessor,
)

import helloworld_pb2
import helloworld_pb2_grpc

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

instrumentor = GrpcInstrumentorClient().instrument()


def run():
    with grpc.insecure_channel("localhost:50051") as channel:

        stub = helloworld_pb2_grpc.GreeterStub(channel)
        response = stub.SayHello(helloworld_pb2.HelloRequest(name="YOU"))

    print("Greeter client received: " + response.message)


if __name__ == "__main__":
    logging.basicConfig()
    run()

I ran python server.py and python client.py in separate consoles with the activated virtual environment before and I got this output:

python server.py

{
    "name": "/helloworld.Greeter/SayHello",
    "context": {
        "trace_id": "0xe45b7b876aa4d9651e67e6b09050ef6e",
        "span_id": "0x5ef0d068dbf136f9",
        "trace_state": "[]"
    },
    "kind": "SpanKind.SERVER",
    "parent_id": "0xe7dbf06ec1c87611",
    "start_time": "2021-03-03T02:56:16.674987Z",
    "end_time": "2021-03-03T02:56:16.675061Z",
    "status": {
        "status_code": "UNSET"
    },
    "attributes": {
        "rpc.system": "grpc",
        "rpc.grpc.status_code": 0,
        "rpc.method": "SayHello",
        "rpc.service": "helloworld.Greeter",
        "rpc.user_agent": "grpc-python/1.36.0 grpc-c/15.0.0 (linux; chttp2)",
        "net.peer.ip": "[::1]",
        "net.peer.port": "35030",
        "net.peer.name": "localhost"
    },
    "events": [],
    "links": [],
    "resource": {
        "telemetry.sdk.language": "python",
        "telemetry.sdk.name": "opentelemetry",
        "telemetry.sdk.version": "1.0.0.dev0",
        "service.name": "unknown_service"
    }
}

python client.py

{
    "name": "/helloworld.Greeter/SayHello",
    "context": {
        "trace_id": "0xe45b7b876aa4d9651e67e6b09050ef6e",
        "span_id": "0xe7dbf06ec1c87611",
        "trace_state": "[]"
    },
    "kind": "SpanKind.CLIENT",
    "parent_id": null,
    "start_time": "2021-03-03T02:56:16.672359Z",
    "end_time": "2021-03-03T02:56:16.676067Z",
    "status": {
        "status_code": "UNSET"
    },
    "attributes": {
        "rpc.system": "grpc",
        "rpc.grpc.status_code": 0,
        "rpc.method": "SayHello",
        "rpc.service": "helloworld.Greeter"
    },
    "events": [],
    "links": [],
    "resource": {
        "telemetry.sdk.language": "python",
        "telemetry.sdk.name": "opentelemetry",
        "telemetry.sdk.version": "1.0.0.dev0",
        "service.name": "unknown_service"
    }
}
Greeter client received: Hello, YOU!

Without these changes I would end up getting the error reported in #321 when I ran client.py.

Fixes #321

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • Test A

Does This PR Require a Core Repo Change?

  • Yes. - Link to PR:
  • No.

Checklist:

See contributing.md for styleguide, changelog guidelines, and more.

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

@ocelotl ocelotl requested a review from a team as a code owner March 3, 2021 02:58
@ocelotl ocelotl requested review from codeboten and lzchen and removed request for a team March 3, 2021 02:58
@alertedsnake
Copy link
Contributor

Nice - for some reason I thought this would be more difficult.

Copy link
Contributor

@codeboten codeboten left a comment

Choose a reason for hiding this comment

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

Thanks for fixing this!

@codeboten codeboten merged commit 3e1a9fa into open-telemetry:main Apr 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

opentelemetry-instrumentation-grpc fails if client channel is used as a context manager
3 participants