Skip to content

Commit

Permalink
expose Prometheus metrics through QRest
Browse files Browse the repository at this point in the history
  • Loading branch information
ar committed Feb 13, 2024
1 parent 740695d commit 3ea3dae
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
4 changes: 4 additions & 0 deletions modules/qrest/src/dist/deploy/30_qrest_txnmgr.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<participant class="org.jpos.qrest.participant.Router">
<route path="/q2**" method="GET" name="q2"/>
<route path="/q2**" method="POST" name="q2"/>
<route path="/metrics" method="GET" name="metrics"/>
<route path="/test/load_file" method="POST" name="upload_file"/>
<route path="/welcome.html" method="GET" name="welcome" />
<route path="/dynamic" method="GET" name="dynamic" />
Expand All @@ -17,6 +18,9 @@
<group name="q2">
<participant class="org.jpos.qrest.participant.Q2Info" />
</group>
<group name="metrics">
<participant class="org.jpos.qrest.participant.Metrics" />
</group>
<group name="welcome">
<participant class="org.jpos.qrest.participant.StaticContent">
<property name="documentRoot" value="html" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* jPOS Project [http://jpos.org]
* Copyright (C) 2000-2021 jPOS Software SRL
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package org.jpos.qrest.participant;

import java.io.Serializable;
import java.util.*;

import io.netty.buffer.Unpooled;
import io.netty.handler.codec.http.*;
import org.jpos.q2.Q2;
import org.jpos.qrest.Route;
import org.jpos.transaction.Context;
import org.jpos.transaction.TransactionManager;
import org.jpos.transaction.TransactionParticipant;

import static io.netty.handler.codec.http.HttpResponseStatus.OK;
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
import static org.jpos.qrest.Constants.RESPONSE;

public class Metrics implements TransactionParticipant {
// special temp key in response map to indicate a specific HttpResponseStatus value (should be removed from actual response)
private final static String HTTP_STATUS_KEY = "__http_status__";
private TransactionManager txnmgr;
private Q2 q2;
private List<Route<Map<String,Object>>> routes = new ArrayList<>();
private String prefix;

public Metrics () { }

@Override
public int prepare(long id, Serializable context) {
Context ctx = (Context) context;
HttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK,
Unpooled.wrappedBuffer(q2.getPrometheusMeterRegistry().scrape().getBytes())
);
ctx.put(RESPONSE, response);
return PREPARED | NO_JOIN | READONLY;
}

@SuppressWarnings("unused")
public void setTransactionManager (TransactionManager txnmgr) {
this.txnmgr = txnmgr;
this.q2 = txnmgr.getServer();
}
}

0 comments on commit 3ea3dae

Please sign in to comment.