-
Notifications
You must be signed in to change notification settings - Fork 35
New logging instrumentation #128
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
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,45 @@ | ||
| from __future__ import absolute_import | ||
|
|
||
| import wrapt | ||
| import logging | ||
| import sys | ||
|
|
||
| from ..log import logger | ||
| from ..singletons import tracer | ||
|
|
||
| @wrapt.patch_function_wrapper('logging', 'Logger._log') | ||
| def log_with_instana(wrapped, instance, argv, kwargs): | ||
| # argv[0] = level | ||
| # argv[1] = message | ||
| # argv[2] = args for message | ||
| try: | ||
| parent_span = tracer.active_span | ||
|
|
||
| # Only needed if we're tracing and serious log | ||
| if parent_span and argv[0] >= logging.WARN: | ||
| # get the formatted log message | ||
| msg = argv[1] % argv[2] | ||
|
|
||
| # get additional information if an exception is being handled | ||
| parameters = None | ||
| (t, v, tb) = sys.exc_info() | ||
| if t is not None and v is not None: | ||
| parameters = '{} {}'.format(t , v) | ||
|
|
||
| # create logging span | ||
| with tracer.start_active_span('log', child_of=parent_span) as scope: | ||
| scope.span.log_kv({ 'message': msg }) | ||
| if parameters is not None: | ||
| scope.span.log_kv({ 'parameters': parameters }) | ||
| # extra tags for an error | ||
| if argv[0] >= logging.ERROR: | ||
| scope.span.set_tag('error', True) | ||
| ec = scope.span.tags.get('ec', 0) | ||
| scope.span.set_tag('ec', ec + 1) | ||
| except Exception as e: | ||
| logger.debug('Exception: %s', e, exc_info=True) | ||
| finally: | ||
| return wrapped(*argv, **kwargs) | ||
|
|
||
| logger.debug('Instrumenting logging') | ||
|
|
||
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
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,50 @@ | ||
| from __future__ import absolute_import | ||
|
|
||
| import logging | ||
| import unittest | ||
| from instana.singletons import tracer | ||
|
|
||
|
|
||
| class TestLogging(unittest.TestCase): | ||
| def setUp(self): | ||
| """ Clear all spans before a test run """ | ||
| self.recorder = tracer.recorder | ||
| self.recorder.clear_spans() | ||
| self.logger = logging.getLogger('unit test') | ||
|
|
||
| def tearDown(self): | ||
| """ Do nothing for now """ | ||
| return None | ||
|
|
||
| def test_no_span(self): | ||
| with tracer.start_active_span('test'): | ||
| self.logger.info('info message') | ||
|
|
||
|
|
||
| spans = self.recorder.queued_spans() | ||
| self.assertEqual(1, len(spans)) | ||
|
|
||
| def test_extra_span(self): | ||
| with tracer.start_active_span('test'): | ||
| self.logger.warn('foo %s', 'bar') | ||
|
|
||
| spans = self.recorder.queued_spans() | ||
| self.assertEqual(2, len(spans)) | ||
| self.assertEqual(3, spans[0].k) # intermediate kind | ||
|
|
||
| self.assertEqual('foo bar', spans[0].data.log.get('message')) | ||
|
|
||
| def test_parameters(self): | ||
| with tracer.start_active_span('test'): | ||
| try: | ||
| a = 42 | ||
| b = 0 | ||
| c = a / b | ||
| except Exception as e: | ||
| self.logger.exception('Exception: %s', str(e)) | ||
|
|
||
| spans = self.recorder.queued_spans() | ||
| self.assertEqual(2, len(spans)) | ||
|
|
||
| self.assertIsNotNone(spans[0].data.log.get('parameters')) | ||
|
|
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
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.