Skip to content

Commit 67f3a70

Browse files
authored
Merge pull request #6 from Bo0mer/develop
Expose AppMonitor and Emitter objects
2 parents cb28ed3 + b85f04a commit 67f3a70

File tree

17 files changed

+2856
-614
lines changed

17 files changed

+2856
-614
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@
33

44
Pull metrics for Cloud Foundry applications and forward them to Riemann.
55

6+
This repo provides two functionalities - a `mozzle` Go package and a mozzle
7+
command-line tool.
8+
9+
Package mozzle provides an API for monitoring infrastructure metrics of
10+
Cloud Foundry applications and emitting them to a 3rd party monitoring system.
11+
12+
The `mozzle` command-line tool emits metrics for Cloud Foundry applications
13+
to a specified Riemann instance. The rest of this document describes its usage.
14+
615
## User's guide
716
```
817
Usage of mozzle:
@@ -30,7 +39,7 @@ Example:
3039
The following command will emit metrics for all applications under the `NASA`
3140
organization, within the `rocket` space.
3241
```
33-
mozzle -api api.bosh-lite.com -org NASA -space rocket
42+
mozzle -api https://api.bosh-lite.com -org NASA -space rocket
3443
```
3544

3645
### Demo usage

app.go

Lines changed: 0 additions & 20 deletions
This file was deleted.

appeventmetrics.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package mozzle
2+
3+
import "github.com/Bo0mer/ccv2"
4+
5+
type applicationEvent struct {
6+
ccv2.Event
7+
App application
8+
}
9+
10+
func (e applicationEvent) EmitTo(emitter Emitter) {
11+
attributes := attributes(e.App)
12+
attributes["event"] = e.Entity.Type
13+
attributes["actee"] = e.Entity.ActeeName
14+
attributes["actee_type"] = e.Entity.ActeeType
15+
attributes["actor"] = e.Entity.ActorName
16+
attributes["actor_type"] = e.Entity.ActorType
17+
18+
emitter.Emit(forApp(e.App, Metric{
19+
Time: e.Entity.Timestamp.Unix(),
20+
Service: "app event",
21+
Metric: 1,
22+
State: "ok",
23+
Attributes: attributes,
24+
}))
25+
}

appmetrics.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package mozzle
2+
3+
import "github.com/Bo0mer/ccv2"
4+
5+
type applicationMetrics struct {
6+
ccv2.ApplicationSummary
7+
App application
8+
}
9+
10+
func (m applicationMetrics) EmitTo(e Emitter) {
11+
attributes := attributes(m.App)
12+
state := "ok"
13+
if m.RunningInstances < m.Instances {
14+
state = "warn"
15+
if m.RunningInstances == 0 {
16+
state = "critical"
17+
}
18+
}
19+
20+
e.Emit(forApp(m.App, Metric{
21+
Service: "instance running_count",
22+
Metric: m.RunningInstances,
23+
State: state,
24+
Attributes: attributes,
25+
}))
26+
27+
e.Emit(forApp(m.App, Metric{
28+
Service: "instance configured_count",
29+
Metric: m.Instances,
30+
State: state,
31+
Attributes: attributes,
32+
}))
33+
}

cmd/mozzle/main.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"context"
77
"flag"
88
"fmt"
9-
"log"
109
"os"
1110
"os/signal"
1211

@@ -23,7 +22,7 @@ var (
2322

2423
riemannAddr string
2524

26-
eventsTtl float64
25+
eventsTTL float64
2726
queueSize int
2827
)
2928

@@ -37,13 +36,20 @@ func init() {
3736

3837
flag.StringVar(&riemannAddr, "riemann", "127.0.0.1:5555", "Address of the Riemann endpoint")
3938

40-
flag.Float64Var(&eventsTtl, "events-ttl", 30.0, "TTL for emitted events (in seconds)")
39+
flag.Float64Var(&eventsTTL, "events-ttl", 30.0, "TTL for emitted events (in seconds)")
4140
flag.IntVar(&queueSize, "events-queue-size", 256, "Queue size for outgoing events")
4241
}
4342

4443
func main() {
4544
flag.Parse()
46-
mozzle.Initialize(riemannAddr, float32(eventsTtl), queueSize)
45+
riemann := &mozzle.RiemannEmitter{}
46+
riemann.Initialize(riemannAddr, float32(eventsTTL), queueSize)
47+
defer func() {
48+
if err := riemann.Close(); err != nil {
49+
fmt.Printf("mozzle: error closing riemann emitter: %v\n", err)
50+
}
51+
}()
52+
4753
t := mozzle.Target{
4854
API: apiAddr,
4955
Username: username,
@@ -62,7 +68,7 @@ func main() {
6268
cancel()
6369
}()
6470

65-
if err := mozzle.Monitor(ctx, t); err != nil {
66-
log.Fatal(err)
71+
if err := mozzle.Monitor(ctx, t, riemann); err != nil {
72+
fmt.Printf("mozzle: error occured during Monitor: %v\n", err)
6773
}
6874
}

containermetrics.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package mozzle
2+
3+
import (
4+
"strconv"
5+
6+
cfevent "github.com/cloudfoundry/sonde-go/events"
7+
)
8+
9+
type containerMetrics struct {
10+
*cfevent.ContainerMetric
11+
App application
12+
}
13+
14+
func (m containerMetrics) EmitTo(e Emitter) {
15+
attributes := attributes(m.App)
16+
attributes["instance"] = strconv.Itoa(int(m.GetInstanceIndex()))
17+
18+
e.Emit(forApp(m.App, Metric{
19+
Service: "memory used_bytes",
20+
Metric: int(m.GetMemoryBytes()),
21+
State: "ok",
22+
Attributes: attributes,
23+
}))
24+
e.Emit(forApp(m.App, Metric{
25+
Service: "memory total_bytes",
26+
Metric: int(m.GetMemoryBytesQuota()),
27+
State: "ok",
28+
Attributes: attributes,
29+
}))
30+
e.Emit(forApp(m.App, Metric{
31+
Service: "memory used_ratio",
32+
Metric: ratio(m.GetMemoryBytes(), m.GetMemoryBytesQuota()),
33+
State: "ok",
34+
Attributes: attributes,
35+
}))
36+
37+
e.Emit(forApp(m.App, Metric{
38+
Service: "disk used_bytes",
39+
Metric: int(m.GetDiskBytes()),
40+
State: "ok",
41+
Attributes: attributes,
42+
}))
43+
e.Emit(forApp(m.App, Metric{
44+
Service: "disk total_bytes",
45+
Metric: int(m.GetDiskBytesQuota()),
46+
State: "ok",
47+
Attributes: attributes,
48+
}))
49+
e.Emit(forApp(m.App, Metric{
50+
Service: "disk used_ratio",
51+
Metric: ratio(m.GetDiskBytes(), m.GetDiskBytesQuota()),
52+
State: "ok",
53+
Attributes: attributes,
54+
}))
55+
56+
e.Emit(forApp(m.App, Metric{
57+
Service: "cpu_percent",
58+
Metric: m.GetCpuPercentage(),
59+
State: "ok",
60+
Attributes: attributes,
61+
}))
62+
}

demo/mib/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.vagrant

demo/mib/Vagrantfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Vagrant.configure(2) do |config|
3232
config.vm.provision "shell", keep_color: true, path: "scripts/riemann.sh"
3333

3434
# Provision Grafana dashboards
35-
config.vm.provision "file", source: "dashboards/summary.json", destination: "~/summary.json"
35+
config.vm.provision "file", source: "dashboards/", destination: "~/dashboards/"
3636
config.vm.provision "shell", keep_color: true, path: "scripts/configure_dashboards.sh"
3737

3838
end

0 commit comments

Comments
 (0)