Skip to content

OpenTelemetry support#164

Merged
samuelrince merged 44 commits intomlco2:mainfrom
Afoodi:dev/otel
Jul 31, 2025
Merged

OpenTelemetry support#164
samuelrince merged 44 commits intomlco2:mainfrom
Afoodi:dev/otel

Conversation

@samuelrince
Copy link
Collaborator

@samuelrince samuelrince commented Jul 18, 2025

This PR adds support for OpenTelemetry into EcoLogits. This will allow users to export EcoLogits metrics directly into their monitoring stack.

Thanks to Afoodi for contributing to this new feature! 🤗

Documentation summary

Initialize OpenTelemetry within EcoLogits:

from ecologits import EcoLogits
from openai import OpenAI

# Configure OpenTelemetry endpoint in EcoLogits
EcoLogits.init(
    providers=["openai"],
    opentelemetry_endpoint='https://localhost:4318/v1/metrics'
)

client = OpenAI()

# All requests will log ecologits metrics
response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Tell me a funny joke!"}]
)

Tracked metrics:

Metric name Type Description
ecologits_requests_total Counter Number of requests
ecologits_input_tokens_ratio Gauge Number of input tokens
ecologits_output_tokens_ratio Gauge Number of output tokens
ecologits_request_latency_seconds Gauge Request latency in seconds
ecologits_energy_joules Gauge Average energy consumption of the request in joules
ecologits_gwp_gCO2eq Gauge Average GWP impacts of the request in gCO2eq
ecologits_adpe_gSbeq Gauge Average ADPe impacts of the request in gSbeq
ecologits_pe_joules Gauge Average PE impacts of the request in joules

Use OpenTelemetry labels:

from ecologits import EcoLogits
from openai import OpenAI

EcoLogits.init(
    providers=["openai"], 
    opentelemetry_endpoint="http://localhost:4318/v1/metrics"
)
client = OpenAI()

# with a context manager
with EcoLogits.label(my_custom_label="test"):
    response = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": "Tell me a funny joke"}]
    )
    
# or, with a decorator
@EcoLogits.label(task="summarization")
def summarize(text: str) -> str:
    response = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": f"Summarize this:\n\n{text}"}]
    )
    return response.choices[0].message.content

Work in progress:

  • Make OpenTelemetry an optional dependency
  • Integration with all other providers

@samuelrince samuelrince marked this pull request as draft July 18, 2025 16:00
@samuelrince
Copy link
Collaborator Author

Ping @jaydesl and @sauraisg, can you please review the changes? :)

We still have a bit of work on our side to adapt this to all providers, but it will be done and released by the end of next week for sure!

@samuelrince samuelrince marked this pull request as ready for review July 21, 2025 16:36
@samuelrince samuelrince requested a review from adrienbanse July 21, 2025 16:36
@samuelrince
Copy link
Collaborator Author

Hey @adrienbanse, if you could double-check this PR (and especially the documentation), that would be great! For "integration tests," I have everything working locally, and I will make a special private repo for now.

@samuelrince samuelrince mentioned this pull request Jul 23, 2025
2 tasks
@henrikrexed
Copy link

I would recommend to not report a counter , but rather a Gauge or Histograms.
Counter are create for reporting the per second value

@sauraisg
Copy link
Contributor

Ping @jaydesl and @sauraisg, can you please review the changes? :)

We still have a bit of work on our side to adapt this to all providers, but it will be done and released by the end of next week for sure!

Hi ! Sorry for the delay, I didn't have time to check it before but it seems good to me ! :)

@adrienbanse
Copy link
Collaborator

@samuelrince I just proofread the docs, looks great to me! Sorry but I think I won't have time to proofread the code as well.

@samuelrince samuelrince merged commit 3a50649 into mlco2:main Jul 31, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants