Skip to content

Commit

Permalink
Merge pull request #82 from JarvusInnovations/develop
Browse files Browse the repository at this point in the history
Release: gatekeeper v2.3.0
  • Loading branch information
themightychris authored May 16, 2020
2 parents ea71cbc + 563f338 commit 3db6eb3
Show file tree
Hide file tree
Showing 15 changed files with 195 additions and 28 deletions.
3 changes: 2 additions & 1 deletion .holo/branches/emergence-site/_gatekeeper.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[holomapping]
files = [
"*/**",
"!.vscode/"
"!.vscode/",
"!habitat/"
]
after = "*"
3 changes: 2 additions & 1 deletion .studiorc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/bin/bash
hab pkg install emergence/studio
hab pkg install emergence/studio jarvus/mkdocs-studio
source "$(hab pkg path emergence/studio)/studio.sh"
source "$(hab pkg path jarvus/mkdocs-studio )/studio.sh"
13 changes: 13 additions & 0 deletions console-commands/gk/health/cache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

$memInfo = apcu_sma_info(true);

$totalBytes = $memInfo['seg_size'] * $memInfo['num_seg'];
$freeBytes = $memInfo['avail_mem'];
$responsesBytes = Cache::getIterator('/^response:/')->getTotalSize();
$applicationBytes = $totalBytes - $freeBytes - $responseBytes;

printf("total\t%u\n", $totalBytes);
printf("free\t%u\n", $freeBytes);
printf("responses\t%u\n", $responsesBytes);
printf("application\t%u\n", $applicationBytes);
7 changes: 7 additions & 0 deletions console-commands/gk/health/endpoint-requests.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

use Gatekeeper\Endpoints\Endpoint;

foreach (Endpoint::getAll() AS $Endpoint) {
printf("%s\t%u\n", $Endpoint->Handle, $Endpoint->getCounterMetric('requests'));
}
3 changes: 3 additions & 0 deletions habitat/netdata/default.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[server]
address = "0.0.0.0"
port = 19999
57 changes: 57 additions & 0 deletions habitat/netdata/plan.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
pkg_name=gatekeeper-netdata
pkg_origin=jarvus
pkg_version=1.17.0
pkg_maintainer="Chris Alfano <[email protected]>"
pkg_license=("MIT")
pkg_build_deps=(
jarvus/toml-merge
)
pkg_wrapped_ident="emergence/netdata"
pkg_deps=(
core/bash
"${pkg_wrapped_ident}"
)

pkg_exports=(
[host]=server.address
[port]=server.port
)
pkg_exposes=(port)
pkg_svc_run="netdata -D -c ${pkg_svc_config_path}/netdata.conf"

pkg_binds_optional=(
[mysql]="port username password"
[nginx]="port status_path"
[phpfpm]="port status_path"
)

do_before() {
push_runtime_env NETDATA_PKG_PLUGINS_DIR "${pkg_prefix}/plugins.d"
}

do_build() {
return 0
}

do_install() {
return 0
}

do_build_config() {
do_default_build_config

build_line "Merging config from ${pkg_wrapped_ident}"
cp -nrv "$(pkg_path_for ${pkg_wrapped_ident})"/{config,hooks} "${pkg_prefix}/"
toml-merge \
"$(pkg_path_for ${pkg_wrapped_ident})/default.toml" \
"${PLAN_CONTEXT}/default.toml" \
> "${pkg_prefix}/default.toml"

build_line "Copying local plugins.d"
cp -rv "${PLAN_CONTEXT}/plugins.d" "${pkg_prefix}/"
fix_interpreter "${pkg_prefix}/plugins.d/*" core/bash bin/bash
}

do_strip() {
return 0
}
65 changes: 65 additions & 0 deletions habitat/netdata/plugins.d/gatekeeper.plugin
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/bin/bash

# TODO: move gk-specific charts somewhere additional
# TODO: add top-users chart

# load netdata plugin config
if [ -f "${NETDATA_USER_CONFIG_DIR}/emergence.conf" ]; then
set -a
. "${NETDATA_USER_CONFIG_DIR}/emergence.conf"
set +a
fi


# define CACHE_STATUS chart
echo "CHART gatekeeper.cache_status '' 'Gatekeeper Cache' B 'Gatekeeper' cache stacked 1"
echo "DIMENSION application '' absolute"
echo "DIMENSION responses '' absolute"
echo "DIMENSION free '' absolute"
declare -A CACHE_STATUS


# define ENDPOINT_REQUESTS chart
echo "CHART gatekeeper.endpoint_requests '' 'Endpoint Requests' 'requests/hr' 'Gatekeeper' endpoint_requests stacked 2"
declare -A ENDPOINT_REQUESTS_DIMENSIONS


# update all charts indefinitely
while true; do


# update CACHE_STATUS
while read -r line; do
key="${line%%$'\t'*}"
value="${line#*$'\t'}"
CACHE_STATUS["${key}"]="${value}"
done < <(env -i HAB_LICENSE="${HAB_LICENSE}" hab pkg exec "${EMERGENCE_RUNTIME:-emergence/php-runtime}" emergence-console-run gk:health:cache)

