On 11/15/19 3:56 PM, David Lorente Moldes wrote:
> I want to know how I could get to import data from a text file and then
> export it to Prometheus. I've done a piece of code that it works but at
> the moment I try to display the values in Prometheus, it only shows one
> value and not all of them as I would like. Next, I just posted my code
> and attached some pictures about what Prometheus do and another one to
> show you what I want to get (Overall and Download Throughput graphics).
As far as I understand, you are writing an exporter for a custom
application metric format in order to make Prometheus able to ingest
them, correct?
I see that you are looping over several files in the current directory.
What kind of files are they?
Is there some qualitative difference in the
area/content/partition/whatever they provide? If so, you will have to
add this information as a label to your metric. The simplest form would
be adding a filename="..." label which contains the source file name.
Your current output looks like this:
TFM_throughpt{IP="localhost",module="Overall"} 13.0
TFM_throughpt{IP="localhost",module="Download"} 30.0
TFM_throughpt{IP="localhost",module="Overall"} 43.0
With the change, it could look like this:
TFM_throughpt{IP="localhost",module="Overall",filename="test_foo"} 13.0
TFM_throughpt{IP="localhost",module="Download",filename="test_foo"} 30.0
TFM_throughpt{IP="localhost",module="Overall",filename="test_foo"} 43.0
TFM_throughpt{IP="localhost",module="Overall",filename="test_bar"} 3.0
TFM_throughpt{IP="localhost",module="Download",filename="test_bar"} 0.0
TFM_throughpt{IP="localhost",module="Overall",filename="test_bar"} 3.0
If the difference is rather a temporal one such as "measurements for
hour 4, measurements for hour 5" etc., you will have to think about
other ways for ingestion. Prometheus will always scrape the most recent
data and this is what exporters are supposed to provide.
The temporal/historical dimension simply comes from Prometheus
repeatedly scraping the same address.
If this is the case for you, one solution would be simply dropping the
loop and always opening the "test_current" file (or whatever the file is
called which provides the most-recent measurements).
Some additional notes:
- You may want to have a look at the docs regarding metric naming. For
example, I suggest adding the unit to your metric such as
tfm_throughput_bytes.
- You should also verify that the IP= label is a good idea. If this is a
one-to-one mapping per instance, you might as well use the instance
label [2]. If it contains lots of target IPs, you may run into
cardinality problems [1].
- If "Overall" is just the sum of "Download" and "Upload", you could
also skip this as this could easily be calculated in PromQL.
- If you can control what the files look like, there may be an easier
way to go: node_exporter already has a textfile collector which simply
exposes the metrics from static files in a specific directory [3].
However, you cannot do any format transformations there. In other words:
The files must contain valid data in the Prometheus exposition format.
Also, node_exporter is mainly targeted at instance-level data.
[1]
https://prometheus.io/docs/practices/naming/
[2]
https://www.robustperception.io/controlling-the-instance-label
[3]
https://github.com/prometheus/node_exporter#textfile-collector
Kind regards,
Christian