From 8183fa241a8a17279e57fd8a6f098cfc3e617216 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20VALENTIN?= Date: Thu, 1 Aug 2024 14:05:51 +0200 Subject: [PATCH] fix: opentel trash + bug --- Dockerfile | 5 +++-- src/config/main.py | 42 ++++++++++++++++++++---------------------- src/const.py | 2 +- src/models/jobs.py | 43 ++++++++++++++++++++++--------------------- src/routers/data.py | 1 - 5 files changed, 46 insertions(+), 47 deletions(-) diff --git a/Dockerfile b/Dockerfile index cfe3a59..06c494d 100755 --- a/Dockerfile +++ b/Dockerfile @@ -40,8 +40,8 @@ COPY ./src /app RUN pip install -r /app/requirements.txt # REMOVE RUST -RUN if [ "$TARGETPLATFORM" = "linux/arm/v7" ]; then \ - /usr/local/lib/rustlib/uninstall.sh; \ +RUN if [ "$TARGETPLATFORM" = "linux/arm/v7" ] || [ "$TARGETPLATFORM" = "linux/arm/v6" ]; then \ + /usr/local/lib/rustlib/uninstall.sh; \ fi RUN mkdir /data @@ -67,5 +67,6 @@ LABEL \ # CLEAN RUN rm -rf /var/lib/apt/lists/* +RUN apt remove -y git libpq-dev gcc g++ CMD ["python", "-u", "/app/main.py"] diff --git a/src/config/main.py b/src/config/main.py index e0a20a2..4b5bde0 100755 --- a/src/config/main.py +++ b/src/config/main.py @@ -12,7 +12,6 @@ from opentelemetry.instrumentation.sqlalchemy import SQLAlchemyInstrumentor from opentelemetry.sdk.trace import Resource, TracerProvider from opentelemetry.sdk.trace.export import BatchSpanProcessor -from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter from __version__ import VERSION from config.backend import Backend @@ -257,38 +256,37 @@ def ssl_config(self): def setup_tracing(self): """OTEL setup.""" - if self.config.opentelemetry.enable: # pragma: no cover + if self.config.opentelemetry.enable: # no pragma: no cover RequestsInstrumentor().instrument() - - resource_attributes = { - "service.name": self.config.opentelemetry.service_name, - "telemetry.version": VERSION, - "service.version": VERSION, - "env": self.config.opentelemetry.environment, - "Deployment.environment": self.config.opentelemetry.environment, - } - resource = Resource.create(resource_attributes) - provider = TracerProvider(resource=resource) - otlp_exporter = ( - OTLPSpanExporter(endpoint=self.config.opentelemetry.endpoint, insecure=True) - if self.config.opentelemetry.enable - else InMemorySpanExporter() - ) - processor = BatchSpanProcessor(otlp_exporter) - provider.add_span_processor(processor) - trace.set_tracer_provider(provider) + resource_attributes = { + "service.name": self.config.opentelemetry.service_name, + "telemetry.version": VERSION, + "service.version": VERSION, + "env": self.config.opentelemetry.environment, + "Deployment.environment": self.config.opentelemetry.environment, + } + resource = Resource.create(resource_attributes) + provider = TracerProvider(resource=resource) + processor = BatchSpanProcessor( + OTLPSpanExporter(endpoint=self.config.opentelemetry.endpoint, insecure=True), + export_timeout_millis=5, + ) + provider.add_span_processor(processor) + trace.set_tracer_provider(provider) + else: + trace.set_tracer_provider(trace.NoOpTracerProvider()) self.tracer = trace.get_tracer_provider().get_tracer("main") self.tracing_sqlalchemy() def tracing_sqlalchemy(self): """SQLAchemy Tracing.""" - if "sqlalchemy" in self.config.opentelemetry.extension: + if self.config.opentelemetry.enable and "sqlalchemy" in self.config.opentelemetry.extension: logging.debug("[OpenTelemetry] SQLAchemy loaded") SQLAlchemyInstrumentor().instrument(enable_commenter=True, commenter_options={}) def tracing_fastapi(self, app): """FastAPI Tracing.""" - if "fastapi" in self.config.opentelemetry.extension: + if self.config.opentelemetry.enable and "fastapi" in self.config.opentelemetry.extension: logging.debug("[OpenTelemetry] FastAPI loaded") FastAPIInstrumentor.instrument_app(app) diff --git a/src/const.py b/src/const.py index 40b151d..a4d2166 100755 --- a/src/const.py +++ b/src/const.py @@ -7,7 +7,7 @@ LOG_FORMAT_DATE = "%Y-%m-%d %H:%M:%S" URL = "https://myelectricaldata.fr" -URL_CONFIG_FILE = "https://github.com/MyElectricalData/myelectricaldata_import/blob/main/config.exemple.yaml" +URL_CONFIG_FILE = "https://github.com/MyElectricalData/myelectricaldata_import/blob/main/config.example.yaml" USAGE_POINT_ID_LENGTH = 14 diff --git a/src/models/jobs.py b/src/models/jobs.py index b7dde37..102e310 100644 --- a/src/models/jobs.py +++ b/src/models/jobs.py @@ -3,6 +3,7 @@ import logging import time import traceback +from typing import List from config.main import APP_CONFIG from config.myelectricaldata import UsagePointId @@ -29,13 +30,13 @@ class Job: def __init__(self, usage_point_id=None): self.usage_point_id = usage_point_id - self.usage_point_config = {} - self.wait_job_start = 10 - self.tempo_enable = False + self.usage_point_config: UsagePointId = {} + self.wait_job_start: int = 10 + self.tempo_enable: bool = False if self.usage_point_id is None: - self.usage_points_all = DatabaseUsagePoints().get_all() + self.usage_points_all: List[UsagePointId] = DatabaseUsagePoints().get_all() else: - self.usage_points_all = [DatabaseUsagePoints(self.usage_point_id).get()] + self.usage_points_all: List[UsagePointId] = [DatabaseUsagePoints(self.usage_point_id).get()] def boot(self): """Boots the import job.""" @@ -171,7 +172,7 @@ def get_gateway_status(self): Returns: None """ - detail = "Récupération du statut de la passerelle :" + detail = "Récupération du statut de la passerelle" try: title(detail) Status(headers=self.header_generate(token=False)).ping() @@ -193,7 +194,7 @@ def get_account_status(self): def run(): usage_point_id = self.usage_point_config.usage_point_id - title(f"[{usage_point_id}] {detail} :") + title(f"[{usage_point_id}] {detail}") status = Status(headers=self.header_generate()).status(usage_point_id=usage_point_id) if status.get("error"): message = f'{status["status_code"]} - {status["description"]["detail"]}' @@ -236,7 +237,7 @@ def get_contract(self): def run(usage_point_config): usage_point_id = usage_point_config.usage_point_id - title(f"[{usage_point_id}] {detail} :") + title(f"[{usage_point_id}] {detail}") Contract( headers=self.header_generate(), usage_point_id=usage_point_id, @@ -273,7 +274,7 @@ def get_addresses(self): def run(usage_point_config): usage_point_id = usage_point_config.usage_point_id - title(f"[{usage_point_id}] {detail} :") + title(f"[{usage_point_id}] {detail}") Address(headers=self.header_generate(), usage_point_id=usage_point_id).get() export_finish() @@ -295,7 +296,7 @@ def get_consumption(self): def run(usage_point_config): usage_point_id = usage_point_config.usage_point_id - title(f"[{usage_point_id}] {detail} :") + title(f"[{usage_point_id}] {detail}") if hasattr(usage_point_config, "consumption") and usage_point_config.consumption: Daily(headers=self.header_generate(), usage_point_id=usage_point_id).get() export_finish() @@ -320,7 +321,7 @@ def get_consumption_detail(self): def run(usage_point_config): usage_point_id = usage_point_config.usage_point_id - title(f"[{usage_point_id}] {detail} :") + title(f"[{usage_point_id}] {detail}") if hasattr(usage_point_config, "consumption_detail") and usage_point_config.consumption_detail: Detail(headers=self.header_generate(), usage_point_id=usage_point_id).get() export_finish() @@ -345,7 +346,7 @@ def get_production(self): def run(usage_point_config): usage_point_id = usage_point_config.usage_point_id - title(f"[{usage_point_id}] {detail} :") + title(f"[{usage_point_id}] {detail}") if hasattr(usage_point_config, "production") and usage_point_config.production: Daily( headers=self.header_generate(), @@ -374,7 +375,7 @@ def get_production_detail(self): def run(usage_point_config): usage_point_id = usage_point_config.usage_point_id - title(f"[{usage_point_id}] {detail} :") + title(f"[{usage_point_id}] {detail}") if hasattr(usage_point_config, "production_detail") and usage_point_config.production_detail: Detail( headers=self.header_generate(), @@ -402,7 +403,7 @@ def get_consumption_max_power(self): detail = "Récupération de la puissance maximum journalière" def run(usage_point_id: str, usage_point_config: UsagePointId) -> None: - title(f"[{usage_point_id}] {detail} :") + title(f"[{usage_point_id}] {detail}") if getattr(usage_point_config, "consumption_max_power", True): Power(headers=self.header_generate(), usage_point_id=usage_point_id).get() export_finish() @@ -425,11 +426,11 @@ def run(usage_point_id: str, usage_point_config: UsagePointId) -> None: def get_tempo(self): """Get tempo from gateway.""" try: - title("Récupération des données Tempo :") + title("Récupération des données Tempo") Tempo().fetch() - title("Calcul des jours Tempo :") + title("Calcul des jours Tempo") Tempo().calc_day() - title("Récupération des tarifs Tempo :") + title("Récupération des tarifs Tempo") Tempo().fetch_price() export_finish() except Exception as e: @@ -440,7 +441,7 @@ def get_tempo(self): def get_ecowatt(self): """Get ecowatt from gateway.""" try: - title("Récupération des données EcoWatt :") + title("Récupération des données EcoWatt") Ecowatt().fetch() export_finish() except Exception as e: @@ -454,7 +455,7 @@ def stat_price(self): def run(usage_point_config): usage_point_id = usage_point_config.usage_point_id - title(f"[{usage_point_id}] {detail} :") + title(f"[{usage_point_id}] {detail}") if hasattr(usage_point_config, "consumption_detail") and usage_point_config.consumption_detail: logging.info("Consommation :") Stat(usage_point_id=usage_point_id, measurement_direction="consumption").generate_price() @@ -491,9 +492,9 @@ def run(usage_point_id, target): if APP_CONFIG.home_assistant: if APP_CONFIG.mqtt: if self.usage_point_id is None: - for usage_point_id, usage_point_config in self.usage_points_all.items(): + for usage_point_config in self.usage_points_all: if usage_point_config.enable: - run(usage_point_id, target) + run(usage_point_config.usage_point_id, target) else: run(self.usage_point_id, target) else: diff --git a/src/routers/data.py b/src/routers/data.py index 6fa012d..67da62c 100644 --- a/src/routers/data.py +++ b/src/routers/data.py @@ -15,7 +15,6 @@ from database.detail import DatabaseDetail from database.max_power import DatabaseMaxPower from database.usage_points import DatabaseUsagePoints -from db_schema import Contracts from doc import DOCUMENTATION from models.ajax import Ajax