2022-02-03 19:22:35 +01:00
{% import (
2022-04-19 17:26:21 +02:00
"net/url"
2022-06-06 23:57:05 +02:00
2023-01-04 08:26:27 +01:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/htmlcomponents"
2022-11-30 06:22:12 +01:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promutils"
2022-02-03 19:22:35 +01:00
) %}
2020-12-14 12:36:48 +01:00
2021-06-18 09:53:10 +02:00
{% stripspace %}
2020-12-14 12:36:48 +01:00
2022-06-06 23:57:05 +02:00
{% func TargetsResponsePlain(tsr *targetsStatusResult, filter *requestFilter) %}
2022-04-19 17:26:21 +02:00
2022-06-06 23:57:05 +02:00
{% if tsr.err != nil %}
{%s= tsr.err.Error() %}
2022-04-19 17:26:21 +02:00
{% return %}
{% endif %}
2020-12-14 12:36:48 +01:00
2022-06-06 23:57:05 +02:00
{% for _, jts := range tsr.jobTargetsStatuses %}
job={%s= jts.jobName %}{% space %}({%d jts.upCount %}/{%d jts.targetsTotal %}{% space %}up)
2022-04-19 17:26:21 +02:00
{% newline %}
2022-06-06 23:57:05 +02:00
{% for _, ts := range jts.targetsStatus %}
2022-04-19 17:26:21 +02:00
{%s= "\t" %}
state={% if ts.up %}up{% else %}down{% endif %},{% space %}
endpoint={%s= ts.sw.Config.ScrapeURL %},{% space %}
2022-11-30 06:22:12 +01:00
labels={%s= ts.sw.Config.Labels.String() %},{% space %}
{% if filter.showOriginalLabels %}originalLabels={%s= ts.sw.Config.OriginalLabels.String() %},{% space %}{% endif %}
2022-04-19 17:26:21 +02:00
scrapes_total={%d ts.scrapesTotal %},{% space %}
scrapes_failed={%d ts.scrapesFailed %},{% space %}
2023-12-06 21:24:24 +01:00
last_scrape={%s ts.getDurationFromLastScrape() %},{% space %}
2022-04-19 17:26:21 +02:00
scrape_duration={%d int(ts.scrapeDuration) %}ms,{% space %}
samples_scraped={%d ts.samplesScraped %},{% space %}
error={% if ts.err != nil %}{%s= ts.err.Error() %}{% endif %}
{% newline %}
{% endfor %}
2020-12-14 12:36:48 +01:00
{% endfor %}
2021-06-18 09:53:10 +02:00
2022-06-06 23:57:05 +02:00
{% for _, jobName := range tsr.emptyJobs %}
job={%s= jobName %}{% space %}(0/0 up)
2020-12-14 12:36:48 +01:00
{% newline %}
2021-06-18 09:53:10 +02:00
{% endfor %}
2020-12-14 12:36:48 +01:00
{% endfunc %}
2022-06-06 23:57:05 +02:00
{% func TargetsResponseHTML(tsr *targetsStatusResult, filter *requestFilter) %}
2020-12-14 12:36:48 +01:00
<!DOCTYPE html>
2020-12-15 10:58:53 +01:00
<html lang="en">
<head>
2023-01-04 08:26:27 +01:00
{%= htmlcomponents.CommonHeader() %}
2022-06-06 23:57:05 +02:00
<title>Active Targets</title>
2020-12-15 10:58:53 +01:00
</head>
2022-04-20 10:25:04 +02:00
<body>
2023-01-04 08:26:27 +01:00
{%= htmlcomponents.Navbar() %}
2022-06-06 23:57:05 +02:00
<div class="container-fluid">
{% if tsr.err != nil %}
2023-01-04 08:26:27 +01:00
{%= htmlcomponents.ErrorNotification(tsr.err) %}
2022-06-06 23:57:05 +02:00
{% endif %}
<div class="row">
<main class="col-12">
<h1>Active Targets</h1>
<hr />
{%= filtersForm(filter) %}
<hr />
{%= targetsTabs(tsr, filter, "scrapeTargets") %}
</main>
2022-04-20 10:25:04 +02:00
</div>
</div>
2022-06-06 23:57:05 +02:00
</body>
</html>
{% endfunc %}
{% func ServiceDiscoveryResponse(tsr *targetsStatusResult, filter *requestFilter) %}
<!DOCTYPE html>
<html lang="en">
<head>
2023-01-04 08:26:27 +01:00
{%= htmlcomponents.CommonHeader() %}
2022-06-06 23:57:05 +02:00
<title>Discovered Targets</title>
</head>
<body>
2023-01-04 08:26:27 +01:00
{%= htmlcomponents.Navbar() %}
2022-04-19 17:26:21 +02:00
<div class="container-fluid">
2022-06-06 23:57:05 +02:00
{% if tsr.err != nil %}
2023-01-04 08:26:27 +01:00
{%= htmlcomponents.ErrorNotification(tsr.err) %}
2022-04-19 17:26:21 +02:00
{% endif %}
<div class="row">
<main class="col-12">
2022-06-06 23:57:05 +02:00
<h1>Discovered Targets</h1>
2022-04-19 17:26:21 +02:00
<hr />
2022-06-06 23:57:05 +02:00
{%= filtersForm(filter) %}
2022-04-19 17:26:21 +02:00
<hr />
2022-06-06 23:57:05 +02:00
{%= targetsTabs(tsr, filter, "discoveredTargets") %}
2022-04-19 17:26:21 +02:00
</main>
</div>
2021-06-18 09:53:10 +02:00
</div>
2020-12-15 10:58:53 +01:00
</body>
2020-12-14 12:36:48 +01:00
</html>
{% endfunc %}
2022-06-06 23:57:05 +02:00
{% func filtersForm(filter *requestFilter) %}
<div class="row g-3 align-items-center mb-3">
<div class="col-auto">
<button id="all-btn" type="button" class="btn{% space %}{% if !filter.showOnlyUnhealthy %}btn-secondary{% else %}btn-success{% endif %}"
onclick="location.href='?{%= queryArgs(filter, map[string]string{"show_only_unhealthy": "false"}) %}'">
All
</button>
</div>
<div class="col-auto">
<button id="unhealthy-btn" type="button" class="btn{% space %}{% if filter.showOnlyUnhealthy %}btn-secondary{% else %}btn-danger{% endif %}"
onclick="location.href='?{%= queryArgs(filter, map[string]string{"show_only_unhealthy": "true"}) %}'">
Unhealthy
</button>
</div>
<div class="col-auto">
<button type="button" class="btn btn-primary" onclick="document.querySelectorAll('.scrape-job').forEach((el) => { el.style.display = 'none'; })">
Collapse all
</button>
</div>
<div class="col-auto">
<button type="button" class="btn btn-secondary" onclick="document.querySelectorAll('.scrape-job').forEach((el) => { el.style.display = 'block'; })">
Expand all
</button>
</div>
<div class="col-auto">
<button type="button" class="btn btn-success" onclick="document.getElementById('filters').style.display='block'">
Filter targets
</button>
</div>
</div>
<div id="filters" {% if filter.endpointSearch == "" && filter.labelSearch == "" %}style="display:none"{% endif %}>
<form class="form-horizontal">
<div class="form-group mb-3">
<label for="endpoint_search" class="col-sm-10 control-label">Endpoint filter (<a target="_blank" href="https://github.com/google/re2/wiki/Syntax">Regexp</a> is accepted)</label>
<div class="col-sm-10">
<input type="text" id="endpoint_search" name="endpoint_search"
placeholder="For example, 127.0.0.1" class="form-control" value="{%s filter.endpointSearch %}"/>
</div>
</div>
<div class="form-group mb-3">
<label for="label_search" class="col-sm-10 control-label">Labels filter (<a target="_blank" href="https://prometheus.io/docs/prometheus/latest/querying/basics/#time-series-selectors">Arbitrary time series selectors</a> are accepted)</label>
<div class="col-sm-10">
<input type="text" id="label_search" name="label_search"
placeholder="For example, {instance=~'.+:9100'}" class="form-control" value="{%s filter.labelSearch %}"/>
</div>
</div>
<input type="hidden" name="show_only_unhealthy" value="{%v filter.showOnlyUnhealthy %}"/>
<input type="hidden" name="show_original_labels" value="{%v filter.showOriginalLabels %}"/>
<button type="submit" class="btn btn-success mb-3">Submit</button>
<button type="button" class="btn btn-danger mb-3" onclick="location.href='?'">Clear target filters</button>
</form>
</div>
{% endfunc %}
{% func targetsTabs(tsr *targetsStatusResult, filter *requestFilter, activeTab string) %}
<ul class="nav nav-tabs" id="myTab" role="tablist">
<li class="nav-item" role="presentation">
<button class="nav-link{%if activeTab=="scrapeTargets"%}{% space %}active{%endif%}" type="button" role="tab"
onclick="location.href='targets?{%= queryArgs(filter, nil) %}'">
Active targets
</button>
</li>
<li class="nav-item" role="presentation">
<button class="nav-link{%if activeTab=="discoveredTargets"%}{% space %}active{%endif%}" type="button" role="tab"
onclick="location.href='service-discovery?{%= queryArgs(filter, nil) %}'">
Discovered targets
</button>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane active" role="tabpanel">
{% switch activeTab %}
{% case "scrapeTargets" %}
{%= scrapeTargets(tsr) %}
{% case "discoveredTargets" %}
{%= discoveredTargets(tsr) %}
{% endswitch %}
</div>
</div>
{% endfunc %}
{% func scrapeTargets(tsr *targetsStatusResult) %}
<div class="row mt-4">
<div class="col-12">
{% for i, jts := range tsr.jobTargetsStatuses %}
2023-07-21 04:14:30 +02:00
{%= scrapeJobTargets(i, jts, tsr.hasOriginalLabels) %}
2022-06-06 23:57:05 +02:00
{% endfor %}
{% for i, jobName := range tsr.emptyJobs %}
{% code
num := i + len(tsr.jobTargetsStatuses)
jts := &jobTargetsStatuses{
jobName: jobName,
}
%}
2023-07-21 04:14:30 +02:00
{%= scrapeJobTargets(num, jts, tsr.hasOriginalLabels) %}
2022-06-06 23:57:05 +02:00
{% endfor %}
</div>
</div>
{% endfunc %}
2023-07-21 04:14:30 +02:00
{% func scrapeJobTargets(num int, jts *jobTargetsStatuses, hasOriginalLabels bool) %}
2022-06-06 23:57:05 +02:00
<div class="row mb-4">
<div class="col-12">
<h4>
<span class="me-2">{%s jts.jobName %}{% space %}({%d jts.upCount %}/{%d jts.targetsTotal %}{% space %}up)</span>
{%= showHideScrapeJobButtons(num) %}
</h4>
<div id="scrape-job-{%d num %}" class="scrape-job table-responsive">
<table class="table table-striped table-hover table-bordered table-sm">
<thead>
<tr>
<th scope="col">Endpoint</th>
<th scope="col">State</th>
<th scope="col" title="target labels">Labels</th>
2023-07-21 04:14:30 +02:00
{% if hasOriginalLabels %}
<th scope="col" title="debug relabeling">Debug relabeling</th>
{% endif %}
2022-06-06 23:57:05 +02:00
<th scope="col" title="total scrapes">Scrapes</th>
<th scope="col" title="total scrape errors">Errors</th>
<th scope="col" title="the time of the last scrape">Last Scrape</th>
<th scope="col" title="the duration of the last scrape">Duration</th>
<th scope="col" title="the number of metrics scraped during the last scrape">Samples</th>
<th scope="col" title="error from the last scrape (if any)">Last error</th>
</tr>
</thead>
<tbody>
{% for _, ts := range jts.targetsStatus %}
{% code
endpoint := ts.sw.Config.ScrapeURL
2023-07-21 04:14:30 +02:00
originalLabels := ts.sw.Config.OriginalLabels
2022-12-10 11:09:21 +01:00
// The target is uniquely identified by a pointer to its original labels.
2023-07-21 04:14:30 +02:00
targetID := getLabelsID(originalLabels)
2022-06-06 23:57:05 +02:00
%}
<tr {% if !ts.up %}{%space%}class="alert alert-danger" role="alert" {% endif %}>
<td class="endpoint">
2023-07-21 04:14:30 +02:00
<a href="{%s endpoint %}" target="_blank">{%s endpoint %}</a>
{% if hasOriginalLabels %}
{% space %}
(<a href="target_response?id={%s targetID %}" target="_blank"
title="click to fetch target response on behalf of the scraper">response</a>)
{% endif %}
2022-06-06 23:57:05 +02:00
</td>
<td>
{% if ts.up %}
<span class="badge bg-success">UP</span>
{% else %}
<span class="badge bg-danger">DOWN</span>
{% endif %}
</td>
<td class="labels">
2023-07-21 04:14:30 +02:00
<div
{% if hasOriginalLabels %}
{% space %}title="click to show original labels"
onclick="document.getElementById('original-labels-{%s targetID %}').style.display='block'"
{% endif %}
>
{%= formatLabels(ts.sw.Config.Labels) %}
</div>
{% if hasOriginalLabels %}
<div style="display:none" id="original-labels-{%s targetID %}">
{%= formatLabels(originalLabels) %}
</div>
{% endif %}
2022-12-10 11:09:21 +01:00
</td>
2023-07-21 04:14:30 +02:00
{% if hasOriginalLabels %}
<td>
<a href="target-relabel-debug?id={%s targetID %}" target="_blank">target</a>{% space %}
<a href="metric-relabel-debug?id={%s targetID %}" target="_blank">metrics</a>
</td>
{% endif %}
2022-06-06 23:57:05 +02:00
<td>{%d ts.scrapesTotal %}</td>
<td>{%d ts.scrapesFailed %}</td>
2023-12-06 21:24:24 +01:00
<td>{%s ts.getDurationFromLastScrape() %}
2022-06-06 23:57:05 +02:00
<td>{%d int(ts.scrapeDuration) %}ms</td>
<td>{%d ts.samplesScraped %}</td>
<td>{% if ts.err != nil %}{%s ts.err.Error() %}{% endif %}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
{% endfunc %}
{% func discoveredTargets(tsr *targetsStatusResult) %}
2023-07-21 04:14:30 +02:00
{% if !tsr.hasOriginalLabels %}
Discovered targets are unavailable when -promscrape.dropOriginalLabels command-line flag is set
{% return %}
{% endif %}
2022-06-06 23:57:05 +02:00
{% code tljs := tsr.getTargetLabelsByJob() %}
<div class="row mt-4">
<div class="col-12">
{% for i, tlj := range tljs %}
{%= discoveredJobTargets(i, tlj) %}
{% endfor %}
</div>
</div>
{% endfunc %}
{% func discoveredJobTargets(num int, tlj *targetLabelsByJob) %}
<h4>
<span class="me-2">{%s tlj.jobName %}{% space %}({%d tlj.activeTargets %}/{%d tlj.activeTargets+tlj.droppedTargets %}{% space %}active)</span>
{%= showHideScrapeJobButtons(num) %}
</h4>
<div id="scrape-job-{%d num %}" class="scrape-job table-responsive">
<table class="table table-striped table-hover table-bordered table-sm">
<thead>
<tr>
<th scope="col" style="width: 5%">Status</th>
2022-12-10 11:09:21 +01:00
<th scope="col" style="width: 60%">Discovered Labels</th>
2022-06-06 23:57:05 +02:00
<th scope="col" style="width: 30%">Target Labels</th>
2022-12-10 11:09:21 +01:00
<th scope="col" stile="width: 5%">Debug relabeling</a>
2022-06-06 23:57:05 +02:00
</tr>
</thead>
<tbody>
{% for _, t := range tlj.targets %}
<tr
{% if !t.up %}
{% space %}role="alert"{% space %}
2022-11-30 06:22:12 +01:00
{% if t.labels.Len() > 0 %}
2022-06-06 23:57:05 +02:00
class="alert alert-danger"
{% else %}
class="alert alert-warning"
{% endif %}
{% endif %}
>
<td>
{% if t.up %}
<span class="badge bg-success">UP</span>
2022-11-30 06:22:12 +01:00
{% elseif t.labels.Len() > 0 %}
2022-06-06 23:57:05 +02:00
<span class="badge bg-danger">DOWN</span>
{% else %}
2023-12-01 15:37:57 +01:00
<span class="badge bg-warning">DROPPED ({%s string(t.dropReason) %})</span>
2022-06-06 23:57:05 +02:00
{% endif %}
</td>
<td class="labels">
2022-12-10 11:09:21 +01:00
{%= formatLabels(t.originalLabels) %}
2022-06-06 23:57:05 +02:00
</td>
<td class="labels">
2022-11-30 06:22:12 +01:00
{%= formatLabels(t.labels) %}
2022-06-06 23:57:05 +02:00
</td>
2022-12-10 11:09:21 +01:00
<td>
2023-07-21 04:14:30 +02:00
{% code targetID := getLabelsID(t.originalLabels) %}
<a href="target-relabel-debug?id={%s targetID %}" target="_blank">debug</a>
2022-12-10 11:09:21 +01:00
</td>
2022-06-06 23:57:05 +02:00
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endfunc %}
{% func showHideScrapeJobButtons(num int) %}
<button type="button" class="btn btn-primary btn-sm me-1"
onclick="document.getElementById('scrape-job-{%d num %}').style.display='none'">
collapse
</button>
<button type="button" class="btn btn-secondary btn-sm me-1"
onclick="document.getElementById('scrape-job-{%d num %}').style.display='block'">
expand
</button>
{% endfunc %}
{% func queryArgs(filter *requestFilter, override map[string]string) %}
2022-04-19 17:26:21 +02:00
{% code
2022-06-06 23:57:05 +02:00
showOnlyUnhealthy := "false"
if filter.showOnlyUnhealthy {
showOnlyUnhealthy = "true"
}
m := map[string]string{
"show_only_unhealthy": showOnlyUnhealthy,
"endpoint_search": filter.endpointSearch,
"label_search": filter.labelSearch,
}
for k, v := range override {
m[k] = v
}
2022-04-19 17:26:21 +02:00
qa := make(url.Values, len(m))
for k, v := range m {
qa[k] = []string{v}
}
%}
{%s qa.Encode() %}
{% endfunc %}
2022-11-30 06:22:12 +01:00
{% func formatLabels(labels *promutils.Labels) %}
{% code labelsList := labels.GetLabels() %}
2021-10-11 11:40:46 +02:00
{
2022-11-30 06:22:12 +01:00
{% for i, label := range labelsList %}
2021-10-11 11:40:46 +02:00
{%s label.Name %}={%q label.Value %}
2022-11-30 06:22:12 +01:00
{% if i+1 < len(labelsList) %},{% space %}{% endif %}
2021-10-11 11:40:46 +02:00
{% endfor %}
}
2020-12-14 12:36:48 +01:00
{% endfunc %}
2020-12-15 10:58:53 +01:00
2021-06-18 09:53:10 +02:00
{% endstripspace %}