Skip to content
This repository was archived by the owner on Sep 17, 2025. It is now read-only.

Commit 38b769a

Browse files
authored
Add django example (#1170)
1 parent 8a94d6f commit 38b769a

File tree

18 files changed

+474
-6
lines changed

18 files changed

+474
-6
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2022-10-26 15:48:57,700 INFO This is an INFO level log entry.
2+
2022-10-26 15:48:57,700 WARNING This is a WARNING level log entry.
3+
2022-10-26 15:48:57,701 ERROR This is an ERROR level log entry.
4+
2022-10-26 15:48:57,702 CRITICAL This is a CRITICAL level log entry.
5+
2022-10-26 16:10:22,849 INFO This is an INFO level log entry.
6+
2022-10-26 16:10:22,850 WARNING This is a WARNING level log entry.
7+
2022-10-26 16:10:22,850 ERROR This is an ERROR level log entry.
8+
2022-10-26 16:10:22,850 CRITICAL This is a CRITICAL level log entry.

contrib/opencensus-ext-azure/examples/traces/django/mysite/db.sqlite3

Whitespace-only changes.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2019, OpenCensus Authors
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
"""Django's command-line utility for administrative tasks."""
17+
import os
18+
import sys
19+
20+
21+
def main():
22+
"""Run administrative tasks."""
23+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
24+
try:
25+
from django.core.management import execute_from_command_line
26+
except ImportError as exc:
27+
raise ImportError(
28+
"Couldn't import Django. Are you sure it's installed and "
29+
"available on your PYTHONPATH environment variable? Did you "
30+
"forget to activate a virtual environment?"
31+
) from exc
32+
execute_from_command_line(sys.argv)
33+
34+
35+
if __name__ == '__main__':
36+
main()
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright 2019, 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.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright 2019, 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+
ASGI config for mysite project.
16+
17+
It exposes the ASGI callable as a module-level variable named ``application``.
18+
19+
For more information on this file, see
20+
https://docs.djangoproject.com/en/3.2/howto/deployment/asgi/
21+
"""
22+
23+
import os
24+
25+
from django.core.asgi import get_asgi_application
26+
27+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
28+
29+
application = get_asgi_application()
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
# Copyright 2019, 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+
Django settings for mysite project.
16+
17+
Generated by 'django-admin startproject' using Django 3.2.14.
18+
19+
For more information on this file, see
20+
https://docs.djangoproject.com/en/3.2/topics/settings/
21+
22+
For the full list of settings and their values, see
23+
https://docs.djangoproject.com/en/3.2/ref/settings/
24+
"""
25+
26+
from pathlib import Path
27+
28+
# Build paths inside the project like this: BASE_DIR / 'subdir'.
29+
BASE_DIR = Path(__file__).resolve().parent.parent
30+
31+
32+
# Quick-start development settings - unsuitable for production
33+
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
34+
35+
# SECURITY WARNING: keep the secret key used in production secret!
36+
SECRET_KEY = 'secret_key_for_test'
37+
38+
ALLOWED_HOSTS = ['*']
39+
40+
# Application definition
41+
42+
INSTALLED_APPS = [
43+
'django.contrib.admin',
44+
'django.contrib.auth',
45+
'django.contrib.contenttypes',
46+
'django.contrib.sessions',
47+
'django.contrib.messages',
48+
'django.contrib.staticfiles',
49+
]
50+
51+
MIDDLEWARE = [
52+
'django.middleware.security.SecurityMiddleware',
53+
'django.contrib.sessions.middleware.SessionMiddleware',
54+
'django.middleware.common.CommonMiddleware',
55+
'django.middleware.csrf.CsrfViewMiddleware',
56+
'django.contrib.auth.middleware.AuthenticationMiddleware',
57+
'django.contrib.messages.middleware.MessageMiddleware',
58+
'django.middleware.clickjacking.XFrameOptionsMiddleware',
59+
'opencensus.ext.django.middleware.OpencensusMiddleware',
60+
]
61+
62+
MY_CONNECTION_STRING = "'<your-ikey-here>'"
63+
64+
OPENCENSUS = {
65+
'TRACE': {
66+
'SAMPLER': 'opencensus.trace.samplers.ProbabilitySampler(rate=1.0)',
67+
'EXPORTER': 'opencensus.ext.azure.trace_exporter.AzureExporter(connection_string=' + MY_CONNECTION_STRING + ')', # noqa: E501
68+
}
69+
}
70+
71+
ROOT_URLCONF = 'mysite.urls'
72+
73+
TEMPLATES = [
74+
{
75+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
76+
'DIRS': [],
77+
'APP_DIRS': True,
78+
'OPTIONS': {
79+
'context_processors': [
80+
'django.template.context_processors.debug',
81+
'django.template.context_processors.request',
82+
'django.contrib.auth.context_processors.auth',
83+
'django.contrib.messages.context_processors.messages',
84+
],
85+
},
86+
},
87+
]
88+
89+
WSGI_APPLICATION = 'mysite.wsgi.application'
90+
91+
92+
# Database
93+
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
94+
95+
DATABASES = {
96+
'default': {
97+
'ENGINE': 'django.db.backends.sqlite3',
98+
'NAME': BASE_DIR / 'db.sqlite3',
99+
}
100+
}
101+
102+
103+
# Password validation
104+
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators
105+
106+
AUTH_PASSWORD_VALIDATORS = [
107+
{
108+
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', # noqa: E501
109+
},
110+
{
111+
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', # noqa: E501
112+
},
113+
{
114+
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', # noqa: E501
115+
},
116+
{
117+
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', # noqa: E501
118+
},
119+
]
120+
121+
122+
# Internationalization
123+
# https://docs.djangoproject.com/en/3.2/topics/i18n/
124+
125+
LANGUAGE_CODE = 'en-us'
126+
127+
TIME_ZONE = 'UTC'
128+
129+
USE_I18N = True
130+
131+
USE_L10N = True
132+
133+
USE_TZ = True
134+
135+
136+
# Static files (CSS, JavaScript, Images)
137+
# https://docs.djangoproject.com/en/3.2/howto/static-files/
138+
139+
STATIC_URL = '/static/'
140+
141+
# Default primary key field type
142+
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
143+
144+
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
145+
146+
# SECURITY WARNING: don't run with debug turned on in production!
147+
DEBUG = False
148+
ALLOWED_HOSTS = ["*"]
149+
150+
LOGGING = {
151+
'version': 1,
152+
'disable_existing_loggers': False,
153+
'formatters': {
154+
'timestamp': {
155+
'format': '{asctime} {levelname} {message}',
156+
'style': '{',
157+
},
158+
},
159+
'handlers': {
160+
'console': {
161+
'level': 'DEBUG',
162+
'class': 'logging.StreamHandler',
163+
'formatter': 'timestamp',
164+
},
165+
'logfile': {
166+
'level': 'DEBUG',
167+
'class': 'logging.FileHandler',
168+
'formatter': 'timestamp',
169+
'filename': str(BASE_DIR) + "/../logfile",
170+
},
171+
'azure': {
172+
'level': "DEBUG",
173+
'class': "opencensus.ext.azure.log_exporter.AzureLogHandler",
174+
'connection_string': MY_CONNECTION_STRING,
175+
'formatter': 'timestamp',
176+
},
177+
},
178+
'loggers': {
179+
'custom': {
180+
'level': 'INFO',
181+
'handlers': ['console', 'logfile', 'azure']
182+
}
183+
}
184+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Copyright 2019, 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+
"""mysite URL Configuration
15+
16+
The `urlpatterns` list routes URLs to views. For more information please see:
17+
https://docs.djangoproject.com/en/3.2/topics/http/urls/
18+
Examples:
19+
Function views
20+
1. Add an import: from my_app import views
21+
2. Add a URL to urlpatterns: path('', views.home, name='home')
22+
Class-based views
23+
1. Add an import: from other_app.views import Home
24+
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
25+
Including another URLconf
26+
1. Import the include() function: from django.urls import include, path
27+
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
28+
"""
29+
from django.contrib import admin
30+
from django.urls import include, path
31+
32+
urlpatterns = [
33+
path('admin/', admin.site.urls),
34+
path('', include('polls.urls')),
35+
]
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright 2019, 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+
WSGI config for mysite project.
16+
17+
It exposes the WSGI callable as a module-level variable named ``application``.
18+
19+
For more information on this file, see
20+
https://docs.djangoproject.com/en/3.2/howto/deployment/wsgi/
21+
"""
22+
23+
import os
24+
25+
from django.core.wsgi import get_wsgi_application
26+
27+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
28+
29+
application = get_wsgi_application()
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright 2019, 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.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright 2019, 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.

0 commit comments

Comments
 (0)