-
Notifications
You must be signed in to change notification settings - Fork 75
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
feat: Expose metric dictionary to make logging metrics as JSON easier #2162
base: main
Are you sure you want to change the base?
feat: Expose metric dictionary to make logging metrics as JSON easier #2162
Conversation
CodSpeed Performance ReportMerging #2162 will not alter performanceComparing Summary
|
2c3ec58
to
815b748
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #2162 +/- ##
=======================================
Coverage 91.40% 91.40%
=======================================
Files 63 63
Lines 5291 5294 +3
Branches 676 676
=======================================
+ Hits 4836 4839 +3
Misses 321 321
Partials 134 134 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
64e71fd
to
75dc465
Compare
8edd581
to
2125742
Compare
2125742
to
5add1e7
Compare
5add1e7
to
2f2cc7d
Compare
2f2cc7d
to
2d3bf56
Compare
2d3bf56
to
fe99021
Compare
a80e01c
to
de62b92
Compare
de62b92
to
ac3e32a
Compare
ac3e32a
to
2f89e10
Compare
f7542b5
to
6de3bf6
Compare
This reverts commit 6de3bf6.
Reviewer's Guide by SourceryThis pull request introduces a new Sequence diagram for logging a metricsequenceDiagram
participant Logger
participant metrics.log
participant Point
Logger->>metrics.log: info("METRIC", extra={"point": point.to_dict()})
activate metrics.log
metrics.log->>Point: to_dict()
activate Point
Point-->>metrics.log: Returns dictionary
deactivate Point
metrics.log-->>Logger: Returns
deactivate metrics.log
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @edgarrmondragon - I've reviewed your changes - here's some feedback:
Overall Comments:
- Consider renaming
SingerMetricsFormatter
toJSONMetricsFormatter
to better reflect its purpose. - The default logging config should include the pythonjsonlogger library as a dependency.
Here's what I looked at during the review
- 🟢 General issues: all looks good
- 🟢 Security: all looks good
- 🟢 Testing: all looks good
- 🟡 Complexity: 1 issue found
- 🟢 Documentation: all looks good
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
def to_json(self) -> str: | ||
"""Convert this measure to a JSON object. | ||
|
||
class SingerMetricsFormatter(logging.Formatter): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (complexity): Consider using a JSON logging library or formatting the metric explicitly in the log call to avoid the complexity of a custom formatter for logging metrics as JSON..
The new custom formatter introduces extra complexity by manually injecting the JSON-formatted metric into each log record. If the goal is simply to log metrics as JSON, you could simplify this by using a JSON logging library (like python-json-logger) or by formatting the metric before logging. For example:
Using python-json-logger:
from pythonjsonlogger import jsonlogger
import logging
handler = logging.StreamHandler()
formatter = jsonlogger.JsonFormatter('%(asctime)s %(levelname)s %(message)s %(point)s')
handler.setFormatter(formatter)
logger = logging.getLogger("my_logger")
logger.addHandler(handler)
logger.setLevel(logging.INFO)
# Log with a dictionary already provided.
logger.info("METRIC", extra={"point": point.to_dict()})
Or, format the metric explicitly in your log call:
logger.info("METRIC: %s", json.dumps(point.to_dict(), default=str))
Both approaches eliminate the need for a custom formatter while keeping the functionality intact.
📚 Documentation preview 📚: https://meltano-sdk--2162.org.readthedocs.build/en/2162/
Summary by Sourcery
Expose metric dictionary to make logging metrics as JSON easier.
Enhancements:
to_dict
method to thePoint
class to convert a metric to a dictionary.SingerMetricsFormatter
class to format metrics as JSON.log
function to include the metric dictionary in the log record.