Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Histogram.record() producing inaccurate start_time_unix_nano value #4123

Closed
williamdoddsb365 opened this issue Aug 15, 2024 · 2 comments · Fixed by #4137
Closed

Histogram.record() producing inaccurate start_time_unix_nano value #4123

williamdoddsb365 opened this issue Aug 15, 2024 · 2 comments · Fixed by #4137
Assignees
Labels
bug Something isn't working

Comments

@williamdoddsb365
Copy link

williamdoddsb365 commented Aug 15, 2024

Describe your environment

OS: CentOS 7
Python version: Python3.11
SDK version: 1.26.0
API version: 1.26.0

What happened?

When using Python OpenTelemetry (OTel) to record histogram metrics with unique integer attributes, all recorded histograms are showing a static start_time_unix_nano value in the OpenTelemetry logs. Despite different timestamps being recorded for each metric point, the start_time_unix_nano remains constant across all data points, which is not expected behavior. This issue does not occur when static attribute values are used.

Steps to Reproduce

There is an easy to follow MRE here:
https://github.com/williamdoddsb365/otel_histogram_issue

Expected Result

The start_time_unix_nano value should increment for each recorded histogram data point, regardless of whether the attributes are static or dynamic.

Actual Result

The start_time_unix_nano remains constant across all data points when dynamic attributes are sent.

Additional context

No response

Would you like to implement a fix?

None

@williamdoddsb365 williamdoddsb365 added the bug Something isn't working label Aug 15, 2024
@emdneto
Copy link
Member

emdneto commented Aug 15, 2024

tested with this small snippet against the main branch to reproduce the behavior;

pip install opentelemetry-api opentelemetry-sdk
import time

from opentelemetry.metrics import (
    get_meter_provider,
    set_meter_provider,
)
from opentelemetry.sdk.metrics import MeterProvider, Histogram
from opentelemetry.sdk.metrics.export import (
    ConsoleMetricExporter,
    PeriodicExportingMetricReader,
    AggregationTemporality
)

temporality = {
    Histogram: AggregationTemporality.DELTA,
}

reader = PeriodicExportingMetricReader(exporter=ConsoleMetricExporter(preferred_temporality=temporality), export_interval_millis=1_000)
provider = MeterProvider(metric_readers=[reader,])
set_meter_provider(provider)

meter = get_meter_provider().get_meter(__name__)
hist = meter.create_histogram("my_histogram", description="duration", unit="s")

if __name__ == "__main__":
    i = 0
    while True:
        time.sleep(1)
        hist.record(1, {"foo": f"bar{i}"}) # constant start_time_unix_nano
        # hist.record(1, {"foo": f"bar"}) # incremental start_time_unix_nano
        i += 1

example return python example.py | grep time

"start_time_unix_nano": 1724166782606866000,
"time_unix_nano": 1724166783605785000,
"start_time_unix_nano": 1724166782606866000,
"time_unix_nano": 1724166783605785000,
"start_time_unix_nano": 1724166782606866000,
"time_unix_nano": 1724166783605785000,
"start_time_unix_nano": 1724166782606866000,
"time_unix_nano": 1724166783605785000,
"start_time_unix_nano": 1724166782606866000,
"time_unix_nano": 1724166783605785000,
"start_time_unix_nano": 1724166782606866000,
"time_unix_nano": 1724166783605785000,
"start_time_unix_nano": 1724166782606866000,
"time_unix_nano": 1724166783605785000,
"start_time_unix_nano": 1724166782606866000,
"time_unix_nano": 1724166783605785000,
"start_time_unix_nano": 1724166782606866000,
"time_unix_nano": 1724166783605785000,
"start_time_unix_nano": 1724166782606866000,
"time_unix_nano": 1724166783605785000,
"start_time_unix_nano": 1724166782606866000,
"time_unix_nano": 1724166784618787000,
"start_time_unix_nano": 1724166782606866000,
"time_unix_nano": 1724166784618787000,

@williamdoddsb365
Copy link
Author

williamdoddsb365 commented Aug 15, 2024

Thanks for reporting. Also tested with this small snippet against the main branch to reproduce the issue;

pip install opentelemetry-api opentelemetry-sdk
import time

from opentelemetry.metrics import (
    get_meter_provider,
    set_meter_provider,
)
from opentelemetry.sdk.metrics import MeterProvider, Histogram
from opentelemetry.sdk.metrics.export import (
    ConsoleMetricExporter,
    PeriodicExportingMetricReader,
    AggregationTemporality
)

temporality = {
    Histogram: AggregationTemporality.DELTA,
}

reader = PeriodicExportingMetricReader(exporter=ConsoleMetricExporter(preferred_temporality=temporality), export_interval_millis=1_000)
provider = MeterProvider(metric_readers=[reader,])
set_meter_provider(provider)

meter = get_meter_provider().get_meter(__name__)
hist = meter.create_histogram("my_histogram", description="duration", unit="s")

if __name__ == "__main__":
    i = 0
    while True:
        time.sleep(1)
        hist.record(1, {"foo": f"bar{i}"}) # constant start_time_unix_nano
        hist.record(1, {"foo": f"bar"}) # incremental start_time_unix_nano
        i += 1

Thanks for providing a more concise example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants