Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support to watchtower statistics
- Loading branch information
Showing
6 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { useTranslation } from "next-i18next"; | ||
|
||
import Container from "components/services/widget/container"; | ||
import Block from "components/services/widget/block"; | ||
import useWidgetAPI from "utils/proxy/use-widget-api"; | ||
|
||
|
||
export default function Component({ service }) { | ||
const { t } = useTranslation(); | ||
|
||
const { widget } = service; | ||
|
||
const { data: watchData, error: watchError } = useWidgetAPI(widget, "watchtower"); | ||
|
||
if (watchError || !watchData) { | ||
return <Container error={t("widget.api_error")} />; | ||
} | ||
|
||
if (!watchData) { | ||
return ( | ||
<Container service={service}> | ||
<Block label="watchtower.containers_scanned " /> | ||
<Block label="watchtower.containers_updated" /> | ||
<Block label="watchtower.containers_failed" /> | ||
</Container> | ||
); | ||
} | ||
|
||
return ( | ||
<Container service={service}> | ||
<Block label="watchtower.containers_scanned" value={t("common.number", { value: watchData.watchtower_containers_scanned })} /> | ||
<Block label="watchtower.containers_updated" value={t("common.number", { value: watchData.watchtower_containers_updated })} /> | ||
<Block label="watchtower.containers_failed" value={t("common.number", { value: watchData.watchtower_containers_failed })} /> | ||
</Container> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import cache from "memory-cache"; | ||
|
||
import { httpProxy } from "utils/proxy/http"; | ||
import { formatApiCall } from "utils/proxy/api-helpers"; | ||
import getServiceWidget from "utils/config/service-helpers"; | ||
import createLogger from "utils/logger"; | ||
import widgets from "widgets/widgets"; | ||
|
||
const proxyName = "watchtowerProxyHandler"; | ||
const headerCacheKey = `${proxyName}__headers`; | ||
const logger = createLogger(proxyName); | ||
|
||
export default async function watchtowerProxyHandler(req, res) { | ||
const { group, service, endpoint } = req.query; | ||
|
||
if (!group || !service) { | ||
logger.debug("Invalid or missing service '%s' or group '%s'", service, group); | ||
return res.status(400).json({ error: "Invalid proxy service type" }); | ||
} | ||
|
||
const widget = await getServiceWidget(group, service); | ||
|
||
if (!widget) { | ||
logger.debug("Invalid or missing widget for service '%s' in group '%s'", service, group); | ||
return res.status(400).json({ error: "Invalid proxy service type" }); | ||
} | ||
|
||
let headers = cache.get(headerCacheKey); | ||
if (!headers) { | ||
headers = { | ||
"Authorization": `Bearer ${widget.key}`, | ||
} | ||
cache.put(headerCacheKey, headers); | ||
} | ||
|
||
const url = new URL(formatApiCall(widgets[widget.type].api, { endpoint, ...widget })); | ||
|
||
const method = "GET" | ||
const [status, contentType, data] = await httpProxy(url, { | ||
method, | ||
headers, | ||
}); | ||
|
||
const cleanData = data.toString().split("\n").filter(s => s.startsWith("watchtower")) | ||
const jsonRes={} | ||
|
||
cleanData.map(e => e.split(" ")).forEach(strArray => { | ||
const [key, value] = strArray | ||
jsonRes[key] = value | ||
}) | ||
|
||
if (status !== 200) { | ||
logger.error("Error getting data from WatchTower: %d. Data: %s", status, data); | ||
} | ||
|
||
if (contentType) res.setHeader("Content-Type", contentType); | ||
return res.status(status).send(jsonRes); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import watchtowerProxyHandler from "./proxy"; | ||
|
||
const widget = { | ||
api: "{url}/{endpoint}", | ||
proxyHandler: watchtowerProxyHandler, | ||
|
||
mappings: { | ||
"watchtower": { | ||
endpoint: "v1/metrics", | ||
}, | ||
}, | ||
}; | ||
|
||
export default widget; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters