-
Notifications
You must be signed in to change notification settings - Fork 38
feat: Add Spyne instrumentation #706
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2ab9c41
feat: Add instrumentation to Spyne
GSVarsha 41a3338
test(spyne): Added initial tests for spyne
GSVarsha 01b120e
instrumentation(spyne): Handle errors and support custom headers
GSVarsha 087b01b
test(spyne): Add tests for error handling and custom headers
GSVarsha ddd8c4a
chore(spyne): Add spyne to requirements
GSVarsha 244d67e
chore(spyne): Handle repetitive code
GSVarsha 4ed457c
style(spyne): Add typehints
GSVarsha 9220dd1
spyne: Add support for UDC
GSVarsha 43e8cac
spyne: rpc-server adaptation
GSVarsha File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| # (c) Copyright IBM Corp. 2025 | ||
|
|
||
| try: | ||
| import spyne | ||
| import wrapt | ||
| from typing import TYPE_CHECKING, Dict, Any, Callable, Tuple, Iterable, Type, Optional | ||
|
|
||
| from types import SimpleNamespace | ||
|
|
||
| from instana.log import logger | ||
| from instana.singletons import agent, tracer | ||
| from instana.propagators.format import Format | ||
| from instana.util.secrets import strip_secrets_from_query | ||
|
|
||
| if TYPE_CHECKING: | ||
| from instana.span.span import InstanaSpan | ||
| from spyne.application import Application | ||
| from spyne.server.wsgi import WsgiApplication | ||
|
|
||
| def set_span_attributes(span: "InstanaSpan", headers: Dict[str, Any]) -> None: | ||
| if "PATH_INFO" in headers: | ||
| span.set_attribute("rpc.call", headers["PATH_INFO"]) | ||
| if "QUERY_STRING" in headers and len(headers["QUERY_STRING"]): | ||
| scrubbed_params = strip_secrets_from_query( | ||
| headers["QUERY_STRING"], | ||
| agent.options.secrets_matcher, | ||
| agent.options.secrets_list, | ||
| ) | ||
| span.set_attribute("rpc.params", scrubbed_params) | ||
| if "REMOTE_ADDR" in headers: | ||
| span.set_attribute("rpc.host", headers["REMOTE_ADDR"]) | ||
| if "SERVER_PORT" in headers: | ||
| span.set_attribute("rpc.port", headers["SERVER_PORT"]) | ||
|
|
||
| def record_error(span: "InstanaSpan", response_string: str, error: Optional[Type[Exception]]) -> None: | ||
| resp_code = int(response_string.split()[0]) | ||
|
|
||
| if 500 <= resp_code: | ||
| span.record_exception(error) | ||
|
|
||
|
|
||
| @wrapt.patch_function_wrapper("spyne.server.wsgi", "WsgiApplication.handle_error") | ||
| def handle_error_with_instana( | ||
| wrapped: Callable[..., Iterable[object]], | ||
| instance: "WsgiApplication", | ||
| args: Tuple[object], | ||
| kwargs: Dict[str, Any], | ||
| ) -> Iterable[object]: | ||
| ctx = args[0] | ||
|
|
||
| # span created inside process_request() will be handled by finalize() method | ||
| if ctx.udc and ctx.udc.span: | ||
| return wrapped(*args, **kwargs) | ||
|
|
||
| headers = ctx.transport.req_env | ||
| span_context = tracer.extract(Format.HTTP_HEADERS, headers) | ||
|
|
||
| with tracer.start_as_current_span("rpc-server", span_context=span_context) as span: | ||
| set_span_attributes(span, headers) | ||
|
|
||
| response_headers = ctx.transport.resp_headers | ||
|
|
||
| tracer.inject(span.context, Format.HTTP_HEADERS, response_headers) | ||
|
|
||
| response = wrapped(*args, **kwargs) | ||
|
|
||
| record_error(span, ctx.transport.resp_code, ctx.in_error or ctx.out_error) | ||
| return response | ||
|
|
||
| @wrapt.patch_function_wrapper( | ||
| "spyne.server.wsgi", "WsgiApplication._WsgiApplication__finalize" | ||
| ) | ||
| def finalize_with_instana( | ||
| wrapped: Callable[..., Tuple[()]], | ||
| instance: "WsgiApplication", | ||
| args: Tuple[object], | ||
| kwargs: Dict[str, Any], | ||
| ) -> Tuple[()]: | ||
| ctx = args[0] | ||
| response_string = ctx.transport.resp_code | ||
|
|
||
| if ctx.udc and ctx.udc.span and response_string: | ||
| span = ctx.udc.span | ||
| record_error(span, response_string, ctx.in_error or ctx.out_error) | ||
| if span.is_recording(): | ||
| span.end() | ||
|
|
||
| ctx.udc.span = None | ||
| return wrapped(*args, **kwargs) | ||
|
GSVarsha marked this conversation as resolved.
Outdated
|
||
|
|
||
| @wrapt.patch_function_wrapper("spyne.application", "Application.process_request") | ||
| def process_request_with_instana( | ||
| wrapped: Callable[..., None], | ||
| instance: "Application", | ||
| args: Tuple[object], | ||
| kwargs: Dict[str, Any], | ||
| ) -> None: | ||
|
GSVarsha marked this conversation as resolved.
Outdated
|
||
| ctx = args[0] | ||
| headers = ctx.transport.req_env | ||
| span_context = tracer.extract(Format.HTTP_HEADERS, headers) | ||
|
|
||
| with tracer.start_as_current_span( | ||
| "rpc-server", | ||
| span_context=span_context, | ||
| end_on_exit=False, | ||
| ) as span: | ||
| set_span_attributes(span, headers) | ||
|
|
||
| response = wrapped(*args, **kwargs) | ||
| response_headers = ctx.transport.resp_headers | ||
|
|
||
| tracer.inject(span.context, Format.HTTP_HEADERS, response_headers) | ||
|
|
||
| ## Store the span in the user defined context object offered by Spyne | ||
| if ctx.udc: | ||
| ctx.udc.span = span | ||
| else: | ||
| ctx.udc = SimpleNamespace() | ||
| ctx.udc.span = span | ||
| return response | ||
|
GSVarsha marked this conversation as resolved.
Outdated
|
||
|
|
||
| logger.debug("Instrumenting Spyne") | ||
|
|
||
| except ImportError: | ||
| pass | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| # (c) Copyright IBM Corp. 2025 | ||
|
|
||
| import os | ||
| from tests.apps.spyne_app.app import spyne_server as server | ||
| from tests.apps.utils import launch_background_thread | ||
|
|
||
| app_thread = None | ||
|
|
||
| if not os.environ.get('CASSANDRA_TEST') and app_thread is None: | ||
| app_thread = launch_background_thread(server.serve_forever, "Spyne") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| #!/usr/bin/env python | ||
| # -*- coding: utf-8 -*- | ||
|
|
||
| # (c) Copyright IBM Corp. 2025 | ||
|
|
||
| import logging | ||
|
|
||
| from wsgiref.simple_server import make_server | ||
| from spyne import Application, rpc, ServiceBase, Iterable, UnsignedInteger, \ | ||
| String, Unicode | ||
|
|
||
| from spyne.protocol.json import JsonDocument | ||
| from spyne.protocol.http import HttpRpc | ||
| from spyne.server.wsgi import WsgiApplication | ||
|
|
||
| from spyne.error import ResourceNotFoundError | ||
|
|
||
| from tests.helpers import testenv | ||
|
|
||
| logging.basicConfig(level=logging.WARNING) | ||
| logger = logging.getLogger(__name__) | ||
|
|
||
| testenv["spyne_port"] = 10818 | ||
| testenv["spyne_server"] = ("http://127.0.0.1:" + str(testenv["spyne_port"])) | ||
|
|
||
| class HelloWorldService(ServiceBase): | ||
| @rpc(String, UnsignedInteger, _returns=Iterable(String)) | ||
| def say_hello(ctx, name, times): | ||
| """ | ||
| :param name: The name to say hello to | ||
| :param times: The number of times to say hello | ||
|
|
||
| :returns: An array of 'Hello, <name>' strings, repeated <times> times. | ||
| """ | ||
|
|
||
| for i in range(times): | ||
| yield 'Hello, %s' % name | ||
|
|
||
| @rpc(_returns=Unicode) | ||
| def hello(ctx): | ||
| return "<center><h1>🐍 Hello Stan! 🦄</h1></center>" | ||
|
|
||
| @rpc(_returns=Unicode) | ||
| def response_headers(ctx): | ||
| ctx.transport.add_header("X-Capture-This", "this") | ||
| ctx.transport.add_header("X-Capture-That", "that") | ||
| return "Stan wuz here with headers!" | ||
|
|
||
| @rpc(UnsignedInteger) | ||
| def custom_404(ctx, user_id): | ||
| raise ResourceNotFoundError(user_id) | ||
|
|
||
| @rpc() | ||
| def exception(ctx): | ||
| raise Exception('fake error') | ||
|
|
||
|
|
||
| application = Application([HelloWorldService], 'instana.spyne.service.helloworld', | ||
| in_protocol=HttpRpc(validator='soft'), | ||
| out_protocol=JsonDocument(ignore_wrappers=True), | ||
| ) | ||
| wsgi_app = WsgiApplication(application) | ||
| spyne_server = make_server('127.0.0.1', testenv["spyne_port"], wsgi_app) | ||
|
|
||
| if __name__ == '__main__': | ||
| spyne_server.request_queue_size = 20 | ||
| spyne_server.serve_forever() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.