MindsDB 25.4.4.0__py3-none-any.whl → 25.5.3.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of MindsDB might be problematic. Click here for more details.

Files changed (86) hide show
  1. mindsdb/__about__.py +1 -1
  2. mindsdb/__main__.py +107 -125
  3. mindsdb/api/executor/command_executor.py +14 -3
  4. mindsdb/api/executor/datahub/datanodes/information_schema_datanode.py +8 -0
  5. mindsdb/api/executor/datahub/datanodes/mindsdb_tables.py +2 -1
  6. mindsdb/api/executor/datahub/datanodes/system_tables.py +10 -13
  7. mindsdb/api/executor/planner/query_plan.py +1 -0
  8. mindsdb/api/executor/planner/query_planner.py +9 -1
  9. mindsdb/api/executor/sql_query/sql_query.py +24 -8
  10. mindsdb/api/executor/sql_query/steps/apply_predictor_step.py +21 -3
  11. mindsdb/api/executor/sql_query/steps/fetch_dataframe_partition.py +3 -1
  12. mindsdb/api/http/initialize.py +20 -3
  13. mindsdb/api/http/namespaces/analysis.py +14 -1
  14. mindsdb/api/http/namespaces/config.py +19 -11
  15. mindsdb/api/http/namespaces/tree.py +1 -1
  16. mindsdb/api/http/start.py +7 -2
  17. mindsdb/api/mysql/mysql_proxy/mysql_proxy.py +4 -8
  18. mindsdb/api/mysql/mysql_proxy/utilities/exceptions.py +0 -4
  19. mindsdb/api/postgres/postgres_proxy/postgres_packets/postgres_message_formats.py +2 -2
  20. mindsdb/integrations/handlers/bigquery_handler/requirements.txt +1 -0
  21. mindsdb/integrations/handlers/chromadb_handler/requirements.txt +1 -0
  22. mindsdb/integrations/handlers/gmail_handler/requirements.txt +1 -0
  23. mindsdb/integrations/handlers/google_analytics_handler/requirements.txt +2 -1
  24. mindsdb/integrations/handlers/google_books_handler/requirements.txt +1 -1
  25. mindsdb/integrations/handlers/google_calendar_handler/requirements.txt +1 -0
  26. mindsdb/integrations/handlers/google_content_shopping_handler/requirements.txt +1 -1
  27. mindsdb/integrations/handlers/google_fit_handler/requirements.txt +2 -0
  28. mindsdb/integrations/handlers/google_search_handler/requirements.txt +1 -1
  29. mindsdb/integrations/handlers/jira_handler/jira_handler.archived.py +75 -0
  30. mindsdb/integrations/handlers/jira_handler/jira_handler.py +113 -38
  31. mindsdb/integrations/handlers/jira_handler/jira_tables.py +229 -0
  32. mindsdb/integrations/handlers/jira_handler/requirements.txt +1 -0
  33. mindsdb/integrations/handlers/lightfm_handler/requirements.txt +1 -0
  34. mindsdb/integrations/handlers/lightwood_handler/lightwood_handler.py +0 -2
  35. mindsdb/integrations/handlers/lightwood_handler/requirements.txt +4 -4
  36. mindsdb/integrations/handlers/lindorm_handler/requirements.txt +1 -0
  37. mindsdb/integrations/handlers/ms_one_drive_handler/requirements.txt +2 -0
  38. mindsdb/integrations/handlers/ms_teams_handler/requirements.txt +3 -1
  39. mindsdb/integrations/handlers/openai_handler/helpers.py +3 -5
  40. mindsdb/integrations/handlers/openai_handler/openai_handler.py +25 -12
  41. mindsdb/integrations/handlers/snowflake_handler/requirements.txt +1 -1
  42. mindsdb/integrations/handlers/togetherai_handler/__about__.py +9 -0
  43. mindsdb/integrations/handlers/togetherai_handler/__init__.py +20 -0
  44. mindsdb/integrations/handlers/togetherai_handler/creation_args.py +14 -0
  45. mindsdb/integrations/handlers/togetherai_handler/icon.svg +15 -0
  46. mindsdb/integrations/handlers/togetherai_handler/model_using_args.py +5 -0
  47. mindsdb/integrations/handlers/togetherai_handler/requirements.txt +2 -0
  48. mindsdb/integrations/handlers/togetherai_handler/settings.py +33 -0
  49. mindsdb/integrations/handlers/togetherai_handler/togetherai_handler.py +234 -0
  50. mindsdb/integrations/handlers/vertex_handler/requirements.txt +1 -0
  51. mindsdb/integrations/handlers/youtube_handler/requirements.txt +1 -0
  52. mindsdb/integrations/utilities/files/file_reader.py +5 -2
  53. mindsdb/integrations/utilities/handler_utils.py +4 -0
  54. mindsdb/integrations/utilities/rag/rerankers/base_reranker.py +360 -0
  55. mindsdb/integrations/utilities/rag/rerankers/reranker_compressor.py +6 -346
  56. mindsdb/interfaces/agents/constants.py +14 -2
  57. mindsdb/interfaces/agents/langchain_agent.py +2 -4
  58. mindsdb/interfaces/database/projects.py +1 -7
  59. mindsdb/interfaces/functions/controller.py +14 -16
  60. mindsdb/interfaces/functions/to_markdown.py +9 -124
  61. mindsdb/interfaces/knowledge_base/controller.py +109 -92
  62. mindsdb/interfaces/knowledge_base/preprocessing/document_preprocessor.py +28 -5
  63. mindsdb/interfaces/knowledge_base/utils.py +10 -15
  64. mindsdb/interfaces/model/model_controller.py +0 -2
  65. mindsdb/interfaces/query_context/context_controller.py +55 -15
  66. mindsdb/interfaces/query_context/query_task.py +19 -0
  67. mindsdb/interfaces/skills/sql_agent.py +33 -11
  68. mindsdb/interfaces/storage/db.py +2 -2
  69. mindsdb/interfaces/tasks/task_monitor.py +5 -1
  70. mindsdb/interfaces/tasks/task_thread.py +6 -0
  71. mindsdb/migrations/migrate.py +0 -2
  72. mindsdb/migrations/versions/2025-04-22_53502b6d63bf_query_database.py +27 -0
  73. mindsdb/utilities/config.py +15 -3
  74. mindsdb/utilities/context.py +2 -1
  75. mindsdb/utilities/functions.py +0 -36
  76. mindsdb/utilities/langfuse.py +19 -10
  77. mindsdb/utilities/otel/__init__.py +9 -193
  78. mindsdb/utilities/otel/metric_handlers/__init__.py +5 -1
  79. mindsdb/utilities/otel/prepare.py +198 -0
  80. mindsdb/utilities/sql.py +83 -0
  81. {mindsdb-25.4.4.0.dist-info → mindsdb-25.5.3.0.dist-info}/METADATA +662 -592
  82. {mindsdb-25.4.4.0.dist-info → mindsdb-25.5.3.0.dist-info}/RECORD +85 -69
  83. {mindsdb-25.4.4.0.dist-info → mindsdb-25.5.3.0.dist-info}/WHEEL +1 -1
  84. mindsdb/api/mysql/mysql_proxy/classes/sql_statement_parser.py +0 -151
  85. {mindsdb-25.4.4.0.dist-info → mindsdb-25.5.3.0.dist-info}/licenses/LICENSE +0 -0
  86. {mindsdb-25.4.4.0.dist-info → mindsdb-25.5.3.0.dist-info}/top_level.txt +0 -0