echo "BEGIN gatekeeper.cache_status"
echo "SET application=${CACHE_STATUS[application]}"
echo "SET responses=${CACHE_STATUS[responses]}"
echo "SET free=${CACHE_STATUS[free]}"
echo "END"


# update ENDPOINT_REQUESTS
echo "BEGIN gatekeeper.endpoint_requests"

while read -r line; do
key="${line%%$'\t'*}"
value="${line#*$'\t'}"

if [[ ! -v ENDPOINT_REQUESTS_DIMENSIONS["${key}"] ]]; then
echo "DIMENSION ${key} '' absolute"
ENDPOINT_REQUESTS_DIMENSIONS["${key}"]='printed'
fi

echo "SET ${key}=${value}"
done < <(env -i HAB_LICENSE="${HAB_LICENSE}" hab pkg exec "${EMERGENCE_RUNTIME:-emergence/php-runtime}" emergence-console-run gk:health:endpoint-requests)

echo "END"


# wait netdata-provided amount of time until next exec
sleep "${NETDATA_UPDATE_EVERY:-1}"
done
28 changes: 15 additions & 13 deletions php-classes/Gatekeeper/ApiRequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,27 @@ public static function handleRequest() {
,'timeout' => static::$defaultTimeout
,'timeoutConnect' => static::$defaultTimeoutConnect
# ,'debug' => true // uncomment to debug proxy process and see output following response
# ,'afterResponseSync' => true // uncomment to debug afterResponse code from browser
,'afterResponse' => function($responseBody, $responseHeaders, $options, $curlHandle) use ($request, &$metrics, &$beforeEvent) {
// ,'afterResponseSync' => true // true to debug afterResponse code from browser
,'afterResponse' => function ($responseBody, $responseHeaders, $options, $curlHandle) use ($request, &$metrics, &$beforeEvent) {

$curlInfo = curl_getinfo($curlHandle);
list($path, $query) = explode('?', $request->getUrl());


// initialize log record
$Transaction = Transaction::create([
'Endpoint' => $request->getEndpoint()
,'Key' => $request->getKey()
,'ClientIP' => ip2long($_SERVER['REMOTE_ADDR'])
,'Method' => $_SERVER['REQUEST_METHOD']
,'Path' => $path
,'Query' => $query
,'ResponseTime' => $curlInfo['starttransfer_time'] * 1000
,'ResponseCode' => $curlInfo['http_code']
,'ResponseBytes' => $curlInfo['size_download']
]);
if (!Cache::fetch('flags/gatekeeper/skip-insert-transaction')) {
$Transaction = Transaction::create([
'Endpoint' => $request->getEndpoint()
,'Key' => $request->getKey()
,'ClientIP' => ip2long($_SERVER['REMOTE_ADDR'])
,'Method' => $_SERVER['REQUEST_METHOD']
,'Path' => $path
,'Query' => $query
,'ResponseTime' => $curlInfo['starttransfer_time'] * 1000
,'ResponseCode' => $curlInfo['http_code']
,'ResponseBytes' => $curlInfo['size_download']
]);
}


// fire afterApiRequest
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
<?php

Emergence\People\Person::$relationships['Subscriptions'] = [
'type' => 'one-many',
'class' => Gatekeeper\Endpoints\Subscription::class
];

Emergence\People\Person::$relationships['Keys'] = [
'type' => 'many-many',
'class' => Gatekeeper\Keys\Key::class,
'linkClass' => Gatekeeper\Keys\KeyUser::class
];
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

Emergence\People\Person::$relationships['Subscriptions'] = [
'type' => 'one-many',
'class' => Gatekeeper\Endpoints\Subscription::class
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

/**
* Change the network interface that Gatekeeper makes requests from
*
* - `false`: no override (let cURL choose)
* - `null`: override automatically (use HTTP_HOST)
* - String: any IP or hostname
*/
Gatekeeper\ApiRequestHandler::$sourceInterface = false;
3 changes: 0 additions & 3 deletions php-config/Gatekeeper/ApiRequestHandler.config.php

This file was deleted.

8 changes: 8 additions & 0 deletions php-config/Gatekeeper/Gatekeeper.config.d/api-hostname.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

/**
* One or more hostnames to automatically prepend /api/ to requests for
*/
// Gatekeeper\Gatekeeper::$apiHostname = [
// 'api.example.com'
// ];
6 changes: 6 additions & 0 deletions php-config/Gatekeeper/Gatekeeper.config.d/portal-hostname.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

/**
* Hostname for generating links to public portal
*/
// Gatekeeper\Gatekeeper::$portalHostname = 'developer.example.com';
4 changes: 0 additions & 4 deletions php-config/Gatekeeper/Gatekeeper.config.php

This file was deleted.

0 comments on commit 3db6eb3

Please sign in to comment.