-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrometheusDataWriter.scala
130 lines (108 loc) · 4.75 KB
/
PrometheusDataWriter.scala
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* Copyright 2011-2019 GatlingCorp (https://gatling.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gatling.prometheus
import java.io.IOException
import io.gatling.commons.util.Clock
import io.prometheus.client.{ Counter, Histogram }
import io.prometheus.client.exporter.HTTPServer
import io.gatling.core.config.GatlingConfiguration
import io.gatling.core.stats.message.{ End, Start }
import io.gatling.core.stats.writer._
case class PrometheusData(
startedUsers: Counter,
finishedUsers: Counter,
requestLatencyHist: Histogram,
groupRequest: Histogram,
errorCounter: Counter,
simulation: String,
server: Option[HTTPServer]
) extends DataWriterData
class PrometheusDataWriter(clock: Clock, configuration: GatlingConfiguration) extends DataWriter[PrometheusData] {
override def onInit(init: Init): PrometheusData = {
var port = configuration.data.prometheus.port
var server: Option[HTTPServer] = None
try {
logger.info(s"trying to start Prometheus Endpoint Server on port: $port")
server = Option(new HTTPServer(port, true))
port = server.get.getPort
logger.info(s"Started Prometheus Endpoint Server on port: $port")
} catch {
case e: IOException => {
logger.info(String.format(s"Failed to start the Prometheus server on port $port, Got IO Exception"))
e.printStackTrace()
}
}
PrometheusData(
startedUsers = Counter.build.name("total_started_users").labelNames("simulation")
.help("Total Gatling users Started").register,
finishedUsers = Counter.build.name("total_finished_users").labelNames("simulation")
.help("Total Gatling users Finished").register,
requestLatencyHist = Histogram.build.name("requests_latency_secondsHistogram")
.help("Request latency in seconds.").labelNames("simulation", "group", "metric", "error", "responseCode", "oK").register,
groupRequest = Histogram.build.name("groupRequestInfo")
.help("Metrics for group requests.").labelNames( "group", "oK").register,
errorCounter = Counter.build.name("error_msg_count")
.help("Keeps count of each error message").labelNames("simulation", "errorMsg").register,
simulation = init.runMessage.simulationId,
server = server
)
}
override def onMessage(message: LoadEventMessage, data: PrometheusData): Unit = message match {
case user: UserMessage => onUserMessage(user, data)
case response: ResponseMessage => onResponseMessage(response, data)
case error: ErrorMessage => onErrorMessage(error, data)
case groupMessage: GroupMessage => onGroupMessage(groupMessage, data)
case _ =>
}
override def onFlush(data: PrometheusData): Unit = {}
override def onCrash(cause: String, data: PrometheusData): Unit = {
if (data.server.isDefined)
data.server.get.stop()
}
def onStop(data: PrometheusData): Unit = {
if (data.server.isDefined)
data.server.get.stop()
}
private def onUserMessage(user: UserMessage, data: PrometheusData): Unit = {
import user._
event match {
case Start =>
data.startedUsers.labels(data.simulation).inc()
case End =>
data.finishedUsers.labels(data.simulation).inc()
}
}
private def onResponseMessage(response: ResponseMessage, data: PrometheusData): Unit = {
import response.{ endTimestamp, startTimestamp, groupHierarchy, name, message, responseCode, status }
logger.debug(s"Received Response message, ${name}")
data.requestLatencyHist.labels(
data.simulation,groupHierarchy.head, name, message.getOrElse(""), responseCode.getOrElse("0"), status.toString
)
.observe((endTimestamp - startTimestamp) / 1000D)
}
private def onGroupMessage(groupMessage: GroupMessage, data: PrometheusData): Unit = {
import groupMessage.{ endTimestamp, startTimestamp, groupHierarchy, status }
// logger.debug(s"Received Response message, ${name}")
data.groupRequest.labels(
groupHierarchy.head,
status.toString,
)
.observe((endTimestamp - startTimestamp) / 1000D)
}
private def onErrorMessage(error: ErrorMessage, data: PrometheusData): Unit = {
data.errorCounter.labels(data.simulation, error.message).inc()
}
}