/
factories.go
116 lines (98 loc) · 3.29 KB
/
factories.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package factories
import (
"encoding/binary"
"fmt"
"net/http"
"strconv"
"strings"
"time"
"github.com/cloudfoundry/sonde-go/events"
"github.com/gogo/protobuf/proto"
uuid "github.com/nu7hatch/gouuid"
)
func NewUUID(id *uuid.UUID) *events.UUID {
return &events.UUID{Low: proto.Uint64(binary.LittleEndian.Uint64(id[:8])), High: proto.Uint64(binary.LittleEndian.Uint64(id[8:]))}
}
func NewHttpStartStop(req *http.Request, statusCode int, contentLength int64, peerType events.PeerType, requestId *uuid.UUID) *events.HttpStartStop {
now := proto.Int64(time.Now().UnixNano())
httpStartStop := &events.HttpStartStop{
StartTimestamp: now,
StopTimestamp: now,
RequestId: NewUUID(requestId),
PeerType: &peerType,
Method: events.Method(events.Method_value[req.Method]).Enum(),
Uri: proto.String(fmt.Sprintf("%s://%s%s", scheme(req), req.Host, req.URL.Path)),
RemoteAddress: proto.String(req.RemoteAddr),
UserAgent: proto.String(req.UserAgent()),
StatusCode: proto.Int(statusCode),
ContentLength: proto.Int64(contentLength),
}
if applicationId, err := uuid.ParseHex(req.Header.Get("X-CF-ApplicationID")); err == nil {
httpStartStop.ApplicationId = NewUUID(applicationId)
}
if instanceIndex, err := strconv.Atoi(req.Header.Get("X-CF-InstanceIndex")); err == nil {
httpStartStop.InstanceIndex = proto.Int(instanceIndex)
}
if instanceId := req.Header.Get("X-CF-InstanceID"); instanceId != "" {
httpStartStop.InstanceId = proto.String(instanceId)
}
allForwards := req.Header[http.CanonicalHeaderKey("X-Forwarded-For")]
for _, forwarded := range allForwards {
httpStartStop.Forwarded = append(httpStartStop.Forwarded, parseXForwarded(forwarded)...)
}
return httpStartStop
}
func NewError(source string, code int32, message string) *events.Error {
err := &events.Error{
Source: proto.String(source),
Code: proto.Int32(code),
Message: proto.String(message),
}
return err
}
func NewValueMetric(name string, value float64, unit string) *events.ValueMetric {
return &events.ValueMetric{
Name: proto.String(name),
Value: proto.Float64(value),
Unit: proto.String(unit),
}
}
func NewCounterEvent(name string, delta uint64) *events.CounterEvent {
return &events.CounterEvent{
Name: proto.String(name),
Delta: proto.Uint64(delta),
}
}
func NewLogMessage(messageType events.LogMessage_MessageType, messageString, appId, sourceType string) *events.LogMessage {
currentTime := time.Now()
logMessage := &events.LogMessage{
Message: []byte(messageString),
AppId: &appId,
MessageType: &messageType,
SourceType: proto.String(sourceType),
Timestamp: proto.Int64(currentTime.UnixNano()),
}
return logMessage
}
func NewContainerMetric(applicationId string, instanceIndex int32, cpuPercentage float64, memoryBytes uint64, diskBytes uint64) *events.ContainerMetric {
return &events.ContainerMetric{
ApplicationId: &applicationId,
InstanceIndex: &instanceIndex,
CpuPercentage: &cpuPercentage,
MemoryBytes: &memoryBytes,
DiskBytes: &diskBytes,
}
}
func parseXForwarded(forwarded string) []string {
addrs := strings.Split(forwarded, ",")
for i, addr := range addrs {
addrs[i] = strings.TrimSpace(addr)
}
return addrs
}
func scheme(req *http.Request) string {
if req.TLS == nil {
return "http"
}
return "https"
}