@@ -1,206 +1,22 @@
1
1
  import os
2
- import typing
3
-
4
- from opentelemetry import trace # noqa: F401
5
- from opentelemetry import metrics # noqa: F401
6
- from opentelemetry.exporter.otlp.proto.grpc._log_exporter import OTLPLogExporter as OTLPLogExporterGRPC
7
- from opentelemetry.exporter.otlp.proto.http._log_exporter import OTLPLogExporter as OTLPLogExporterHTTP
8
- from opentelemetry.sdk._logs._internal.export import LogExporter
9
- from opentelemetry.exporter.otlp.proto.grpc.metric_exporter import OTLPMetricExporter as OTLPMetricExporterGRPC
10
- from opentelemetry.exporter.otlp.proto.http.metric_exporter import OTLPMetricExporter as OTLPMetricExporterHTTP
11
- from opentelemetry.sdk.metrics.export import MetricExporter, ConsoleMetricExporter
12
- from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter as OTLPSpanExporterGRPC
13
- from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter as OTLPSpanExporterHTTP
14
- from opentelemetry.sdk.trace.export import SpanExporter, ConsoleSpanExporter
15
- from opentelemetry.sdk.resources import Resource
16
- from opentelemetry.sdk.trace.sampling import TraceIdRatioBased
17
-
18
- from mindsdb.utilities.otel.logger import setup_logger
19
- from mindsdb.utilities.otel.meter import setup_meter
20
- from mindsdb.utilities.otel.tracer import setup_tracer
21
- from mindsdb.utilities.utils import parse_csv_attributes
22
- from mindsdb.utilities import log
23
-
24
- logger = log.getLogger(__name__)
25
-
26
- # Check OpenTelemetry exporter type
27
- OTEL_EXPORTER_TYPE = os.getenv("OTEL_EXPORTER_TYPE", "console") # console or otlp
28
-
29
- # Define OpenTelemetry exporter protocol
30
- OTEL_EXPORTER_PROTOCOL = os.getenv("OTEL_EXPORTER_PROTOCOL", "grpc") # grpc or http
31
-
32
- # Define OTLP endpoint. If not set, the default OTLP endpoint will be used
33
- OTEL_OTLP_ENDPOINT = os.getenv("OTEL_OTLP_ENDPOINT", "http://localhost:4317")
34
-
35
- # Define OTLP logging endpoint. If not set, the default OTLP logging endpoint will be used
36
- OTEL_OTLP_LOGGING_ENDPOINT = os.getenv("OTEL_OTLP_LOGGING_ENDPOINT", OTEL_OTLP_ENDPOINT)
37
-
38
- # Define OTLP tracing endpoint. If not set, the default OTLP tracing endpoint will be used
39
- OTEL_OTLP_TRACING_ENDPOINT = os.getenv("OTEL_OTLP_TRACING_ENDPOINT", OTEL_OTLP_ENDPOINT)
40
-
41
- # Define OTLP metrics endpoint. If not set, the default OTLP metrics endpoint will be used
42
- OTEL_OTLP_METRICS_ENDPOINT = os.getenv("OTEL_OTLP_METRICS_ENDPOINT", OTEL_OTLP_ENDPOINT)
43
-
44
- # Define service name
45
- OTEL_SERVICE_NAME = os.getenv("OTEL_SERVICE_NAME", "mindsdb")
46
-
47
- # Define service instace ID
48
- OTEL_SERVICE_INSTANCE_ID = os.getenv("OTEL_SERVICE_INSTANCE_ID", "mindsdb-instance")
49
-
50
- # The name of the environment we"re on, by default local for development, this is set differently per-env in our Helm
51
- # chart values files
52
- OTEL_SERVICE_ENVIRONMENT = os.getenv("OTEL_SERVICE_ENVIRONMENT", "local").lower()
53
-
54
- # Define service release
55
- OTEL_SERVICE_RELEASE = os.getenv("OTEL_SERVICE_RELEASE", "local").lower()
56
-
57
- # Define how often to capture traces
58
- OTEL_TRACE_SAMPLE_RATE = float(os.getenv("OTEL_TRACE_SAMPLE_RATE", "1.0"))
59
-
60
- # Define extra attributes
61
- OTEL_EXTRA_ATTRIBUTES = os.getenv("OTEL_EXTRA_ATTRIBUTES", "")
62
2
 
63
3
  # By default, we have Open Telemetry SDK enabled on all envs, except for local which is disabled by default.
64
4
  OTEL_SDK_DISABLED = (os.getenv("OTEL_SDK_DISABLED", "false").lower() == "true"
65
5
  or os.getenv("OTEL_SERVICE_ENVIRONMENT", "local").lower() == "local")
66
6
 
67
- # Define if OpenTelemetry logging is disabled. By default, it is disabled.
68
- OTEL_LOGGING_DISABLED = os.getenv("OTEL_LOGGING_DISABLED", "true").lower() == "true"
69
-
70
- # Define if OpenTelemetry tracing is disabled. By default, it is enabled.
71
- OTEL_TRACING_DISABLED = os.getenv("OTEL_TRACING_DISABLED", "false").lower() == "true"
72
-
73
- # Define if OpenTelemetry metrics is disabled. By default, it is disabled.
74
- OTEL_METRICS_DISABLED = os.getenv("OTEL_METRICS_DISABLED", "true").lower() == "true"
75
-
76
7
  # If you want to enable Open Telemetry on local for some reason please set OTEL_SDK_FORCE_RUN to true
77
8
  OTEL_SDK_FORCE_RUN = os.getenv("OTEL_SDK_FORCE_RUN", "false").lower() == "true"
