Skip to content

Commit 7ada9cf

Browse files
authored
Merge pull request #1455 from Sage-Bionetworks/develop-fix-tracer
fix: used environment variable to control tracing which allows tracing to be off during pytest run and normal API run
2 parents 3f4185d + b11af55 commit 7ada9cf

File tree

1 file changed

+45
-38
lines changed

1 file changed

+45
-38
lines changed

schematic_api/api/routes.py

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,55 @@
1+
import json
2+
import logging
13
import os
2-
from json.decoder import JSONDecodeError
4+
import pathlib
5+
import pickle
36
import shutil
47
import tempfile
5-
import shutil
8+
import time
69
import urllib.request
7-
import logging
8-
import pathlib
9-
import pickle
10+
from functools import wraps
11+
from json.decoder import JSONDecodeError
12+
from typing import Any, List, Optional
1013

1114
import connexion
15+
import pandas as pd
1216
from connexion.decorators.uri_parsing import Swagger2URIParser
13-
from werkzeug.debug import DebuggedApplication
14-
15-
from flask_cors import cross_origin
16-
from flask import send_from_directory
1717
from flask import current_app as app
18-
from flask import request
19-
20-
import pandas as pd
21-
import json
22-
from typing import Optional, List, Any
23-
from functools import wraps
24-
18+
from flask import request, send_from_directory
19+
from flask_cors import cross_origin
2520
from opentelemetry import trace
26-
from opentelemetry.sdk.trace import TracerProvider
21+
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
2722
from opentelemetry.sdk.resources import SERVICE_NAME, Resource
23+
from opentelemetry.sdk.trace import TracerProvider
2824
from opentelemetry.sdk.trace.export import (
2925
BatchSpanProcessor,
3026
ConsoleSpanExporter,
27+
SimpleSpanProcessor,
3128
Span,
3229
)
33-
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
34-
35-
from schematic.configuration.configuration import CONFIG
36-
from schematic.visualization.attributes_explorer import AttributesExplorer
37-
from schematic.visualization.tangled_tree import TangledTree
38-
from schematic.manifest.generator import ManifestGenerator
39-
from schematic.models.metadata import MetadataModel
40-
41-
from schematic.schemas.data_model_parser import DataModelParser
42-
from schematic.schemas.data_model_graph import DataModelGraph, DataModelGraphExplorer
43-
44-
from schematic.store.synapse import SynapseStorage, ManifestDownload
30+
from opentelemetry.sdk.trace.sampling import ALWAYS_OFF
4531
from synapseclient.core.exceptions import (
46-
SynapseHTTPError,
4732
SynapseAuthenticationError,
48-
SynapseUnmetAccessRestrictions,
33+
SynapseHTTPError,
4934
SynapseNoCredentialsError,
5035
SynapseTimeoutError,
36+
SynapseUnmetAccessRestrictions,
5137
)
38+
from werkzeug.debug import DebuggedApplication
39+
40+
from schematic.configuration.configuration import CONFIG
41+
from schematic.manifest.generator import ManifestGenerator
42+
from schematic.models.metadata import MetadataModel
43+
from schematic.schemas.data_model_graph import DataModelGraph, DataModelGraphExplorer
44+
from schematic.schemas.data_model_parser import DataModelParser
45+
from schematic.store.synapse import ManifestDownload, SynapseStorage
5246
from schematic.utils.general import entity_type_mapping
5347
from schematic.utils.schema_utils import (
54-
get_property_label_from_display_name,
5548
DisplayLabelType,
56-
)
57-
from schematic.utils.schema_utils import (
5849
get_property_label_from_display_name,
59-
DisplayLabelType,
6050
)
51+
from schematic.visualization.attributes_explorer import AttributesExplorer
52+
from schematic.visualization.tangled_tree import TangledTree
6153

6254
logger = logging.getLogger(__name__)
6355
logging.basicConfig(level=logging.DEBUG)
@@ -83,9 +75,24 @@ def export(self, spans: List[Span]) -> None:
8375
f.write(span_json_one_line)
8476

8577

86-
trace.get_tracer_provider().add_span_processor(BatchSpanProcessor(OTLPSpanExporter()))
87-
# processor = SimpleSpanProcessor(FileSpanExporter("otel_spans_schemati_api.json"))
88-
# trace.get_tracer_provider().add_span_processor(processor)
78+
def set_up_tracing() -> None:
79+
"""Set up tracing for the API."""
80+
tracing_export = os.environ.get("TRACING_EXPORT_FORMAT", None)
81+
if tracing_export == "otlp":
82+
trace.get_tracer_provider().add_span_processor(
83+
BatchSpanProcessor(OTLPSpanExporter())
84+
)
85+
elif tracing_export == "file":
86+
timestamp_millis = int(time.time() * 1000)
87+
file_name = f"otel_spans_integration_testing_{timestamp_millis}.ndjson"
88+
file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), file_name)
89+
processor = SimpleSpanProcessor(FileSpanExporter(file_path))
90+
trace.get_tracer_provider().add_span_processor(processor)
91+
else:
92+
trace.set_tracer_provider(TracerProvider(sampler=ALWAYS_OFF))
93+
94+
95+
set_up_tracing()
8996
tracer = trace.get_tracer("Schematic")
9097

9198

0 commit comments

Comments
 (0)