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

refactor: separate monitoring configuration from project initialization #93

Merged
merged 2 commits into from
Feb 10, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions FEATURES.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,10 @@ If the service already exists, skip creation of the service.
* README contains an example of how to call this function within a Jenkinsfile
* Finished event appears in Keptn Bridge (check for source="jenkins-library")

### User Story 12: Configure monitoring for keptn project

**Goal**: A user should be able to configure monitoring for a project after adding the relevant files to the Keptn configuration repo (SLIs/SLOs and monitoring config files as needed)

**DoD**:
* Shared library code contains function `keptnConfigureMonitoring` to trigger the appropriate event.
* README contains an example of how to call this function within a Jenkinsfile for prometheus and dynatrace
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ keptn.keptnAddResources('keptn/sli.yaml','dynatrace/sli.yaml')
keptn.keptnAddResources('keptn/slo.yaml','slo.yaml')
keptn.keptnAddResources('keptn/load.jmx','jmeter/load.jmx')

// Configure monitoring for your keptn project (using dynatrace or prometheus)
keptn.keptnConfigureMonitoring monitoring:"dynatrace"
// keptn.keptnConfigureMonitoring monitoring:"prometheus"

// Custom Labels
// all keptn.send** functions have an optional parameter called labels. It is a way to pass custom labels to the sent event
def labels=[:]
Expand Down
60 changes: 41 additions & 19 deletions src/sh/keptn/Keptn.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ def keptnInit(Map args) {
String service = args.containsKey("service") ? args.service : ""
String monitoring = args.containsKey("monitoring") ? args.monitoring : ""

if (monitoring != "") {
error("keptnInit no longer supports 'monitoring' argument. Use keptnConfigureMonitoring function after adding " +
"all the relevant resources/configurations to the respective project/service/stage")
}

if ((project == "") || (stage == "") || (service == "") ||
(keptn_endpoint == null) || (keptn_bridge == null) || (keptn_api_token == null)) {
error("keptnInit requires project, stage, service, keptn_endpoint, keptn_bridge and keptn_api_token to be set. These values cant be empty!")
Expand Down Expand Up @@ -206,11 +211,31 @@ def keptnInit(Map args) {
} else {
echo "Couldnt create Keptn Service ${service}: " + createServiceResponse.content
}
}

return true
}
def keptnConfigureMonitoring(Map args) {

def keptnInit = keptnLoadFromInit(args)

String monitoring = keptnEnrichedArgs.containsKey("monitoring") ? args.monitoring : ""

// Step #3: Configure Monitoring
// This will ensure that the monitoring tool of choice is configured
if(monitoring != "") {
def configureMonitoringBody = """{
if (!monitoring?.trim()) {
error("keptnConfigureMonitoring needs a 'monitoring' argument specifying the desired type of monitoring (e.g., dynatrace, prometheus)")
}

if ((!keptnEnrichedArgs.project?.trim()) || (!keptnEnrichedArgs.service?.trim()) ||
(!keptnEnrichedArgs.keptn_endpoint?.trim()) || (!keptnEnrichedArgs.keptn_api_token?.trim())) {
error("keptnConfigureMonitoring requires project, service, keptn_endpoint and keptn_api_token to be set. It seems that keptnInit() was not called!")
}

configureMonitoring(monitoring, keptnEnrichedArgs.project, keptnEnrichedArgs.service, keptnEnrichedArgs.keptn_api_token, keptnEnrichedArgs.keptn_endpoint)
}

private void configureMonitoring(String monitoring, String project, String service, String keptn_api_token, String keptn_endpoint) {
if (monitoring != "") {
def configureMonitoringBody = """{
| "data": {
| "project": "${project}",
| "service": "${service}",
Expand All @@ -222,26 +247,23 @@ def keptnInit(Map args) {
| "type": "sh.keptn.event.monitoring.configure"
|}
""".stripMargin()
def configureMonitoringResponse = httpRequest contentType: 'APPLICATION_JSON',
customHeaders: [[maskValue: true, name: 'x-token', value: "${keptn_api_token}"]],
httpMode: 'POST',
requestBody: configureMonitoringBody,
responseHandle: 'STRING',
url: "${keptn_endpoint}/v1/event",
def configureMonitoringResponse = httpRequest contentType: 'APPLICATION_JSON',
customHeaders: [[maskValue: true, name: 'x-token', value: "${keptn_api_token}"]],
httpMode: 'POST',
requestBody: configureMonitoringBody,
responseHandle: 'STRING',
url: "${keptn_endpoint}/v1/event",
validResponseCodes: "100:404",
ignoreSslErrors: true

if (configureMonitoringResponse.status == 200) {
echo "Successfully configured monitoring for: ${monitoring}"
echo "body: ${configureMonitoringBody}"
} else {
echo "Couldnt configure monitoring for ${monitoring}: " + configureMonitoringResponse.content
echo "body: ${configureMonitoringBody}"
}
if (configureMonitoringResponse.status == 200) {
echo "Successfully configured monitoring for: ${monitoring}"
echo "body: ${configureMonitoringBody}"
} else {
echo "Couldnt configure monitoring for ${monitoring}: " + configureMonitoringResponse.content
echo "body: ${configureMonitoringBody}"
pchila marked this conversation as resolved.
Show resolved Hide resolved
}
}

return true
}

/**
Expand Down