78
9
 
10
+ OTEL_ENABLED = not OTEL_SDK_DISABLED or OTEL_SDK_FORCE_RUN
79
11
 
80
- def get_otel_attributes() -> dict:
81
- """
82
- Get OpenTelemetry attributes
83
-
84
- Returns:
85
- dict: OpenTelemetry attributes
86
- """
12
+ def increment_otel_query_request_counter(metadata: dict) -> None:
13
+ pass
87
14
 
88
- base_attributes = {
89
- "service.name": OTEL_SERVICE_NAME,
90
- "service.instance.id": OTEL_SERVICE_INSTANCE_ID,
91
- "environment": OTEL_SERVICE_ENVIRONMENT,
92
- "release": OTEL_SERVICE_RELEASE,
93
- }
94
-
95
- extra_attributes = {}
15
+ trace = None
16
+ if OTEL_ENABLED:
96
17
  try:
97
- extra_attributes = parse_csv_attributes(OTEL_EXTRA_ATTRIBUTES)
98
- except Exception as e:
99
- logger.error(f"Failed to parse OTEL_EXTRA_ATTRIBUTES: {e}")
100
-
101
- attributes = {**extra_attributes, **base_attributes} # Base attributes take precedence over extra attributes
102
-
103
- return attributes
104
-
105
-
106
- def get_logging_exporter() -> typing.Optional[LogExporter]:
107
- """
108
- Get OpenTelemetry logging exporter.
109
-
110
- Returns:
111
- OTLPLogExporter: OpenTelemetry logging exporter
112
- """
113
-
114
- if OTEL_EXPORTER_TYPE == "otlp":
115
-
116
- if OTEL_EXPORTER_PROTOCOL == "grpc":
117
- return OTLPLogExporterGRPC(
118
- endpoint=OTEL_OTLP_LOGGING_ENDPOINT,
119
- insecure=True
120
- )
121
-
122
- elif OTEL_EXPORTER_PROTOCOL == "http":
123
- return OTLPLogExporterHTTP(
124
- endpoint=OTEL_OTLP_LOGGING_ENDPOINT
125
- )
126
-
127
- return None
128
-
129
-
130
- def get_span_exporter() -> SpanExporter:
131
- """
132
- Get OpenTelemetry span exporter
133
-
134
- Returns:
135
- OTLPSpanExporter: OpenTelemetry span exporter
136
- """
137
-
138
- if OTEL_EXPORTER_TYPE == "otlp":
139
-
140
- if OTEL_EXPORTER_PROTOCOL == "grpc":
141
- return OTLPSpanExporterGRPC(
142
- endpoint=OTEL_OTLP_TRACING_ENDPOINT,
143
- insecure=True
144
- )
145
-
146
- elif OTEL_EXPORTER_PROTOCOL == "http":
147
- return OTLPSpanExporterHTTP(
148
- endpoint=OTEL_OTLP_TRACING_ENDPOINT
149
- )
150
-
151
- return ConsoleSpanExporter()
152
-
153
-
154
- def get_metrics_exporter() -> typing.Optional[MetricExporter]:
155
- """
156
- Get OpenTelemetry metrics exporter
157
-
158
- Returns:
159
- OTLPLogExporter: OpenTelemetry metrics exporter
160
- """
161
-
162
- if OTEL_EXPORTER_TYPE == "otlp":
163
-
164
- if OTEL_EXPORTER_PROTOCOL == "grpc":
165
- return OTLPMetricExporterGRPC(
166
- endpoint=OTEL_OTLP_METRICS_ENDPOINT,
167
- insecure=True
168
- )
169
-
170
- elif OTEL_EXPORTER_PROTOCOL == "http":
171
- return OTLPMetricExporterHTTP(
172
- endpoint=OTEL_OTLP_METRICS_ENDPOINT
173
- )
174
-
175
- return ConsoleMetricExporter()
176
-
177
-
178
- if not OTEL_SDK_DISABLED or OTEL_SDK_FORCE_RUN:
179
- logger.info("OpenTelemetry enabled")
180
- logger.info(f"OpenTelemetry exporter type: {OTEL_EXPORTER_TYPE}")
181
- logger.info(f"OpenTelemetry service name: {OTEL_SERVICE_NAME}")
182
- logger.info(f"OpenTelemetry service environment: {OTEL_SERVICE_ENVIRONMENT}")
183
- logger.info(f"OpenTelemetry service release: {OTEL_SERVICE_RELEASE}")
184
- logger.info(f"OpenTelemetry trace sample rate: {OTEL_TRACE_SAMPLE_RATE}")
185
- logger.info(f"OpenTelemetry extra attributes: {OTEL_EXTRA_ATTRIBUTES}")
186
-
187
- # Define OpenTelemetry resources (e.g., service name)
188
- attributes = get_otel_attributes()
189
-
190
- # Define OpenTelemetry sampler
191
- sampler = TraceIdRatioBased(OTEL_TRACE_SAMPLE_RATE)
192
-
193
- # Define OpenTelemetry resources (e.g., service name)
194
- resource = Resource(attributes=attributes)
195
-
196
- if not OTEL_LOGGING_DISABLED:
197
- logger.info("OpenTelemetry Logging is enabled")
198
- setup_logger(resource, get_logging_exporter())
199
-
200
- if not OTEL_TRACING_DISABLED:
201
- logger.info("OpenTelemetry Tracing is enabled")
202
- setup_tracer(resource, sampler, get_span_exporter())
18
+ from mindsdb.utilities.otel.prepare import trace
19
+ from mindsdb.utilities.otel.metric_handlers import increment_otel_query_request_counter
20
+ except Exception:
21
+ pass
203
22
 
