all: refactor by extraction code for conversion from timestampStr to microseconds
See original GitHub issueI noticed while reviewing https://github.com/census-instrumentation/opencensus-python/pull/327#discussion_r220030262 that there is a legacy duplication of code to convert between timestamp strings to microseconds that is https://github.com/census-instrumentation/opencensus-python/blob/c96083589792a0c278193af1a227c21f9c6c6e97/opencensus/trace/exporters/zipkin_exporter.py#L154-L167
and with that PR there will be three sites with the same code pattern.
I propose that we refactor that code by extracting it to a small helper function
def datetimeStrToMicroseconds(timestamp):
"""
Convert a timestamp string into a microsecond value as required by Zipkin.
It returns an int
"""
dt = datetime.datetime.strptime(timestamp, ISO_DATETIME_REGEX)
mUs = (calendar.timegm(dt.timetuple()) * 1e6) + dt.microsecond
return int(round(mUs))
which will then reduce all those call sites to simply
start_timestamp_mus = datetimeStrToMicroseconds(span.start_time)
end_timestamp_mus = datetimeStrToMicroseconds(span.end_time)
epoch_time_mus = datetimeStrToMicroseconds(time_event.timestamp)
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:5 (5 by maintainers)
Top Results From Across the Web
esl/usec: Convert anything to microseconds and back. - GitHub
Converts anything* to microseconds and back. Tired of implementing the same timestamp conversion functions in different projects under different names?
Read more >Get a timestamp in C in microseconds? - Stack Overflow
You have two choices for getting a microsecond timestamp. ... To // convert the whole struct to nanoseconds, do this: nanoseconds ...
Read more >Refactoring 008 — Convert Variables to Constant
I call that variable a constant. “Refactoring 008 — Convert Variables to Constant” is published by Maximiliano Contieri in Level Up Coding.
Read more >Convert | AppCode Documentation - JetBrains
Convert refactorings allow converting methods to functions or blocks and vice versa, as well as converting properties to instance variables.
Read more >Java Program to Convert Milliseconds to Minutes and Seconds
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Great catch, perhaps then we need to put that in the utilities path if any exists, so that we can use that function across the entire repo.
We should do same here exporter/jaeger: https://github.com/census-instrumentation/opencensus-python/blob/c96083589792a0c278193af1a227c21f9c6c6e97/opencensus/trace/exporters/jaeger_exporter.py#L179-L189