Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
Open Subsurface Data Universe Software
Platform
Domain Data Mgmt Services
Wellbore
Wellbore Domain Services
Commits
952b1704
Commit
952b1704
authored
Jul 09, 2021
by
Alexandre Vincent
Browse files
add data-partition-id to traces
parent
540228b7
Changes
4
Hide whitespace changes
Inline
Side-by-side
app/conf.py
View file @
952b1704
...
...
@@ -371,4 +371,4 @@ AUTHORIZATION_HEADER_NAME = 'Authorization'
APP_KEY_HEADER_NAME
=
'appKey'
CORRELATION_ID_HEADER_NAME
=
'correlation-id'
REQUEST_ID_HEADER_NAME
=
'Request-ID'
PARTITION_ID_HEADER_NAME
=
'data-partition-id'
app/middleware/traces_middleware.py
View file @
952b1704
...
...
@@ -87,6 +87,15 @@ class TracingMiddleware(BaseHTTPMiddleware):
tracer
.
add_attribute_to_current_span
(
attribute_key
=
conf
.
CORRELATION_ID_HEADER_NAME
,
attribute_value
=
correlation_id
)
ctx_partition_id
=
get_or_create_ctx
().
partition_id
partition_id
=
ctx_partition_id
if
ctx_partition_id
is
not
None
\
else
request
.
headers
.
get
(
conf
.
PARTITION_ID_HEADER_NAME
)
# only put in the trace if there is data
if
partition_id
is
not
None
:
tracer
.
add_attribute_to_current_span
(
attribute_key
=
conf
.
PARTITION_ID_HEADER_NAME
,
attribute_value
=
partition_id
)
request_content_type
=
request
.
headers
.
get
(
"Content-type"
)
tracer
.
add_attribute_to_current_span
(
attribute_key
=
"request.header Content-type"
,
...
...
tests/unit/middleware
/client
_test.py
→
tests/unit/
clients/clients_
middleware_test.py
View file @
952b1704
# Copyright 2021 Schlumberger
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import
pytest
from
pytest_httpx
import
HTTPXMock
from
app.clients
import
make_storage_record_client
,
make_search_client
...
...
tests/unit/middleware/traces_middleware_test.py
0 → 100644
View file @
952b1704
# Copyright 2021 Schlumberger
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from
opencensus.trace
import
base_exporter
from
fastapi.testclient
import
TestClient
import
pytest
from
app.wdms_app
import
wdms_app
,
DDMS_V2_PATH
from
app.utils
import
get_or_create_ctx
from
tests.unit.test_utils
import
NopeLogger
from
unittest.mock
import
MagicMock
# Initialize traces exporter in app with a custom one to allow validating our traces
class
TestExporter
(
base_exporter
.
Exporter
):
def
__init__
(
self
)
->
None
:
self
.
exported
=
[]
def
export
(
self
,
span_datas
):
self
.
exported
+=
span_datas
def
find
(
self
,
correlation_id
):
for
sd
in
self
.
exported
:
if
sd
.
attributes
.
get
(
'correlation-id'
)
==
correlation_id
:
return
sd
@
pytest
.
fixture
()
def
ctx_fixture
():
""" Create context with a real tracer in it """
ctx
=
get_or_create_ctx
().
set_current_with_value
(
logger
=
NopeLogger
())
yield
ctx
@
pytest
.
fixture
def
client
(
ctx_fixture
):
yield
TestClient
(
wdms_app
)
wdms_app
.
dependency_overrides
=
{}
def
build_url
(
path
:
str
):
return
DDMS_V2_PATH
+
path
def
test_about_call_creates_correlation_id_if_absent
(
client
:
TestClient
):
# Initialize traces exporter in app, like it is in app's startup_event
wdms_app
.
trace_exporter
=
TestExporter
()
# no header -> works fine
response
=
client
.
get
(
build_url
(
"/about"
))
assert
response
.
status_code
==
200
# one call was exported, with correlation-id
assert
len
(
wdms_app
.
trace_exporter
.
exported
)
==
1
# one call => one export
spandata
=
wdms_app
.
trace_exporter
.
exported
[
0
]
assert
'correlation-id'
in
spandata
.
attributes
.
keys
()
def
test_about_call_traces_existing_correlation_id
(
client
:
TestClient
):
# Initialize traces exporter in app, like it is in app's startup_event
wdms_app
.
trace_exporter
=
TestExporter
()
# no header -> works fine
response
=
client
.
get
(
build_url
(
"/about"
),
headers
=
{
'correlation-id'
:
'some correlation id'
})
assert
response
.
status_code
==
200
# one call was exported, with correlation-id
assert
len
(
wdms_app
.
trace_exporter
.
exported
)
==
1
# one call => one export
spandata
=
wdms_app
.
trace_exporter
.
exported
[
0
]
assert
'correlation-id'
in
spandata
.
attributes
.
keys
()
assert
spandata
.
attributes
[
'correlation-id'
]
==
'some correlation id'
def
test_about_call_traces_existing_data_partition_id
(
client
:
TestClient
):
# Initialize traces exporter in app, like it is in app's startup_event
wdms_app
.
trace_exporter
=
TestExporter
()
# no header -> works fine
response
=
client
.
get
(
build_url
(
"/about"
))
assert
response
.
status_code
==
200
# one call was exported, without data-partition-id
assert
len
(
wdms_app
.
trace_exporter
.
exported
)
==
1
# one call => one export
spandata
=
wdms_app
.
trace_exporter
.
exported
[
0
]
assert
'data-partition-id'
not
in
spandata
.
attributes
.
keys
()
# data-partition-id header -> works as well
client
.
get
(
build_url
(
"/about"
),
headers
=
{
'data-partition-id'
:
'some partition id'
})
# a second call was exported, with data-partition-id
assert
len
(
wdms_app
.
trace_exporter
.
exported
)
==
2
# one call => one export
spandata
=
wdms_app
.
trace_exporter
.
exported
[
1
]
assert
'data-partition-id'
in
spandata
.
attributes
.
keys
()
assert
spandata
.
attributes
[
'data-partition-id'
]
==
'some partition id'
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment