-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AL-12056: Send metrics directly (#460)
* AL-12056: Send metrics directly * AL-12056: Increase sleep to 60 * AL-12056: Format jmx to anodot metric * AL-12056: Use both cases of metrics * AL-12056: Use own classes for process metrics * AL-12056: add typing * AL-12056: Use prometheus and manual types of metrics * AL-12056: Read kafka raw json data from test_kfk * AL-12056: Refactoring * AL-12056: Wait kafka raw test for a long time * AL-12056: Fix * AL-12056: generate metrics * AL-12056: Fix tests * AL-12056: Fix tests * AL-12056: Fix tests * AL-12056: Fix tests * AL-12056: /metrics return json * AL-12056: refactoring * AL-12056: refactoring
- Loading branch information
1 parent
700f1ad
commit 0301b1f
Showing
9 changed files
with
155 additions
and
25 deletions.
There are no files selected for viewing
This file contains 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 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 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,97 @@ | ||
class Sample: | ||
def __init__(self, name, value, labels, timestamp=None): | ||
self.name = name | ||
self.value = value | ||
self.labels = labels | ||
self.timestamp = timestamp | ||
|
||
|
||
class MetricValue: | ||
def __init__(self): | ||
self._value = 0.0 | ||
|
||
def inc(self, value=1): | ||
self._value += value | ||
|
||
def dec(self, value=1): | ||
self._value -= value | ||
|
||
def set(self, value): | ||
self._value = value | ||
|
||
def get(self): | ||
return self._value | ||
|
||
@property | ||
def value(self): | ||
return self._value | ||
|
||
|
||
class Metric: | ||
def __init__(self, | ||
name, | ||
documentation, | ||
labelnames=(),): | ||
self.name = name | ||
self.documentation = documentation | ||
self._labelnames = labelnames | ||
self._metrics = {} | ||
|
||
def labels(self, *labelvalues, **labelkwargs): | ||
if labelvalues and labelkwargs: | ||
raise ValueError("Can't pass both *args and **kwargs") | ||
if labelkwargs: | ||
if sorted(labelkwargs) != sorted(self._labelnames): | ||
raise ValueError('Incorrect label names') | ||
labelvalues = tuple(labelkwargs[l] for l in self._labelnames) | ||
else: | ||
if len(labelvalues) != len(self._labelnames): | ||
raise ValueError('Incorrect label count') | ||
labelvalues = tuple(l for l in labelvalues) | ||
|
||
if labelvalues not in self._metrics: | ||
self._metrics[labelvalues] = MetricValue() | ||
|
||
return self._metrics[labelvalues] | ||
|
||
@property | ||
def samples(self): | ||
return [Sample( | ||
name=self.name, | ||
value=self._metrics[labelvalues].value, | ||
labels=dict(zip(self._labelnames, labelvalues)), | ||
) for labelvalues in self._metrics] | ||
|
||
|
||
class Gauge(Metric): | ||
def __init__(self, | ||
name, | ||
documentation, | ||
labelnames=(), **kwargs): | ||
super().__init__( | ||
name=name, | ||
documentation=documentation, | ||
labelnames=labelnames, | ||
) | ||
self.type = 'gauge' | ||
|
||
|
||
class Counter(Metric): | ||
def __init__(self, | ||
name, | ||
documentation, | ||
labelnames=(), **kwargs): | ||
super().__init__( | ||
name=name, | ||
documentation=documentation, | ||
labelnames=labelnames, | ||
) | ||
self.type = 'counter' | ||
|
||
@property | ||
def samples(self): | ||
return [Sample( | ||
name=self.name + '_total', | ||
value=self._metrics[labelvalues].value, | ||
labels=dict(zip(self._labelnames, labelvalues)), | ||
) for labelvalues in self._metrics] |
This file contains 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 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 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 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 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 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