|
| 1 | +# Copyright 2017, OpenCensus Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from django.http import HttpResponse |
| 16 | +from django.shortcuts import render |
| 17 | + |
| 18 | +from .forms import HelloForm |
| 19 | + |
| 20 | +from opencensus.trace import config_integration |
| 21 | + |
| 22 | +import mysql.connector |
| 23 | +import psycopg2 |
| 24 | +import sqlalchemy |
| 25 | + |
| 26 | +import time |
| 27 | +import os |
| 28 | +import requests |
| 29 | + |
| 30 | +DB_HOST = 'localhost' |
| 31 | + |
| 32 | +# MySQL settings |
| 33 | +MYSQL_PASSWORD = os.environ.get('SYSTEST_MYSQL_PASSWORD') |
| 34 | + |
| 35 | +# PostgreSQL settings |
| 36 | +POSTGRES_PASSWORD = os.environ.get('SYSTEST_POSTGRES_PASSWORD') |
| 37 | + |
| 38 | +INTEGRATIONS = ['mysql', 'postgresql', 'sqlalchemy', 'requests'] |
| 39 | + |
| 40 | +config_integration.trace_integrations(INTEGRATIONS) |
| 41 | + |
| 42 | + |
| 43 | +def home(request): |
| 44 | + return render(request, 'home.html') |
| 45 | + |
| 46 | + |
| 47 | +def greetings(request): |
| 48 | + if request.method == 'POST': |
| 49 | + form = HelloForm(request.POST) |
| 50 | + |
| 51 | + if form.is_valid(): |
| 52 | + first_name = form.cleaned_data['fname'] |
| 53 | + last_name = form.cleaned_data['lname'] |
| 54 | + return HttpResponse( |
| 55 | + "Hello, {} {}".format(first_name, last_name)) |
| 56 | + else: |
| 57 | + return render(request, 'home.html') |
| 58 | + |
| 59 | + return render(request, 'home.html') |
| 60 | + |
| 61 | + |
| 62 | +def trace_requests(request): |
| 63 | + response = requests.get('http://www.google.com') |
| 64 | + return HttpResponse(str(response.status_code)) |
| 65 | + |
| 66 | + |
| 67 | +def mysql_trace(request): |
| 68 | + try: |
| 69 | + conn = mysql.connector.connect( |
| 70 | + host=DB_HOST, |
| 71 | + user='root', |
| 72 | + password=MYSQL_PASSWORD) |
| 73 | + cursor = conn.cursor() |
| 74 | + |
| 75 | + query = 'SELECT 2*3' |
| 76 | + cursor.execute(query) |
| 77 | + |
| 78 | + result = [] |
| 79 | + |
| 80 | + for item in cursor: |
| 81 | + result.append(item) |
| 82 | + |
| 83 | + return HttpResponse(str(result)) |
| 84 | + |
| 85 | + except Exception: |
| 86 | + msg = "Query failed. Check your env vars for connection settings." |
| 87 | + return HttpResponse(msg, status=500) |
| 88 | + |
| 89 | + |
| 90 | +def postgresql_trace(request): |
| 91 | + try: |
| 92 | + conn = psycopg2.connect( |
| 93 | + host=DB_HOST, |
| 94 | + user='postgres', |
| 95 | + password=POSTGRES_PASSWORD, |
| 96 | + dbname='postgres') |
| 97 | + cursor = conn.cursor() |
| 98 | + |
| 99 | + query = 'SELECT 2*3' |
| 100 | + cursor.execute(query) |
| 101 | + |
| 102 | + result = [] |
| 103 | + |
| 104 | + for item in cursor.fetchall(): |
| 105 | + result.append(item) |
| 106 | + |
| 107 | + return HttpResponse(str(result)) |
| 108 | + |
| 109 | + except Exception: |
| 110 | + msg = "Query failed. Check your env vars for connection settings." |
| 111 | + return HttpResponse(msg, status=500) |
| 112 | + |
| 113 | + |
| 114 | +def sqlalchemy_mysql_trace(request): |
| 115 | + try: |
| 116 | + engine = sqlalchemy.create_engine( |
| 117 | + 'mysql+mysqlconnector://{}:{}@{}'.format( |
| 118 | + 'root', MYSQL_PASSWORD, DB_HOST)) |
| 119 | + conn = engine.connect() |
| 120 | + |
| 121 | + query = 'SELECT 2*3' |
| 122 | + |
| 123 | + result_set = conn.execute(query) |
| 124 | + |
| 125 | + result = [] |
| 126 | + |
| 127 | + for item in result_set: |
| 128 | + result.append(item) |
| 129 | + |
| 130 | + return HttpResponse(str(result)) |
| 131 | + |
| 132 | + except Exception: |
| 133 | + msg = "Query failed. Check your env vars for connection settings." |
| 134 | + return HttpResponse(msg, status=500) |
| 135 | + |
| 136 | + |
| 137 | +def sqlalchemy_postgresql_trace(request): |
| 138 | + try: |
| 139 | + engine = sqlalchemy.create_engine( |
| 140 | + 'postgresql://{}:{}@{}/{}'.format( |
| 141 | + 'postgres', POSTGRES_PASSWORD, |
| 142 | + DB_HOST, 'postgres')) |
| 143 | + conn = engine.connect() |
| 144 | + |
| 145 | + query = 'SELECT 2*3' |
| 146 | + |
| 147 | + result_set = conn.execute(query) |
| 148 | + |
| 149 | + result = [] |
| 150 | + |
| 151 | + for item in result_set: |
| 152 | + result.append(item) |
| 153 | + |
| 154 | + return HttpResponse(str(result)) |
| 155 | + |
| 156 | + except Exception: |
| 157 | + msg = "Query failed. Check your env vars for connection settings." |
| 158 | + return HttpResponse(msg, status=500) |
| 159 | + |
| 160 | + |
| 161 | +def health_check(request): |
| 162 | + return HttpResponse("ok", status=200) |
| 163 | + |
| 164 | + |
| 165 | +def get_request_header(request): |
| 166 | + return HttpResponse(request.META.get('HTTP_X_CLOUD_TRACE_CONTEXT')) |
0 commit comments