Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Philip Krauss authored and Philip Krauss committed Aug 14, 2018
1 parent b99c3ad commit 72c1766
Show file tree
Hide file tree
Showing 8 changed files with 237 additions and 21 deletions.
34 changes: 33 additions & 1 deletion _meta/beat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,36 @@

springboot2beat:
# Defines how often an event is sent to the output
period: 1s
period: 15s
# Spring Boot 2 - Managment Adress
host: "http://127.0.0.1:9001"
# Metrics to "beat"
metrics:
- "jvm.memory.max"
- "jvm.memory.used"
- "jvm.memory.committed"
- "jvm.threads.daemon"
- "jvm.threads.live"
- "jvm.threads.peak"
- "jvm.buffer.memory.used"
- "jvm.buffer.count"
- "jvm.buffer.total.capacity"
- "jvm.gc.memory.promoted"
- "jvm.gc.max.data.size"
- "jvm.gc.pause"
- "jvm.gc.memory.allocated"
- "http.server.requests.percentile"
- "http.server.requests.histogram"
- "http.server.requests"
- "system.load.average.1m"
- "system.cpu.usage"
- "tomcat.global.request.max"
- "tomcat.global.request"
- "tomcat.global.error"
- "tomcat.global.received"
- "tomcat.sessions.active.max"
- "tomcat.sessions.created"
- "tomcat.sessions.expired"
- "tomcat.sessions.rejected"
- "tomcat.sessions.active.current"
- "tomcat.sessions.alive.max"
24 changes: 20 additions & 4 deletions _meta/fields.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,24 @@
title: springboot2beat
description:
fields:
- name: counter
type: long
- name: "jvm_memory_max_value"
type: float
required: true
description: >
PLEASE UPDATE DOCUMENTATION
- name: "jvm_memory_used_value"
type: float
required: true
- name: "jvm_memory_committed_value"
type: float
required: true
- name: "jvm_threads_daemon_value"
type: float
- name: "jvm_threads_live_value"
type: float
- name: "jvm_threads_peak_value"
type: float
- name: "jvm_buffer_memory_used_value"
type: float
- name: "jvm_buffer_count_value"
type: float
- name: "jvm_buffer_total_capacity_value"
type: float
110 changes: 110 additions & 0 deletions beater/actuators.go
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))
}
13 changes: 1 addition & 12 deletions beater/springboot2beat.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,13 @@ func (bt *Springboot2beat) Run(b *beat.Beat) error {
}

ticker := time.NewTicker(bt.config.Period)
counter := 1
for {
bt.ProcessMetricsActuator(b)
select {
case <-bt.done:
return nil
case <-ticker.C:
}

event := beat.Event{
Timestamp: time.Now(),
Fields: common.MapStr{
"type": b.Info.Name,
"counter": counter,
},
}
bt.client.Publish(event)
logp.Info("Event sent")
counter++
}
}

Expand Down
8 changes: 6 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ package config
import "time"

type Config struct {
Period time.Duration `config:"period"`
Period time.Duration `config:"period"`
Scheme string `config:"scheme"`
Host string `config:"host"`
Port uint `config:"port"`
Metrics []string `config:"metrics"`
}

var DefaultConfig = Config{
Period: 1 * time.Second,
Period: 1 * time.Second,
}
1 change: 1 addition & 0 deletions data/meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"uuid":"6436cb81-d61d-4afa-996e-c27a1746ea6d"}
34 changes: 33 additions & 1 deletion springboot2beat.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,39 @@

springboot2beat:
# Defines how often an event is sent to the output
period: 1s
period: 15s
# Spring Boot 2 - Managment Adress
host: "http://127.0.0.1:9001"
# Metrics to "beat"
metrics:
- "jvm.memory.max"
- "jvm.memory.used"
- "jvm.memory.committed"
- "jvm.threads.daemon"
- "jvm.threads.live"
- "jvm.threads.peak"
- "jvm.buffer.memory.used"
- "jvm.buffer.count"
- "jvm.buffer.total.capacity"
- "jvm.gc.memory.promoted"
- "jvm.gc.max.data.size"
- "jvm.gc.pause"
- "jvm.gc.memory.allocated"
- "http.server.requests.percentile"
- "http.server.requests.histogram"
- "http.server.requests"
- "system.load.average.1m"
- "system.cpu.usage"
- "tomcat.global.request.max"
- "tomcat.global.request"
- "tomcat.global.error"
- "tomcat.global.received"
- "tomcat.sessions.active.max"
- "tomcat.sessions.created"
- "tomcat.sessions.expired"
- "tomcat.sessions.rejected"
- "tomcat.sessions.active.current"
- "tomcat.sessions.alive.max"

#================================ General ======================================

Expand Down
34 changes: 33 additions & 1 deletion springboot2beat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,39 @@

springboot2beat:
# Defines how often an event is sent to the output
period: 1s
period: 15s
# Spring Boot 2 - Managment Adress
host: "http://127.0.0.1:9001"
# Metrics to "beat"
metrics:
- "jvm.memory.max"
- "jvm.memory.used"
- "jvm.memory.committed"
- "jvm.threads.daemon"
- "jvm.threads.live"
- "jvm.threads.peak"
- "jvm.buffer.memory.used"
- "jvm.buffer.count"
- "jvm.buffer.total.capacity"
- "jvm.gc.memory.promoted"
- "jvm.gc.max.data.size"
- "jvm.gc.pause"
- "jvm.gc.memory.allocated"
- "http.server.requests.percentile"
- "http.server.requests.histogram"
- "http.server.requests"
- "system.load.average.1m"
- "system.cpu.usage"
- "tomcat.global.request.max"
- "tomcat.global.request"
- "tomcat.global.error"
- "tomcat.global.received"
- "tomcat.sessions.active.max"
- "tomcat.sessions.created"
- "tomcat.sessions.expired"
- "tomcat.sessions.rejected"
- "tomcat.sessions.active.current"
- "tomcat.sessions.alive.max"

#================================ General =====================================

Expand Down

0 comments on commit 72c1766

Please sign in to comment.