示例#1
0
    def test_constructor_default(self):
        patch = mock.patch(
            'opencensus.trace.reporters.google_cloud_reporter.Client',
            new=_Client)

        with patch:
            reporter = google_cloud_reporter.GoogleCloudReporter()

        project_id = 'PROJECT'
        self.assertEqual(reporter.project_id, project_id)
示例#2
0
    def test_constructor_explicit(self):
        client = mock.Mock()
        project_id = 'PROJECT'
        client.project = project_id

        reporter = google_cloud_reporter.GoogleCloudReporter(
            client=client, project_id=project_id)

        self.assertIs(reporter.client, client)
        self.assertEqual(reporter.project_id, project_id)
示例#3
0
    def test_translate_to_stackdriver(self):
        project_id = 'PROJECT'
        trace = {'spans': [], 'traceId': '6e0c63257de34c92bf9efcd03927272e'}

        client = mock.Mock()
        client.project = project_id
        reporter = google_cloud_reporter.GoogleCloudReporter(
            client=client, project_id=project_id)

        traces = reporter.translate_to_stackdriver(trace)

        trace['projectId'] = project_id
        expected_traces = {'traces': [trace]}

        self.assertEqual(traces, expected_traces)
示例#4
0
    def test_report(self):
        trace = {'spans': [], 'traceId': '6e0c63257de34c92bf9efcd03927272e'}

        client = mock.Mock()
        project_id = 'PROJECT'
        client.project = project_id

        reporter = google_cloud_reporter.GoogleCloudReporter(
            client=client, project_id=project_id)

        reporter.report(trace)

        trace['projectId'] = project_id
        traces = {'traces': [trace]}

        client.patch_traces.assert_called_with(traces)
        self.assertTrue(client.patch_traces.called)
示例#5
0
import psycopg2
import sqlalchemy

from opencensus.trace.ext.flask.flask_middleware import FlaskMiddleware
from opencensus.trace import config_integration
from opencensus.trace.reporters import google_cloud_reporter

sys.path.insert(0, os.path.abspath(__file__ + "/../../../.."))
from ext import config

INTEGRATIONS = ['mysql', 'postgresql', 'sqlalchemy']

app = flask.Flask(__name__)

# Enbale tracing, send traces to Stackdriver Trace
reporter = google_cloud_reporter.GoogleCloudReporter()
middleware = FlaskMiddleware(app, reporter=reporter)
config_integration.trace_integrations(INTEGRATIONS)


@app.route('/')
def hello():
    return 'hello'


@app.route('/mysql')
def mysql_query():
    try:
        conn = mysql.connector.connect(user=config.MYSQL_USER,
                                       password=config.MYSQL_PASSWORD)
        cursor = conn.cursor()