Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit fc51086

Browse filesBrowse files
authored
feat: Add open telemetry trace in schema and related unit tests (#648)
* fix: lint_setup_py was failing in Kokoro is not fixed * feat: adding opentelemetry tracing * feat: added opentelemetry support * feat: added open telemetry tracing support and tests * refactor: lint fixes * refactor: lint fixes * refactor: added license text * ci: corrrected version for google-cloud-spanner
1 parent 287b893 commit fc51086
Copy full SHA for fc51086

File tree

2 files changed

+372
-20
lines changed
Filter options

2 files changed

+372
-20
lines changed

‎django_spanner/schema.py

Copy file name to clipboardExpand all lines: django_spanner/schema.py
+99-18
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from django.db import NotSupportedError
88
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
9+
from django_spanner._opentelemetry_tracing import trace_call
910

1011

1112
class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
@@ -119,7 +120,15 @@ def create_model(self, model):
119120
sql += " " + tablespace_sql
120121
# Prevent using [] as params, in the case a literal '%' is used in the
121122
# definition
122-
self.execute(sql, params or None)
123+
trace_attributes = {
124+
"model_name": self.quote_name(model._meta.db_table)
125+
}
126+
with trace_call(
127+
"CloudSpannerDjango.create_model",
128+
self.connection,
129+
trace_attributes,
130+
):
131+
self.execute(sql, params or None)
123132

124133
# Add any field index and index_together's (deferred as SQLite
125134
# _remake_table needs it)
@@ -144,8 +153,25 @@ def delete_model(self, model):
144153
model, index=True, primary_key=False
145154
)
146155
for index_name in index_names:
147-
self.execute(self._delete_index_sql(model, index_name))
148-
super().delete_model(model)
156+
trace_attributes = {
157+
"model_name": self.quote_name(model._meta.db_table),
158+
"index_name": index_name,
159+
}
160+
with trace_call(
161+
"CloudSpannerDjango.delete_model.delete_index",
162+
self.connection,
163+
trace_attributes,
164+
):
165+
self.execute(self._delete_index_sql(model, index_name))
166+
trace_attributes = {
167+
"model_name": self.quote_name(model._meta.db_table)
168+
}
169+
with trace_call(
170+
"CloudSpannerDjango.delete_model",
171+
self.connection,
172+
trace_attributes,
173+
):
174+
super().delete_model(model)
149175

150176
def add_field(self, model, field):
151177
"""
@@ -250,8 +276,28 @@ def remove_field(self, model, field):
250276
# column.
251277
index_names = self._constraint_names(model, [field.column], index=True)
252278
for index_name in index_names:
253-
self.execute(self._delete_index_sql(model, index_name))
254-
super().remove_field(model, field)
279+
trace_attributes = {
280+
"model_name": self.quote_name(model._meta.db_table),
281+
"field": field.column,
282+
"index_name": index_name,
283+
}
284+
with trace_call(
285+
"CloudSpannerDjango.remove_field.delete_index",
286+
self.connection,
287+
trace_attributes,
288+
):
289+
self.execute(self._delete_index_sql(model, index_name))
290+
291+
trace_attributes = {
292+
"model_name": self.quote_name(model._meta.db_table),
293+
"field": field.column,
294+
}
295+
with trace_call(
296+
"CloudSpannerDjango.remove_field",
297+
self.connection,
298+
trace_attributes,
299+
):
300+
super().remove_field(model, field)
255301

256302
def column_sql(
257303
self, model, field, include_default=False, exclude_not_null=False
@@ -320,7 +366,14 @@ def add_index(self, model, index):
320366
(field_name, " DESC" if order == "DESC" else "")
321367
for field_name, order in index.fields_orders
322368
]
323-
super().add_index(model, index)
369+
trace_attributes = {
370+
"model_name": self.quote_name(model._meta.db_table),
371+
"index": "|".join(index.fields),
372+
}
373+
with trace_call(
374+
"CloudSpannerDjango.add_index", self.connection, trace_attributes,
375+
):
376+
super().add_index(model, index)
324377

325378
def quote_value(self, value):
326379
# A more complete implementation isn't currently required.
@@ -355,20 +408,48 @@ def _alter_field(
355408
"index isn't yet supported."
356409
)
357410
for index_name in index_names:
358-
self.execute(self._delete_index_sql(model, index_name))
359-
super()._alter_field(
360-
model,
361-
old_field,
362-
new_field,
363-
old_type,
364-
new_type,
365-
old_db_params,
366-
new_db_params,
367-
strict=False,
368-
)
411+
trace_attributes = {
412+
"model_name": self.quote_name(model._meta.db_table),
413+
"alter_field": old_field.column,
414+
"index_name": index_name,
415+
}
416+
with trace_call(
417+
"CloudSpannerDjango.alter_field.delete_index",
418+
self.connection,
419+
trace_attributes,
420+
):
421+
self.execute(self._delete_index_sql(model, index_name))
422+
trace_attributes = {
423+
"model_name": self.quote_name(model._meta.db_table),
424+
"alter_field": old_field.column,
425+
}
426+
with trace_call(
427+
"CloudSpannerDjango.alter_field",
428+
self.connection,
429+
trace_attributes,
430+
):
431+
super()._alter_field(
432+
model,
433+
old_field,
434+
new_field,
435+
old_type,
436+
new_type,
437+
old_db_params,
438+
new_db_params,
439+
strict=False,
440+
)
369441
# Recreate the index that was dropped earlier.
370442
if nullability_changed and new_field.db_index:
371-
self.execute(self._create_index_sql(model, [new_field]))
443+
trace_attributes = {
444+
"model_name": self.quote_name(model._meta.db_table),
445+
"alter_field": new_field.column,
446+
}
447+
with trace_call(
448+
"CloudSpannerDjango.alter_field.recreate_index",
449+
self.connection,
450+
trace_attributes,
451+
):
452+
self.execute(self._create_index_sql(model, [new_field]))
372453

373454
def _alter_column_type_sql(self, model, old_field, new_field, new_type):
374455
# Spanner needs to use sql_alter_column_not_null if the field is

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.