204
- if not OTEL_METRICS_DISABLED:
205
- logger.info("OpenTelemetry Metrics is enabled")
206
- setup_meter(resource, get_metrics_exporter())
@@ -1,4 +1,4 @@
1
- from mindsdb.utilities.otel import metrics, OTEL_SERVICE_NAME
1
+ from mindsdb.utilities.otel.prepare import metrics, OTEL_SERVICE_NAME
2
2
 
3
3
  _query_request_counter = None
4
4
 
@@ -23,3 +23,7 @@ def get_query_request_counter():
23
23
  )
24
24
 
25
25
  return _query_request_counter
26
+
27
+ def increment_otel_query_request_counter(metadata: dict) -> None:
28
+ query_request_counter = get_query_request_counter()
29
+ query_request_counter.add(1, metadata)
@@ -0,0 +1,198 @@
1
+ import os
2
+ import typing
3
+
4
+ from opentelemetry import trace # noqa: F401
5
+ from opentelemetry import metrics # noqa: F401
6
+ from opentelemetry.exporter.otlp.proto.grpc._log_exporter import OTLPLogExporter as OTLPLogExporterGRPC
7
+ from opentelemetry.exporter.otlp.proto.http._log_exporter import OTLPLogExporter as OTLPLogExporterHTTP
8
+ from opentelemetry.sdk._logs._internal.export import LogExporter
9
+ from opentelemetry.exporter.otlp.proto.grpc.metric_exporter import OTLPMetricExporter as OTLPMetricExporterGRPC
10
+ from opentelemetry.exporter.otlp.proto.http.metric_exporter import OTLPMetricExporter as OTLPMetricExporterHTTP
11
+ from opentelemetry.sdk.metrics.export import MetricExporter, ConsoleMetricExporter
12
+ from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter as OTLPSpanExporterGRPC
13
+ from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter as OTLPSpanExporterHTTP
14
+ from opentelemetry.sdk.trace.export import SpanExporter, ConsoleSpanExporter
15
+ from opentelemetry.sdk.resources import Resource
16
+ from opentelemetry.sdk.trace.sampling import TraceIdRatioBased
17
+
18
+ from mindsdb.utilities.otel.logger import setup_logger
19
+ from mindsdb.utilities.otel.meter import setup_meter
20
+ from mindsdb.utilities.otel.tracer import setup_tracer
21
+ from mindsdb.utilities.utils import parse_csv_attributes
22
+ from mindsdb.utilities import log
23
+
24
+ logger = log.getLogger(__name__)
25
+
26
+ # Check OpenTelemetry exporter type
27
+ OTEL_EXPORTER_TYPE = os.getenv("OTEL_EXPORTER_TYPE", "console") # console or otlp
28
+
29
+ # Define OpenTelemetry exporter protocol
30
+ OTEL_EXPORTER_PROTOCOL = os.getenv("OTEL_EXPORTER_PROTOCOL", "grpc") # grpc or http
31
+
32
+ # Define OTLP endpoint. If not set, the default OTLP endpoint will be used
33
+ OTEL_OTLP_ENDPOINT = os.getenv("OTEL_OTLP_ENDPOINT", "http://localhost:4317")
34
+
35
+ # Define OTLP logging endpoint. If not set, the default OTLP logging endpoint will be used
36
+ OTEL_OTLP_LOGGING_ENDPOINT = os.getenv("OTEL_OTLP_LOGGING_ENDPOINT", OTEL_OTLP_ENDPOINT)
37
+
38
+ # Define OTLP tracing endpoint. If not set, the default OTLP tracing endpoint will be used
39
+ OTEL_OTLP_TRACING_ENDPOINT = os.getenv("OTEL_OTLP_TRACING_ENDPOINT", OTEL_OTLP_ENDPOINT)
40
+
41
+ # Define OTLP metrics endpoint. If not set, the default OTLP metrics endpoint will be used
42
+ OTEL_OTLP_METRICS_ENDPOINT = os.getenv("OTEL_OTLP_METRICS_ENDPOINT", OTEL_OTLP_ENDPOINT)
43
+
44
+ # Define service name
45
+ OTEL_SERVICE_NAME = os.getenv("OTEL_SERVICE_NAME", "mindsdb")
46
+
47
+ # Define service instace ID
48
+ OTEL_SERVICE_INSTANCE_ID = os.getenv("OTEL_SERVICE_INSTANCE_ID", "mindsdb-instance")
49
+
50
+ # The name of the environment we"re on, by default local for development, this is set differently per-env in our Helm
51
+ # chart values files
52
+ OTEL_SERVICE_ENVIRONMENT = os.getenv("OTEL_SERVICE_ENVIRONMENT", "local").lower()
53
+
54
+ # Define service release
55
+ OTEL_SERVICE_RELEASE = os.getenv("OTEL_SERVICE_RELEASE", "local").lower()
56
+
57
+ # Define how often to capture traces
58
+ OTEL_TRACE_SAMPLE_RATE = float(os.getenv("OTEL_TRACE_SAMPLE_RATE", "1.0"))
59
+
60
+ # Define extra attributes
61
+ OTEL_EXTRA_ATTRIBUTES = os.getenv("OTEL_EXTRA_ATTRIBUTES", "")
62
+
63
+ # Define if OpenTelemetry logging is disabled. By default, it is disabled.
64
+ OTEL_LOGGING_DISABLED = os.getenv("OTEL_LOGGING_DISABLED", "true").lower() == "true"
65
+
66
+ # Define if OpenTelemetry tracing is disabled. By default, it is enabled.
67
+ OTEL_TRACING_DISABLED = os.getenv("OTEL_TRACING_DISABLED", "false").lower() == "true"
68
+
69
+ # Define if OpenTelemetry metrics is disabled. By default, it is disabled.
70
+ OTEL_METRICS_DISABLED = os.getenv("OTEL_METRICS_DISABLED", "true").lower() == "true"
71
+
72
+
73
+ def get_otel_attributes() -> dict:
74
+ """
75
+ Get OpenTelemetry attributes
76
+
77
+ Returns:
78
+ dict: OpenTelemetry attributes
79
+ """
80
+
81
+ base_attributes = {
82
+ "service.name": OTEL_SERVICE_NAME,
83
+ "service.instance.id": OTEL_SERVICE_INSTANCE_ID,
84
+ "environment": OTEL_SERVICE_ENVIRONMENT,
85
+ "release": OTEL_SERVICE_RELEASE,
86
+ }
87
+
88
+ extra_attributes = {}
89
+ try:
90
+ extra_attributes = parse_csv_attributes(OTEL_EXTRA_ATTRIBUTES)
91
+ except Exception as e:
92
+ logger.error(f"Failed to parse OTEL_EXTRA_ATTRIBUTES: {e}")
93
+
94
+ attributes = {**extra_attributes, **base_attributes} # Base attributes take precedence over extra attributes
95
+
96
+ return attributes
97
+
98
+
99
+ def get_logging_exporter() -> typing.Optional[LogExporter]:
100
+ """
101
+ Get OpenTelemetry logging exporter.
102
+
103
+ Returns:
104
+ OTLPLogExporter: OpenTelemetry logging exporter
105
+ """
106
+
107
+ if OTEL_EXPORTER_TYPE == "otlp":
108
+
109
+ if OTEL_EXPORTER_PROTOCOL == "grpc":
110
+ return OTLPLogExporterGRPC(
111
+ endpoint=OTEL_OTLP_LOGGING_ENDPOINT,
112
+ insecure=True
113
+ )
114
+
115
+ elif OTEL_EXPORTER_PROTOCOL == "http":
116
+ return OTLPLogExporterHTTP(
117
+ endpoint=OTEL_OTLP_LOGGING_ENDPOINT
118
+ )
119
+
120
+ return None
121
+
122
+
123
+ def get_span_exporter() -> SpanExporter:
124
+ """
125
+ Get OpenTelemetry span exporter
126
+
127
+ Returns:
128
+ OTLPSpanExporter: OpenTelemetry span exporter
129
+ """
130
+
131
+ if OTEL_EXPORTER_TYPE == "otlp":
132
+
133
+ if OTEL_EXPORTER_PROTOCOL == "grpc":
134
+ return OTLPSpanExporterGRPC(
135
+ endpoint=OTEL_OTLP_TRACING_ENDPOINT,
136
+ insecure=True
137
+ )
138
+
139
+ elif OTEL_EXPORTER_PROTOCOL == "http":
140
+ return OTLPSpanExporterHTTP(
141
+ endpoint=OTEL_OTLP_TRACING_ENDPOINT
142
+ )
143
+
144
+ return ConsoleSpanExporter()
145
+
146
+
147
+ def get_metrics_exporter() -> typing.Optional[MetricExporter]:
148
+ """
149
+ Get OpenTelemetry metrics exporter
150
+
151
+ Returns:
152
+ OTLPLogExporter: OpenTelemetry metrics exporter
153
+ """
154
+
155
+ if OTEL_EXPORTER_TYPE == "otlp":
156
+
157
+ if OTEL_EXPORTER_PROTOCOL == "grpc":
158
+ return OTLPMetricExporterGRPC(
159
+ endpoint=OTEL_OTLP_METRICS_ENDPOINT,
160
+ insecure=True
161
+ )
162
+
163
+ elif OTEL_EXPORTER_PROTOCOL == "http":
164
+ return OTLPMetricExporterHTTP(
165
+ endpoint=OTEL_OTLP_METRICS_ENDPOINT
166
+ )
167
+
168
+ return ConsoleMetricExporter()
169
+
170
+
171
+ logger.info("OpenTelemetry enabled")
172
+ logger.info(f"OpenTelemetry exporter type: {OTEL_EXPORTER_TYPE}")
173
+ logger.info(f"OpenTelemetry service name: {OTEL_SERVICE_NAME}")
174
+ logger.info(f"OpenTelemetry service environment: {OTEL_SERVICE_ENVIRONMENT}")
175
+ logger.info(f"OpenTelemetry service release: {OTEL_SERVICE_RELEASE}")
176
+ logger.info(f"OpenTelemetry trace sample rate: {OTEL_TRACE_SAMPLE_RATE}")
177
+ logger.info(f"OpenTelemetry extra attributes: {OTEL_EXTRA_ATTRIBUTES}")
178
+
179
+ # Define OpenTelemetry resources (e.g., service name)
180
+ attributes = get_otel_attributes()
181
+
182
+ # Define OpenTelemetry sampler
183
+ sampler = TraceIdRatioBased(OTEL_TRACE_SAMPLE_RATE)
184
+
185
+ # Define OpenTelemetry resources (e.g., service name)
186
+ resource = Resource(attributes=attributes)
187
+
188
+ if not OTEL_LOGGING_DISABLED:
189
+ logger.info("OpenTelemetry Logging is enabled")
190
+ setup_logger(resource, get_logging_exporter())
191
+
192
+ if not OTEL_TRACING_DISABLED:
193
+ logger.info("OpenTelemetry Tracing is enabled")
194
+ setup_tracer(resource, sampler, get_span_exporter())
195
+
196
+ if not OTEL_METRICS_DISABLED:
197
+ logger.info("OpenTelemetry Metrics is enabled")
198
+ setup_meter(resource, get_metrics_exporter())
@@ -0,0 +1,83 @@
1
+ def _is_in_quotes(pos: int, quote_positions: list[tuple[int, int]]) -> bool:
2
+ """
3
+ Check if a position is within any quoted string.
4
+
5
+ Args:
6
+ pos (int): The position to check.
7
+ quote_positions (list[tuple[int, int]]): A list of tuples, each containing the start and
8
+ end positions of a quoted string.
9
+
10
+ Returns:
11
+ bool: True if the position is within any quoted string, False otherwise.
12
+ """
13
+ return any(start < pos < end for start, end in quote_positions)
14
+
15
+
16
+ def clear_sql(sql: str) -> str:
17
+ '''Remove comments (--, /**/, and oracle-stype #) and trailing ';' from sql
18
+ Note: written mostly by LLM
19
+
20
+ Args:
21
+ sql (str): The SQL query to clear.
22
+
23
+ Returns:
24
+ str: The cleared SQL query.
25
+ '''
26
+ if sql is None:
27
+ raise ValueError('sql query is None')
28
+
29
+ # positions of (', ", `)
30
+ quote_positions = []
31
+ for quote_char in ["'", '"', '`']:
32
+ i = 0
33
+ while i < len(sql):
34
+ if sql[i] == quote_char and (i == 0 or sql[i - 1] != '\\'):
35
+ start = i
36
+ i += 1
37
+ while i < len(sql) and (sql[i] != quote_char or sql[i - 1] == '\\'):
38
+ i += 1
39
+ if i < len(sql):
40
+ quote_positions.append((start, i))
41
+ i += 1
42
+
43
+ # del /* */ comments
44
+ result = []
45
+ i = 0
46
+ while i < len(sql):
47
+ if i + 1 < len(sql) and sql[i:i + 2] == '/*' and not _is_in_quotes(i, quote_positions):
48
+ # skip until */
49
+ i += 2
50
+ while i + 1 < len(sql) and sql[i:i + 2] != '*/':
51
+ i += 1
52
+ if i + 1 < len(sql):
53
+ i += 2 # skip */
54
+ else:
55
+ i += 1
56
+ else:
57
+ result.append(sql[i])
58
+ i += 1
59
+
60
+ sql = ''.join(result)
61
+
62
+ # del -- and # comments
63
+ result = []
64
+ i = 0
65
+ while i < len(sql):
66
+ if i + 1 < len(sql) and sql[i:i + 2] == '--' and not _is_in_quotes(i, quote_positions):
67
+ while i < len(sql) and sql[i] != '\n':
68
+ i += 1
69
+ elif sql[i] == '#' and not _is_in_quotes(i, quote_positions):
70
+ while i < len(sql) and sql[i] != '\n':
71
+ i += 1
72
+ else:
73
+ result.append(sql[i])
74
+ i += 1
75
+
76
+ sql = ''.join(result)
77
+
78
+ # del ; at the end
79
+ sql = sql.rstrip()
80
+ if sql and sql[-1] == ';':
81
+ sql = sql[:-1].rstrip()
82
+
83
+ return sql.strip(' \n\t')