-
Notifications
You must be signed in to change notification settings - Fork 818
Add PlatformCollector to collect information about the platform #152
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ca3dbba
Add PlatformCollector to collect information about the platform
mortenlj 3d4f456
Use positional argument specifier to work in py26
mortenlj a3f9515
Remove machine and system info better handled by node exporter
mortenlj ac69024
Update README with latest changes
mortenlj 23deb1d
Fixing a couple typos
mortenlj d909a7a
Fix unused variables and long line
mortenlj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 | ||
from __future__ import unicode_literals | ||
|
||
import platform as pf | ||
|
||
from . import core | ||
|
||
|
||
class PlatformCollector(object): | ||
"""Collector for platform information""" | ||
|
||
def __init__(self, registry=core.REGISTRY, platform=None): | ||
self._platform = pf if platform is None else platform | ||
info = self._info() | ||
system = self._platform.system() | ||
if system == "Java": | ||
info.update(self._java()) | ||
self._metrics = [ | ||
self._add_metric("python_info", "Python information", info) | ||
] | ||
if registry: | ||
registry.register(self) | ||
|
||
def collect(self): | ||
return self._metrics | ||
|
||
@staticmethod | ||
def _add_metric(name, documentation, data): | ||
labels = data.keys() | ||
values = [data[k] for k in labels] | ||
g = core.GaugeMetricFamily(name, documentation, labels=labels) | ||
g.add_metric(values, 1) | ||
return g | ||
|
||
def _info(self): | ||
major, minor, patchlevel = self._platform.python_version_tuple() | ||
return { | ||
"version": self._platform.python_version(), | ||
"implementation": self._platform.python_implementation(), | ||
"major": major, | ||
"minor": minor, | ||
"patchlevel": patchlevel | ||
} | ||
|
||
def _java(self): | ||
java_version, _, vminfo, osinfo = self._platform.java_ver() | ||
vm_name, vm_release, vm_vendor = vminfo | ||
system, kernel, _ = osinfo | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is unused |
||
return { | ||
"jvm_version": java_version, | ||
"jvm_release": vm_release, | ||
"jvm_vendor": vm_vendor, | ||
"jvm_name": vm_name | ||
} | ||
|
||
|
||
PLATFORM_COLLECTOR = PlatformCollector() | ||
"""PlatformCollector in default Registry REGISTRY""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from __future__ import unicode_literals | ||
|
||
import unittest | ||
|
||
from prometheus_client import CollectorRegistry, PlatformCollector | ||
|
||
|
||
class TestPlatformCollector(unittest.TestCase): | ||
def setUp(self): | ||
self.registry = CollectorRegistry() | ||
self.platform = _MockPlatform() | ||
|
||
def test_python_info(self): | ||
PlatformCollector(registry=self.registry, platform=self.platform) | ||
self.assertLabels("python_info", { | ||
"version": "python_version", | ||
"implementation": "python_implementation", | ||
"major": "pvt_major", | ||
"minor": "pvt_minor", | ||
"patchlevel": "pvt_patchlevel" | ||
}) | ||
|
||
def test_system_info_java(self): | ||
self.platform._system = "Java" | ||
PlatformCollector(registry=self.registry, platform=self.platform) | ||
self.assertLabels("python_info", { | ||
"version": "python_version", | ||
"implementation": "python_implementation", | ||
"major": "pvt_major", | ||
"minor": "pvt_minor", | ||
"patchlevel": "pvt_patchlevel", | ||
"jvm_version": "jv_release", | ||
"jvm_release": "vm_release", | ||
"jvm_vendor": "vm_vendor", | ||
"jvm_name": "vm_name" | ||
}) | ||
|
||
def assertLabels(self, name, labels): | ||
for metric in self.registry.collect(): | ||
for n, l, value in metric.samples: | ||
if n == name: | ||
assert l == labels | ||
return | ||
assert False | ||
|
||
|
||
class _MockPlatform(object): | ||
def __init__(self): | ||
self._system = "system" | ||
|
||
def python_version_tuple(self): | ||
return "pvt_major", "pvt_minor", "pvt_patchlevel" | ||
|
||
def python_version(self): | ||
return "python_version" | ||
|
||
def python_implementation(self): | ||
return "python_implementation" | ||
|
||
def system(self): | ||
return self._system | ||
|
||
def java_ver(self): | ||
return "jv_release", "jv_vendor", ("vm_name", "vm_release", "vm_vendor"), ("os_name", "os_version", "os_arch") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
"Python platform" would be a little clearer.