-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Philip Krauss
authored and
Philip Krauss
committed
Aug 14, 2018
1 parent
b99c3ad
commit 72c1766
Showing
8 changed files
with
237 additions
and
21 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,110 @@ | ||
package beater | ||
|
||
import( | ||
"fmt" | ||
"time" | ||
"strings" | ||
|
||
"net/http" | ||
"io/ioutil" | ||
"encoding/json" | ||
|
||
"github.com/elastic/beats/libbeat/beat" | ||
"github.com/elastic/beats/libbeat/logp" | ||
"github.com/elastic/beats/libbeat/common" | ||
) | ||
|
||
// | ||
// Http Response Struct for Concurrent GET's | ||
// | ||
type HttpResponse struct { | ||
Status string | ||
Body []byte | ||
Url string | ||
} | ||
|
||
// | ||
// Metric Endpoint Response | ||
// | ||
type Metric struct { | ||
Name string `json: "statistic"` | ||
Measurements []Measurement `json: "measurements"` | ||
} | ||
|
||
// | ||
// Sub Struct conatining micrometer.io Measurement struct | ||
// | ||
type Measurement struct { | ||
Statistic string `json: "statistic"` | ||
Value float32 `json: "value"` | ||
} | ||
|
||
// | ||
// Perform a HTTP GET and put the response back on the channel | ||
// | ||
// @param url | ||
// @param ch | ||
// | ||
func DoHttpGet(url string, ch chan<- HttpResponse) { | ||
client := http.Client{ | ||
Timeout: time.Duration(10 * time.Second), | ||
} | ||
request, _ := http.NewRequest("GET", url, nil) | ||
// TODO -- add basic auth | ||
// request.SetBasicAuth(bt.config.Username, bt.config.Password) | ||
response, err := client.Do(request) | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer response.Body.Close() | ||
|
||
// Fetch "good" Response Body | ||
body, err := ioutil.ReadAll(response.Body) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// Push back to Channel | ||
ch <- HttpResponse{response.Status, body, url} | ||
} | ||
|
||
// | ||
// Process the Metrics Actuator | ||
// | ||
// @access /actuator/metrics | ||
// | ||
func (bt *Springboot2beat) ProcessMetricsActuator(b *beat.Beat) { | ||
var ch chan HttpResponse = make(chan HttpResponse) | ||
url := fmt.Sprintf("%s/actuator/metrics", bt.config.Host) | ||
|
||
// Commit Metric Requests | ||
for _, metric := range(bt.config.Metrics) { | ||
go DoHttpGet(fmt.Sprintf("%s/%s", url, metric), ch) | ||
} | ||
|
||
// Init Metrics Map | ||
fields := common.MapStr{ | ||
"type": b.Info.Name, | ||
} | ||
|
||
// Join Results to Event | ||
for range(bt.config.Metrics) { | ||
response := <-ch | ||
metric := Metric{} | ||
json.Unmarshal(response.Body, &metric) | ||
prefix := strings.Replace(metric.Name, ".", "_", -1) | ||
for _, measurement := range(metric.Measurements) { | ||
key := fmt.Sprintf("%s_%s", prefix, strings.ToLower(measurement.Statistic)) | ||
fields[key] = measurement.Value | ||
} | ||
} | ||
close(ch) | ||
|
||
// Push Event | ||
event := beat.Event{ | ||
Timestamp: time.Now(), | ||
Fields: fields, | ||
} | ||
bt.client.Publish(event) | ||
logp.Info(fmt.Sprintf("Event with %d metrics sent."), len(fields)) | ||
} |
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 @@ | ||
{"uuid":"6436cb81-d61d-4afa-996e-c27a1746ea6d"} |
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