Skip to content

Commit

Permalink
Send span lines with span logs. (#96)
Browse files Browse the repository at this point in the history
* Send span lines with span logs.
  • Loading branch information
keep94 authored Sep 7, 2022
1 parent b0e8337 commit e409750
Show file tree
Hide file tree
Showing 9 changed files with 387 additions and 12 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

setuptools.setup(
name='wavefront-sdk-python',
version='1.8.7', # The version number. Update with each pull request.
version='1.8.8', # The version number. Update with each pull request.
author='Wavefront by VMware',
url='https://github.com/wavefrontHQ/wavefront-sdk-python',
license='Apache-2.0',
Expand Down
136 changes: 136 additions & 0 deletions test/test_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
"""Unit Tests for Wavefront Python SDK.
@author Travis Keep ([email protected])
"""

import unittest
import uuid
from unittest.mock import ANY
from unittest.mock import Mock
from unittest.mock import patch

import requests

from wavefront_sdk.client import WavefrontClient
from wavefront_sdk.entities.tracing.span_log import SpanLog


class TestUtils(unittest.TestCase):
"""Test direct ingestion."""

def setUp(self):
self._sender = WavefrontClient(
'no_server',
'no_token',
flush_interval_seconds=86400, # turn off auto flushing
enable_internal_metrics=False)
self._spans_log_buffer = self._sender._spans_log_buffer
self._tracing_spans_buffer = self._sender._tracing_spans_buffer
self._response = Mock()
self._response.status_code = 200

def test_send_span_with_span_logs(self):

self._sender.send_span(
'spanName',
1635123456789,
12345,
'localhost',
uuid.UUID('11111111-2222-3333-4444-555555555555'),
uuid.UUID('11111111-0000-0001-0002-123456789012'),
[uuid.UUID('55555555-4444-3333-2222-111111111111')],
None,
[('application', 'Wavefront'), ('service', 'test-spans')],
[SpanLog(
1635123789456000,
{"FooLogKey": "FooLogValue"})])

self.maxDiff = None

# Verify span logs correctly emitted
actual_line = self._spans_log_buffer.get()
expected_line = (
'{"traceId": "11111111-2222-3333-4444-555555555555", '
'"spanId": "11111111-0000-0001-0002-123456789012", '
'"logs": [{"timestamp": 1635123789456000, '
'"fields": {"FooLogKey": "FooLogValue"}}], '
'"span": "\\"spanName\\" source=\\"localhost\\" '
'traceId=11111111-2222-3333-4444-555555555555 '
'spanId=11111111-0000-0001-0002-123456789012 '
'parent=55555555-4444-3333-2222-111111111111 '
'\\"application\\"=\\"Wavefront\\" \\"service\\"=\\"test-spans\\" '
'\\"_spanLogs\\"=\\"true\\" 1635123456789 12345\\n"}\n')
self.assertEqual(expected_line, actual_line)

# Verify span correctly emitted
actual_line = self._tracing_spans_buffer.get()
expected_line = (
'"spanName" source="localhost" '
'traceId=11111111-2222-3333-4444-555555555555 '
'spanId=11111111-0000-0001-0002-123456789012 '
'parent=55555555-4444-3333-2222-111111111111 '
'"application"="Wavefront" "service"="test-spans" '
'"_spanLogs"="true" 1635123456789 12345\n')
self.assertEqual(expected_line, actual_line)

def test_send_span_without_span_logs(self):

self._sender.send_span(
'spanName',
1635123456789,
12345,
'localhost',
uuid.UUID('11111111-2222-3333-4444-555555555555'),
uuid.UUID('11111111-0000-0001-0002-123456789012'),
[uuid.UUID('55555555-4444-3333-2222-111111111111')],
None,
[('application', 'Wavefront'), ('service', 'test-spans')],
[])

self.maxDiff = None

# Assert no span logs emitted
self.assertTrue(self._spans_log_buffer.empty())

# Verify span correctly emitted
actual_line = self._tracing_spans_buffer.get()
expected_line = (
'"spanName" source="localhost" '
'traceId=11111111-2222-3333-4444-555555555555 '
'spanId=11111111-0000-0001-0002-123456789012 '
'parent=55555555-4444-3333-2222-111111111111 '
'"application"="Wavefront" "service"="test-spans" '
'1635123456789 12345\n')
self.assertEqual(expected_line, actual_line)

def test_report_event(self):

with patch.object(
requests, 'post', return_value=self._response) as mock_post:
self._sender._report(
'', self._sender.WAVEFRONT_EVENT_FORMAT, '', Mock())

mock_post.assert_called_once_with(
ANY,
params=None,
headers=ANY,
data=ANY,
timeout=self._sender.HTTP_TIMEOUT)

def test_report_non_event(self):

with patch.object(
requests, 'post', return_value=self._response) as mock_post:
self._sender._report('', 'metric', '', Mock())

mock_post.assert_called_once_with(
ANY,
params=ANY,
headers=ANY,
data=ANY,
timeout=self._sender.HTTP_TIMEOUT)


if __name__ == '__main__':
# run 'python -m unittest discover' from top-level to run tests
unittest.main()
136 changes: 136 additions & 0 deletions test/test_direct.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
"""Unit Tests for Wavefront Python SDK.
@author Travis Keep ([email protected])
"""

import unittest
import uuid
from unittest.mock import ANY
from unittest.mock import Mock
from unittest.mock import patch

import requests

from wavefront_sdk.direct import WavefrontDirectClient
from wavefront_sdk.entities.tracing.span_log import SpanLog


class TestUtils(unittest.TestCase):
"""Test direct ingestion."""

def setUp(self):
self._sender = WavefrontDirectClient(
'no_server',
'no_token',
flush_interval_seconds=86400, # turn off auto flushing
enable_internal_metrics=False)
self._spans_log_buffer = self._sender._spans_log_buffer
self._tracing_spans_buffer = self._sender._tracing_spans_buffer
self._response = Mock()
self._response.status_code = 200

def test_send_span_with_span_logs(self):

self._sender.send_span(
'spanName',
1635123456789,
12345,
'localhost',
uuid.UUID('11111111-2222-3333-4444-555555555555'),
uuid.UUID('11111111-0000-0001-0002-123456789012'),
[uuid.UUID('55555555-4444-3333-2222-111111111111')],
None,
[('application', 'Wavefront'), ('service', 'test-spans')],
[SpanLog(
1635123789456000,
{"FooLogKey": "FooLogValue"})])

self.maxDiff = None

# Verify span logs correctly emitted
actual_line = self._spans_log_buffer.get()
expected_line = (
'{"traceId": "11111111-2222-3333-4444-555555555555", '
'"spanId": "11111111-0000-0001-0002-123456789012", '
'"logs": [{"timestamp": 1635123789456000, '
'"fields": {"FooLogKey": "FooLogValue"}}], '
'"span": "\\"spanName\\" source=\\"localhost\\" '
'traceId=11111111-2222-3333-4444-555555555555 '
'spanId=11111111-0000-0001-0002-123456789012 '
'parent=55555555-4444-3333-2222-111111111111 '
'\\"application\\"=\\"Wavefront\\" \\"service\\"=\\"test-spans\\" '
'\\"_spanLogs\\"=\\"true\\" 1635123456789 12345\\n"}\n')
self.assertEqual(expected_line, actual_line)

# Verify span correctly emitted
actual_line = self._tracing_spans_buffer.get()
expected_line = (
'"spanName" source="localhost" '
'traceId=11111111-2222-3333-4444-555555555555 '
'spanId=11111111-0000-0001-0002-123456789012 '
'parent=55555555-4444-3333-2222-111111111111 '
'"application"="Wavefront" "service"="test-spans" '
'"_spanLogs"="true" 1635123456789 12345\n')
self.assertEqual(expected_line, actual_line)

def test_send_span_without_span_logs(self):

self._sender.send_span(
'spanName',
1635123456789,
12345,
'localhost',
uuid.UUID('11111111-2222-3333-4444-555555555555'),
uuid.UUID('11111111-0000-0001-0002-123456789012'),
[uuid.UUID('55555555-4444-3333-2222-111111111111')],
None,
[('application', 'Wavefront'), ('service', 'test-spans')],
[])

self.maxDiff = None

# Assert no span logs emitted
self.assertTrue(self._spans_log_buffer.empty())

# Verify span correctly emitted
actual_line = self._tracing_spans_buffer.get()
expected_line = (
'"spanName" source="localhost" '
'traceId=11111111-2222-3333-4444-555555555555 '
'spanId=11111111-0000-0001-0002-123456789012 '
'parent=55555555-4444-3333-2222-111111111111 '
'"application"="Wavefront" "service"="test-spans" '
'1635123456789 12345\n')
self.assertEqual(expected_line, actual_line)

def test_report_event(self):

with patch.object(
requests, 'post', return_value=self._response) as mock_post:
self._sender._report(
'', self._sender.WAVEFRONT_EVENT_FORMAT, '', Mock())

mock_post.assert_called_once_with(
ANY,
params=None,
headers=ANY,
data=ANY,
timeout=self._sender.HTTP_TIMEOUT)

def test_report_non_event(self):

with patch.object(
requests, 'post', return_value=self._response) as mock_post:
self._sender._report('', 'metric', '', Mock())

mock_post.assert_called_once_with(
ANY,
params=ANY,
headers=ANY,
data=ANY,
timeout=self._sender.HTTP_TIMEOUT)


if __name__ == '__main__':
# run 'python -m unittest discover' from top-level to run tests
unittest.main()
95 changes: 95 additions & 0 deletions test/test_proxy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
"""Unit Tests for Wavefront Python SDK.
@author Travis Keep ([email protected])
"""

import unittest
import uuid
from unittest.mock import Mock
from unittest.mock import call

from wavefront_sdk.common.proxy_connection_handler import (
ProxyConnectionHandler)
from wavefront_sdk.entities.tracing.span_log import SpanLog
from wavefront_sdk.proxy import WavefrontProxyClient


class TestUtils(unittest.TestCase):
"""Test proxy ingestion."""

def setUp(self):
self._sender = WavefrontProxyClient(
'no_host',
None,
None,
None,
enable_internal_metrics=False)
self._sender._tracing_proxy_connection_handler = (
Mock(spec=ProxyConnectionHandler))
self._tracing = self._sender._tracing_proxy_connection_handler

def test_send_span_with_span_logs(self):

self._sender.send_span(
'spanName',
1635123456789,
12345,
'localhost',
uuid.UUID('11111111-2222-3333-4444-555555555555'),
uuid.UUID('11111111-0000-0001-0002-123456789012'),
[uuid.UUID('55555555-4444-3333-2222-111111111111')],
None,
[('application', 'Wavefront'), ('service', 'test-spans')],
[SpanLog(
1635123789456000,
{"FooLogKey": "FooLogValue"})])

expected_span_line = (
'"spanName" source="localhost" '
'traceId=11111111-2222-3333-4444-555555555555 '
'spanId=11111111-0000-0001-0002-123456789012 '
'parent=55555555-4444-3333-2222-111111111111 '
'"application"="Wavefront" "service"="test-spans" '
'"_spanLogs"="true" 1635123456789 12345\n')
expected_span_log_line = (
'{"traceId": "11111111-2222-3333-4444-555555555555", '
'"spanId": "11111111-0000-0001-0002-123456789012", '
'"logs": [{"timestamp": 1635123789456000, '
'"fields": {"FooLogKey": "FooLogValue"}}], '
'"span": "\\"spanName\\" source=\\"localhost\\" '
'traceId=11111111-2222-3333-4444-555555555555 '
'spanId=11111111-0000-0001-0002-123456789012 '
'parent=55555555-4444-3333-2222-111111111111 '
'\\"application\\"=\\"Wavefront\\" \\"service\\"=\\"test-spans\\" '
'\\"_spanLogs\\"=\\"true\\" 1635123456789 12345\\n"}\n')
self._tracing.send_data.assert_has_calls(
[call(expected_span_line), call(expected_span_log_line)])
self.assertEqual(2, self._tracing.send_data.call_count)

def test_send_span_without_span_logs(self):

self._sender.send_span(
'spanName',
1635123456789,
12345,
'localhost',
uuid.UUID('11111111-2222-3333-4444-555555555555'),
uuid.UUID('11111111-0000-0001-0002-123456789012'),
[uuid.UUID('55555555-4444-3333-2222-111111111111')],
None,
[('application', 'Wavefront'), ('service', 'test-spans')],
[])

expected_span_line = (
'"spanName" source="localhost" '
'traceId=11111111-2222-3333-4444-555555555555 '
'spanId=11111111-0000-0001-0002-123456789012 '
'parent=55555555-4444-3333-2222-111111111111 '
'"application"="Wavefront" "service"="test-spans" '
'1635123456789 12345\n')
self._tracing.send_data.assert_called_once_with(expected_span_line)


if __name__ == '__main__':
# run 'python -m unittest discover' from top-level to run tests
unittest.main()
Loading

0 comments on commit e409750

Please sign in to comment.