VictoriaMetrics/lib/promscrape/targets_response.qtpl
Nikolay ce8c2dd1f1
Changes targets api (#961)
* changes /targets api
adds html response if requester accepts text/html,
adds quick template for /targets api,
fixes pathPrefix for / requests

* changes namings

* renamed targets file

* Update app/victoria-metrics/main.go

Co-authored-by: Aliaksandr Valialkin <valyala@gmail.com>

* adds trimspace to qtpl,
moves content-type for targets response closer to writer

* fixes bug with prefix

Co-authored-by: Aliaksandr Valialkin <valyala@gmail.com>
2020-12-14 13:36:48 +02:00

104 lines
No EOL
4.2 KiB
Text

{% import "github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal"
%}
{% stripspace %}
{% func TargetsResponsePlain (jts []jobTargetsStatuses, showOriginLabels bool) -%}
{% for _, js := range jts %}
job={%q= js.job %}{% space %} ({%d js.upCount %}/{%d js.targetsTotal %} {% space %} up)
{% newline %}
{% for _, ts := range js.targetsStatus %}
{% code
labels := promLabelsString(ts.labels)
ol := promLabelsString(ts.originalLabels)
%}
{%s= "\t" %}state={% if ts.up %}up{% else %}down{% endif %},
{% space %} endpoint={%s= ts.endpoint %},
{% space %} labels={%s= labels %}
{% if showOriginLabels %},{% space %} originalLabels={%s= ol %}{% endif %},
{% space %} last_scrape={%f.3 ts.lastScrapeTime.Seconds() %}s ago,
{% space %} scrape_duration={%f.3 float64(ts.scrapeDuration.Seconds()) %}s,
{% space %} error={%q= ts.error %}
{% newline %}
{% endfor %}
{% endfor %}
{% newline %}
{% endfunc %}
{% func TargetsResponseHTML(jts []jobTargetsStatuses, redirectPath string, onlyUnhealthy bool) %}
<!DOCTYPE html>
<style>
.border{
border-collapse: collapse;
border: 1px solid black;
}
.table-row:hover{
background-color: #f5f5f5;
}
</style>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>VictoriaMetrics Database</title>
</head>
<body>
<h1>Targets</h1>
<div id="showTargets" class="btn-group btn-group-toggle" data-toggle="buttons">
<label class="btn">
<input type="radio" name="targets" id="all-targets" autocomplete="off" onclick="location.href='{%s= redirectPath %}';" {% if !onlyUnhealthy %}checked {% endif %}> All
</label>
<label class="btn">
<input type="radio" name="targets" id="unhealthy-targets" autocomplete="off" onclick="location.href='{%s= redirectPath %}?show_only_unhealthy=true';" {% if onlyUnhealthy %}checked {% endif %}> Unhealthy
</label>
<br />
</div>
{% for _,js :=range jts %}
<div class="table-container">
<h2 class="job_header danger">
<a id="job-{%q= js.job %}" >{%q= js.job %} ({%d js.upCount %}/{%d js.targetsTotal %} up)</a>
</h2>
<table class="table-bordered table-hover border">
<thead class="job_details border">
<tr class="table-row border">
<th class="border">Endpoint</th>
<th class="border">State</th>
<th class="border">Labels</th>
<th class="border">Last Scrape</th>
<th class="border">Scrape Duration</th>
<th class="border">Error</th>
</tr>
</thead>
<tbody>
{% for _, ts := range js.targetsStatus %}
{% if onlyUnhealthy && ts.up %} {% continue %} {% endif %}
<tr class="table-row border">
<td class="endpoint border">
<a href="{%s= ts.endpoint %}">{%s= ts.endpoint %}</a><br>
</td>
<td class="state border">
<span class="state_indicator">{% if ts.up %}UP{% else %}DOWN{% endif %}</span>
</td>
<td class="labels border", title="Original {% space %} labels: {% space %} {%= formatLabel(ts.originalLabels) %}">
{%= formatLabel(ts.labels) %}
</td>
<td class="last-scrape border">{%s ts.lastScrapeTime.String() %} ago</td>
<td class="scrape-duration border">{%s ts.scrapeDuration.String() %}</td>
<td class="errors border"><span class="alert alert-danger state_indicator">{%s= ts.error %}</span></td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endfor %}
</body>
</html>
{% endfunc %}
{% func formatLabel(labels []prompbmarshal.Label) %}
{% for _, label := range labels %}
{% space %} {%s label.Name %}={%q label.Value %} {% space %}
{% endfor %}
{% endfunc %}
{% endstripspace %}