Skip to content

Commit

Permalink
fix: opentel trash + bug
Browse files Browse the repository at this point in the history
  • Loading branch information
m4dm4rtig4n committed Aug 1, 2024
1 parent 9748bc7 commit 8183fa2
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 47 deletions.
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"]
42 changes: 20 additions & 22 deletions src/config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion src/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
43 changes: 22 additions & 21 deletions src/models/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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."""
Expand Down Expand Up @@ -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()
Expand All @@ -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"]}'
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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()

Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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()
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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()
Expand Down Expand Up @@ -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:
Expand Down
1 change: 0 additions & 1 deletion src/routers/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 8183fa2

Please sign in to comment.