Skip to content

Commit

Permalink
refactor: separate monitoring configuration from project initializati…
Browse files Browse the repository at this point in the history
…on (#93)

Remove monitoring configuration from keptnInit and introduce a new function keptnConfigureMonitoring to be called after all the appropriate resources have been added to the keptn project.
If monitoring is still passed to keptnInit the pipeline will fail with an appropriate error message.

Signed-off-by: Paolo Chila [email protected]
BREAKING CHANGE: passing a monitoring type during init will make the pipeline fail! Please remove the argument and use keptnConfigureMonitoring
  • Loading branch information
pchila authored Feb 10, 2022
1 parent 8afff0e commit e93e7f0
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 19 deletions.
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}"
}
}

return true
}

/**
Expand Down

0 comments on commit e93e7f0

Please sign in to comment.