1+ import json
2+ import logging
13import os
2- from json .decoder import JSONDecodeError
4+ import pathlib
5+ import pickle
36import shutil
47import tempfile
5- import shutil
8+ import time
69import 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
1114import connexion
15+ import pandas as pd
1216from 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
1717from 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
2520from opentelemetry import trace
26- from opentelemetry .sdk . trace import TracerProvider
21+ from opentelemetry .exporter . otlp . proto . http . trace_exporter import OTLPSpanExporter
2722from opentelemetry .sdk .resources import SERVICE_NAME , Resource
23+ from opentelemetry .sdk .trace import TracerProvider
2824from 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
4531from 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
5246from schematic .utils .general import entity_type_mapping
5347from 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
6254logger = logging .getLogger (__name__ )
6355logging .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 ()
8996tracer = trace .get_tracer ("Schematic" )
9097
9198
0 commit comments