-
Notifications
You must be signed in to change notification settings - Fork 191
Eureka health status #78
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
Open
michl-b
wants to merge
10
commits into
danielbayerlein:master
Choose a base branch
from
michl-b:eureka-health-status
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4cf2db5
added EurekaHealthStatus component & documentation
michael-bartsch-witt 156c236
check instance-count of services
michael-bartsch-witt 2330089
Merge remote-tracking branch 'upstream/master' into eureka-health-status
michael-bartsch-witt 6844d6a
fixed standard sytle issues
michael-bartsch-witt ff56d21
refactoring & app instance health check
michael-bartsch-witt 4b0b899
Merge remote-tracking branch 'upstream/master' into eureka-health-status
michael-bartsch-witt 6c33822
readme added eureka health status to index
michael-bartsch-witt 805c89d
use clear veriable names, code cleanup
michael-bartsch-witt 1dc1319
fixed review format issues, completed README
michael-bartsch-witt d61a354
fixed review format issues & README
michael-bartsch-witt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,168 @@ | ||
import { Component } from 'react' | ||
import fetch from 'isomorphic-unfetch' | ||
import yup from 'yup' | ||
import Widget from '../../widget' | ||
import Table, { Th, Td } from '../../table' | ||
import { basicAuthHeader } from '../../../lib/auth' | ||
import styled from 'styled-components' | ||
|
||
const schema = yup.object().shape({ | ||
url: yup.string().url().required(), | ||
interval: yup.number(), | ||
eurekaQuery: yup.string().required(), | ||
appsQuery: yup.string().required(), | ||
healthQuery: yup.string().required(), | ||
title: yup.string(), | ||
minimumInstances: yup.number(), | ||
appNamePattern: yup.string(), | ||
authKey: yup.string() | ||
}) | ||
|
||
const EurekaDiv = styled.div` | ||
background-color: ${props => props.hasError ? props.theme.palette.errorColor : props.theme.palette.canvasColor}; | ||
` | ||
|
||
export default class EurekaHealthStatus extends Component { | ||
static defaultProps = { | ||
interval: 1000 * 60 * 60, | ||
title: 'Eureka Health Status', | ||
minimumInstances: 2, | ||
appNamePattern: '' | ||
} | ||
|
||
state = { | ||
error: false, | ||
loading: true, | ||
appStatus: '', | ||
infoMessage: '' | ||
} | ||
|
||
constructor (props, context) { | ||
super(props, context) | ||
this.checkInstanceCount = this.checkInstanceCount.bind(this) | ||
} | ||
|
||
componentDidMount () { | ||
schema.validate(this.props) | ||
.then(() => this.fetchInformation()) | ||
.catch((err) => { | ||
console.error(`${err.name} @ ${this.constructor.name}`, err.errors) | ||
this.setState({ error: true, loading: false }) | ||
}) | ||
} | ||
|
||
componentWillUnmount () { | ||
clearTimeout(this.timeout) | ||
} | ||
|
||
checkInstanceCount (appList) { | ||
const { appNamePattern, minimumInstances } = this.props | ||
let hasError = false | ||
appList.forEach(function (entry) { | ||
if ((appNamePattern.length === 0 || entry.name.startsWith(appNamePattern)) && entry.instance.length < minimumInstances) { | ||
hasError = true | ||
} | ||
}) | ||
return hasError | ||
} | ||
|
||
async checkInstanceHealth (url, appNamePattern, appList) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can optimize this function in the next step. @chrishelgert Can you support @michl-b, please? |
||
let hasError = false | ||
for (var i = 0; i < appList.length; i++) { | ||
const app = appList[i] | ||
if (appNamePattern.length === 0 || app.name.startsWith(appNamePattern)) { | ||
for (var j = 0; j < app.instance.length; j++) { | ||
try { | ||
const curInstance = app.instance[j] | ||
let opts = {headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }} | ||
const resHealth = await fetch(url + curInstance.healthCheckUrl, opts) | ||
const healthJson = await resHealth.json() | ||
hasError = healthJson.status !== 'UP' | ||
} catch (error) { | ||
hasError = true | ||
} | ||
} | ||
} | ||
} | ||
return hasError | ||
} | ||
|
||
async fetchInformation () { | ||
const { authKey, url, eurekaQuery, healthQuery, appsQuery, appNamePattern } = this.props | ||
let opts = authKey ? { headers: basicAuthHeader(authKey) } : {} | ||
|
||
try { | ||
const res = await fetch(`${url}${eurekaQuery}${healthQuery}`, opts) | ||
const json = await res.json() | ||
|
||
let appStatus = '' | ||
let infoMessage = '' | ||
let hasError = json.status !== 'UP' | ||
|
||
if (!hasError) { | ||
opts = {headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }} | ||
|
||
try { | ||
const resApps = await fetch(`${url}${eurekaQuery}${appsQuery}`, opts) | ||
const jsonApps = await resApps.json() | ||
|
||
hasError = jsonApps.applications.apps__hashcode.includes('DOWN') | ||
|
||
const appsStatus = jsonApps.applications.apps__hashcode.split('_') | ||
if (appsStatus.length > 0 && appsStatus.length < 4) { | ||
appStatus = `${appsStatus[0]}: ${appsStatus[1]}` | ||
} else { | ||
appStatus = `${appsStatus[0]}: ${appsStatus[1]} - ${appsStatus[2]}: ${appsStatus[3]}` | ||
} | ||
|
||
if (!hasError) { | ||
hasError = this.checkInstanceCount(jsonApps.applications.application) | ||
if (hasError) { | ||
infoMessage = 'Instance redundancy failed' | ||
} | ||
} | ||
|
||
if (!hasError) { | ||
hasError = await this.checkInstanceHealth(url, appNamePattern, jsonApps.applications.application) | ||
if (hasError) { | ||
infoMessage = 'App health check failed' | ||
} | ||
} | ||
} catch (error) { | ||
hasError = true | ||
} | ||
} else { | ||
infoMessage = 'Eureka health failed' | ||
} | ||
this.setState({ appStatus, infoMessage, hasError, error: false, loading: false }) | ||
} catch (error) { | ||
this.setState({ error: true, loading: false }) | ||
} finally { | ||
this.timeout = setTimeout(() => this.fetchInformation(), this.props.interval) | ||
} | ||
} | ||
|
||
render () { | ||
const { error, loading, appStatus, infoMessage, hasError } = this.state | ||
const { title } = this.props | ||
|
||
return ( | ||
<Widget title={title} loading={loading} error={error}> | ||
<EurekaDiv hasError={hasError}> | ||
<Table> | ||
<tbody> | ||
<tr> | ||
<Th>Apps</Th> | ||
<Td>{appStatus}</Td> | ||
</tr> | ||
<tr> | ||
<Th>Info</Th> | ||
<Td>{infoMessage}</Td> | ||
</tr> | ||
</tbody> | ||
</Table> | ||
</EurekaDiv> | ||
</Widget> | ||
) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have an look to
every
, it's more elegant.Untested:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
every is not working because it stops with the first
false
return value. So one element is excluded by namePattern, no more apps are checked.