Skip to content
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

Fix exporter test #263

Merged
merged 3 commits into from
Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 6 additions & 20 deletions promgen/templates/promgen/exporter_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,9 @@ <h1>Project: {{ project.name }}</h1>
<div class="panel-footer">
<div class="input-group-btn">
<button class="btn btn-primary">Register Exporter</button>
<exporter-test
class="btn btn-info"
{% if project.farm %}
href="{% url 'exporter-scrape' project.farm.id %}"
{% else %}
title="Please assign farm to test"
disabled
{% endif %}
form="#exporter_form"
target="#exporterresult">{% trans "Test" %}</exporter-test>
<exporter-test class="btn btn-info" href="{% url 'exporter-scrape' project.id %}" target="#exporterresult">
{% trans "Test" %}
</exporter-test>
</div>
</div>
</div>
Expand All @@ -67,16 +60,9 @@ <h1>Project: {{ project.name }}</h1>
<input type="hidden" name="enabled" value="1" />
<div class="input-group-btn">
<button style="width:80%" class="btn btn-primary">Register {{ default.job }} :{{ default.port }}{{ default.path }}</button>
<exporter-test
style="width:20%"
class="btn btn-info"
{% if project.farm %}
href="{% url 'exporter-scrape' project.farm.id %}"
{% else %}
title="Please assign farm to test"
disabled
{% endif %}
target="#exporterresult">{% trans "Test" %}</exporter-test>
<exporter-test class="btn btn-info" href="{% url 'exporter-scrape' project.id %}" target="#exporterresult">
{% trans "Test" %}
</exporter-test>
</div>
</form>
{% endfor %}
Expand Down
14 changes: 3 additions & 11 deletions promgen/templates/promgen/project_detail_exporters.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,9 @@
<input type="hidden" name="scheme" value="{{ exporter.scheme }}" />
<input type="hidden" name="port" value="{{ exporter.port }}" />
<input type="hidden" name="path" value="{{ exporter.path }}" />
<exporter-test
class="btn btn-info btn-xs"
{% if project.farm %}
href="{% url 'exporter-scrape' project.farm.id %}"
{% else %}
title="Please assign farm to test"
disabled
{% endif %}
target="#exporterresult"
>{% trans "Test" %}</exporter-test>
</div>
<exporter-test class="btn btn-info btn-xs" href="{% url 'exporter-scrape' project.id %}" target="#exporterresult">
{% trans "Test" %}
</exporter-test>
</form>
</td>
<td>
Expand Down
2 changes: 1 addition & 1 deletion promgen/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
path('project/<int:pk>/newfarm', views.FarmRegister.as_view(), name='farm-new'),
path('project/<int:pk>/exporter', views.ExporterRegister.as_view(), name='project-exporter'),
path('project/<int:pk>/notifier', views.ProjectNotifierRegister.as_view(), name='project-notifier'),
path('project/<int:pk>/scrape', views.ExporterScrape.as_view(), name='exporter-scrape'),

path('exporter/<int:pk>/delete', views.ExporterDelete.as_view(), name='exporter-delete'),
path('exporter/<int:pk>/toggle', views.ExporterToggle.as_view(), name='exporter-toggle'),
Expand All @@ -63,7 +64,6 @@

path('farm', views.FarmList.as_view(), name='farm-list'),
path('farm/<int:pk>', views.FarmDetail.as_view(), name='farm-detail'),
path('farm/<int:pk>/scrape', views.ExporterScrape.as_view(), name='exporter-scrape'),
path('farm/<int:pk>/refresh', views.FarmRefresh.as_view(), name='farm-refresh'),
path('farm/<int:pk>/hosts', views.HostRegister.as_view(), name='hosts-add'),
path('farm/<int:pk>/update', views.FarmUpdate.as_view(), name='farm-update'),
Expand Down
26 changes: 12 additions & 14 deletions promgen/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -615,23 +615,18 @@ def form_valid(self, form):
exporter, _ = models.Exporter.objects.get_or_create(project=project, **form.clean())
return HttpResponseRedirect(reverse('project-detail', args=[project.id]))

class ExporterScrape(LoginRequiredMixin, FormView):
class ExporterScrape(LoginRequiredMixin, View):
# TODO: Move to /rest/project/<slug>/scrape
form_class = forms.ExporterForm

def form_invalid(self, form):
return JsonResponse(
{"message": "Form Errors", "errors": form.errors}, status=400
)

def form_valid(self, form):
def post(self, request, pk):
futures = []
farm = get_object_or_404(models.Farm, id=self.kwargs["pk"])
farm = get_object_or_404(models.Farm, project=pk)
# So we have a mutable dictionary
data = request.POST.dict()

# The default __metrics_path__ for Prometheus is /metrics so we need to
# manually add it here in the case it's not set for our test
if not form.cleaned_data["path"]:
form.cleaned_data["path"] = "/metrics"
if not data["path"]:
data["path"] = "/metrics"

def query():
with concurrent.futures.ThreadPoolExecutor(max_workers=20) as executor:
Expand All @@ -640,7 +635,7 @@ def query():
executor.submit(
util.get,
"{scheme}://{host}:{port}{path}".format(
host=host.name, **form.cleaned_data
host=host.name, **data
),
)
)
Expand All @@ -659,7 +654,10 @@ def query():
logger.exception("Unknown Exception")
yield "Unknown URL", "Unknown error"

return JsonResponse(dict(query()))
try:
return JsonResponse(dict(query()))
except Exception as e:
return JsonResponse({"error": "Error with query %s" % e})


class URLRegister(LoginRequiredMixin, FormView, mixins.ProjectMixin):
Expand Down