beamlit 0.0.31rc39__py3-none-any.whl → 0.0.31rc40__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
@@ -8,22 +8,16 @@ from opentelemetry.exporter.otlp.proto.grpc.metric_exporter import (
8
8
  from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import (
9
9
  OTLPSpanExporter,
10
10
  )
11
- from opentelemetry.instrumentation.fastapi import ( # type: ignore
12
- FastAPIInstrumentor,
13
- )
14
- from opentelemetry.instrumentation.httpx import ( # type: ignore
15
- HTTPXClientInstrumentor,
16
- )
17
- from opentelemetry.instrumentation.logging import ( # type: ignore
18
- LoggingInstrumentor,
19
- )
11
+ from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
12
+ from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor
13
+ from opentelemetry.instrumentation.logging import LoggingInstrumentor
14
+ from opentelemetry.metrics import NoOpMeterProvider
20
15
  from opentelemetry.sdk.metrics import MeterProvider
21
- from opentelemetry.sdk.metrics.export import (
22
- PeriodicExportingMetricReader,
23
- )
16
+ from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader
24
17
  from opentelemetry.sdk.resources import Resource
25
18
  from opentelemetry.sdk.trace import TracerProvider
26
19
  from opentelemetry.sdk.trace.export import BatchSpanProcessor
20
+ from opentelemetry.trace import NoOpTracerProvider
27
21
  from typing_extensions import Dict
28
22
 
29
23
  from .settings import get_settings
@@ -57,11 +51,20 @@ def get_resource_attributes() -> Dict[str, Any]:
57
51
  return resources_dict
58
52
 
59
53
 
60
- def get_metrics_exporter() -> OTLPMetricExporter:
54
+ def get_metrics_exporter() -> OTLPMetricExporter | None:
55
+ settings = get_settings()
56
+ if settings is None:
57
+ raise Exception("Settings are not initialized")
58
+ if not settings.enable_opentelemetry:
59
+ # Return None or a NoOpExporter equivalent
60
+ return None
61
61
  return OTLPMetricExporter()
62
62
 
63
63
 
64
- def get_span_exporter() -> OTLPSpanExporter:
64
+ def get_span_exporter() -> OTLPSpanExporter | None:
65
+ settings = get_settings()
66
+ if not settings.enable_opentelemetry:
67
+ return None
65
68
  return OTLPSpanExporter()
66
69
 
67
70
 
@@ -71,6 +74,16 @@ def instrument_app(app: FastAPI):
71
74
  settings = get_settings()
72
75
  if settings is None:
73
76
  raise Exception("Settings are not initialized")
77
+
78
+ if not settings.enable_opentelemetry:
79
+ # Use NoOp implementations to stub tracing and metrics
80
+ trace.set_tracer_provider(NoOpTracerProvider())
81
+ tracer = trace.get_tracer(__name__)
82
+
83
+ metrics.set_meter_provider(NoOpMeterProvider())
84
+ meter = metrics.get_meter(__name__)
85
+ return
86
+
74
87
  resource = Resource.create(
75
88
  {
76
89
  "service.name": settings.name,
@@ -78,24 +91,33 @@ def instrument_app(app: FastAPI):
78
91
  "service.workspace": settings.workspace,
79
92
  }
80
93
  )
81
- # Set up the TracerProvider
82
- trace_provider = TracerProvider(resource=resource)
83
- span_processor = BatchSpanProcessor(get_span_exporter())
84
- trace_provider.add_span_processor(span_processor)
85
- trace.set_tracer_provider(trace_provider)
86
- tracer = trace_provider.get_tracer(__name__)
87
-
88
- metrics_exporter = PeriodicExportingMetricReader(get_metrics_exporter())
89
- meter_provider = MeterProvider(
90
- resource=resource, metric_readers=[metrics_exporter]
91
- )
92
- metrics.set_meter_provider(meter_provider)
93
- meter = meter_provider.get_meter(__name__)
94
94
 
95
- FastAPIInstrumentor.instrument_app( # type: ignore
96
- app=app, tracer_provider=trace_provider, meter_provider=meter_provider
95
+ # Set up the TracerProvider if not already set
96
+ if not isinstance(trace.get_tracer_provider(), TracerProvider):
97
+ trace_provider = TracerProvider(resource=resource)
98
+ span_processor = BatchSpanProcessor(get_span_exporter())
99
+ trace_provider.add_span_processor(span_processor)
100
+ trace.set_tracer_provider(trace_provider)
101
+ tracer = trace_provider.get_tracer(__name__)
102
+ else:
103
+ tracer = trace.get_tracer(__name__)
104
+
105
+ # Set up the MeterProvider if not already set
106
+ if not isinstance(metrics.get_meter_provider(), MeterProvider):
107
+ metrics_exporter = PeriodicExportingMetricReader(get_metrics_exporter())
108
+ meter_provider = MeterProvider(
109
+ resource=resource, metric_readers=[metrics_exporter]
110
+ )
111
+ metrics.set_meter_provider(meter_provider)
112
+ meter = meter_provider.get_meter(__name__)
113
+ else:
114
+ meter = metrics.get_meter(__name__)
115
+
116
+ # Only instrument the app when OpenTelemetry is enabled
117
+ FastAPIInstrumentor.instrument_app(
118
+ app=app, tracer_provider=trace.get_tracer_provider(), meter_provider=metrics.get_meter_provider()
97
119
  )
98
- HTTPXClientInstrumentor().instrument(meter_provider=meter_provider) # type: ignore
99
- LoggingInstrumentor(tracer_provider=trace_provider).instrument(
120
+ HTTPXClientInstrumentor().instrument(meter_provider=metrics.get_meter_provider())
121
+ LoggingInstrumentor(tracer_provider=trace.get_tracer_provider()).instrument(
100
122
  set_logging_format=True
101
123
  )
@@ -67,6 +67,7 @@ class Settings(BaseSettings):
67
67
  mcp_hub_url: str = Field(default="https://mcp-hub-server.beamlit.workers.com")
68
68
  registry_url: str = Field(default="https://us.registry.beamlit.com")
69
69
  log_level: str = Field(default="INFO")
70
+ enable_opentelemetry: bool = Field(default=False)
70
71
  agent: SettingsAgent = SettingsAgent()
71
72
  server: SettingsServer = SettingsServer()
72
73
  authentication: SettingsAuthentication = SettingsAuthentication()
beamlit/serve/app.py CHANGED
@@ -41,13 +41,14 @@ logger.info(
41
41
  f" on {settings.server.host}:{settings.server.port}"
42
42
  )
43
43
 
44
- Traceloop.init(
45
- app_name=settings.name,
46
- exporter=get_span_exporter(),
47
- metrics_exporter=get_metrics_exporter(),
48
- resource_attributes=get_resource_attributes(),
49
- should_enrich_metrics=os.getenv("ENRICHED_METRICS", "false") == "true",
50
- )
44
+ if settings.enable_opentelemetry:
45
+ Traceloop.init(
46
+ app_name=settings.name,
47
+ exporter=get_span_exporter(),
48
+ metrics_exporter=get_metrics_exporter(),
49
+ resource_attributes=get_resource_attributes(),
50
+ should_enrich_metrics=os.getenv("ENRICHED_METRICS", "false") == "true",
51
+ )
51
52
 
52
53
  app = FastAPI(docs_url=None, redoc_url=None)
53
54
  app.add_middleware(
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: beamlit
3
- Version: 0.0.31rc39
3
+ Version: 0.0.31rc40
4
4
  Summary: Add your description here
5
5
  Author-email: cploujoux <ch.ploujoux@gmail.com>
6
6
  Requires-Python: >=3.12
@@ -131,10 +131,10 @@ beamlit/authentication/device_mode.py,sha256=tmr22gllKOZwBRub_QjF5pYa425x-nE8tQN
131
131
  beamlit/common/__init__.py,sha256=vj4_boIBVitMsaQR8BqBqE2eupOIh6MWBAYlYyCCH98,341
132
132
  beamlit/common/error.py,sha256=f9oJDFxhoHK-vpjxBgEp0NwWIk0N_THPemUI7uQxVzU,270
133
133
  beamlit/common/generate.py,sha256=LtdCju_QayRS4lZrrb_0VHqWWvTcv4Mbf-iV1TB_Qko,7522
134
- beamlit/common/instrumentation.py,sha256=MsBDfFcMYqGDiHHj4j5hLHE4EWxZExkhmCeFS3SKzJY,3181
134
+ beamlit/common/instrumentation.py,sha256=GVYeat7qCcqzDoKSYig3s8ZCC172R9JiQIr3Evv3kik,4293
135
135
  beamlit/common/logger.py,sha256=nN_dSOl4bs13QU3Rod-w3e3jYOnlSrHx3_bs-ACY6Aw,1115
136
136
  beamlit/common/secrets.py,sha256=sid81bOe3LflkMKDHwBsBs9nIju8bp5-v9qU9gkyNMc,212
137
- beamlit/common/settings.py,sha256=gKg8YAQ0NJymY1J-D55F1_wfrPwGb8Jmp5ojrjveZwI,5869
137
+ beamlit/common/settings.py,sha256=_4oCVrJZOMaTZoK2Zzo2DWqtUyBsspuOH3iAJ_nU0tw,5923
138
138
  beamlit/common/utils.py,sha256=jouz5igBvT37Xn_e94-foCHyQczVim-UzVcoIF6RWJ4,657
139
139
  beamlit/deploy/__init__.py,sha256=GS7l7Jtm2yKs7iNLKcfjYO-rAhUzggQ3xiYSf3oxLBY,91
140
140
  beamlit/deploy/deploy.py,sha256=5rcPJhz0xtjG_9EUpZuoSwgIkqbE-LPqSF1FoXarMQM,9915
@@ -252,10 +252,10 @@ beamlit/models/websocket_channel.py,sha256=jg3vN7yS_oOIwGtndtIUr1LsyEA58RXLXahqS
252
252
  beamlit/models/workspace.py,sha256=l__bIpbA4oJvxXo7UbEoCcqkvu9MiNt5aXXpZ3bgwHg,4309
253
253
  beamlit/models/workspace_labels.py,sha256=WbnUY6eCTkUNdY7hhhSF-KQCl8fWFfkCf7hzCTiNp4A,1246
254
254
  beamlit/models/workspace_user.py,sha256=70CcifQWYbeWG7TDui4pblTzUe5sVK0AS19vNCzKE8g,3423
255
- beamlit/serve/app.py,sha256=ys7jymdAY1K2FLOrUnNWJA9_tr7B9VGvKpF1F03glEU,3492
255
+ beamlit/serve/app.py,sha256=QzReZZjGFQWhNA1XBDq81rsn9zpEihtLZLrfZi9ThT0,3554
256
256
  beamlit/serve/middlewares/__init__.py,sha256=1dVmnOmhAQWvWktqHkKSIX-YoF6fmMU8xkUQuhg_rJU,148
257
257
  beamlit/serve/middlewares/accesslog.py,sha256=Mu4T4_9OvHybjA0ApzZFpgi2C8f3X1NbUk-76v634XM,631
258
258
  beamlit/serve/middlewares/processtime.py,sha256=lDAaIasZ4bwvN-HKHvZpaD9r-yrkVNZYx4abvbjbrCg,411
259
- beamlit-0.0.31rc39.dist-info/METADATA,sha256=LExxyq0JitobbxrS5Nq3N-sdAfbLaVGd9JW4kZ9FUV4,2405
260
- beamlit-0.0.31rc39.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
261
- beamlit-0.0.31rc39.dist-info/RECORD,,
259
+ beamlit-0.0.31rc40.dist-info/METADATA,sha256=aozXRVioMl7voT5SN7PxYABY_13VI_CEw913zgeo1lo,2405
260
+ beamlit-0.0.31rc40.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
261
+ beamlit-0.0.31rc40.dist-info/RECORD,,