|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SamuelBednarcik\ElasticAPMAgent; |
|
4
|
|
|
|
|
5
|
|
|
use SamuelBednarcik\ElasticAPMAgent\Events\Error; |
|
6
|
|
|
use SamuelBednarcik\ElasticAPMAgent\Events\Metadata; |
|
7
|
|
|
use SamuelBednarcik\ElasticAPMAgent\Events\Span; |
|
8
|
|
|
use SamuelBednarcik\ElasticAPMAgent\Events\Transaction; |
|
9
|
|
|
use SamuelBednarcik\ElasticAPMAgent\Exception\BadEventRequestException; |
|
10
|
|
|
use SamuelBednarcik\ElasticAPMAgent\Serializer\ElasticAPMSerializer; |
|
11
|
|
|
use Symfony\Component\Serializer\Encoder\JsonEncoder; |
|
12
|
|
|
|
|
13
|
|
|
class EventIntakeRequest |
|
14
|
|
|
{ |
|
15
|
|
|
const INTAKE_ENDPOINT = '/intake/v2/events'; |
|
16
|
|
|
const CONTENT_TYPE = 'application/x-ndjson'; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var ElasticAPMSerializer |
|
20
|
|
|
*/ |
|
21
|
|
|
private $serializer; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var Metadata |
|
25
|
|
|
*/ |
|
26
|
|
|
private $metadata; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var Transaction[] |
|
30
|
|
|
*/ |
|
31
|
|
|
private $transactions; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var Span[] |
|
35
|
|
|
*/ |
|
36
|
|
|
private $spans; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var Error[] |
|
40
|
|
|
*/ |
|
41
|
|
|
private $errors; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param ElasticAPMSerializer $serializer |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct(ElasticAPMSerializer $serializer) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->serializer = $serializer; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @return Metadata |
|
53
|
|
|
*/ |
|
54
|
|
|
public function getMetadata(): Metadata |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->metadata; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param Metadata $metadata |
|
61
|
|
|
*/ |
|
62
|
|
|
public function setMetadata(Metadata $metadata): void |
|
63
|
|
|
{ |
|
64
|
|
|
$this->metadata = $metadata; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return Transaction[] |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getTransactions(): array |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->transactions; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param Transaction[] $transactions |
|
77
|
|
|
*/ |
|
78
|
|
|
public function setTransactions(array $transactions): void |
|
79
|
|
|
{ |
|
80
|
|
|
$this->transactions = $transactions; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param Transaction $transaction |
|
85
|
|
|
*/ |
|
86
|
|
|
public function addTransaction(Transaction $transaction): void |
|
87
|
|
|
{ |
|
88
|
|
|
$this->transactions[] = $transaction; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @return Span[] |
|
93
|
|
|
*/ |
|
94
|
|
|
public function getSpans(): array |
|
95
|
|
|
{ |
|
96
|
|
|
return $this->spans; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param Span[] $spans |
|
101
|
|
|
*/ |
|
102
|
|
|
public function setSpans(array $spans): void |
|
103
|
|
|
{ |
|
104
|
|
|
$this->spans = $spans; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param Span $span |
|
109
|
|
|
*/ |
|
110
|
|
|
public function addSpan(Span $span): void |
|
111
|
|
|
{ |
|
112
|
|
|
$this->spans[] = $span; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @return Error[] |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getErrors(): array |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->errors; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @param Error[] $errors |
|
125
|
|
|
*/ |
|
126
|
|
|
public function setErrors(array $errors): void |
|
127
|
|
|
{ |
|
128
|
|
|
$this->errors = $errors; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @param Error $error |
|
133
|
|
|
*/ |
|
134
|
|
|
public function addError(Error $error): void |
|
135
|
|
|
{ |
|
136
|
|
|
$this->errors[] = $error; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Returns NDJSON request body |
|
141
|
|
|
* @throws BadEventRequestException |
|
142
|
|
|
*/ |
|
143
|
|
|
public function getRequestBody(): string |
|
144
|
|
|
{ |
|
145
|
|
|
if ($this->metadata === null) { |
|
146
|
|
|
throw new BadEventRequestException('Metadata must be defined!'); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
$json = $this->serializer->encode( |
|
150
|
|
|
['metadata' => $this->serializer->normalize($this->metadata)], |
|
151
|
|
|
JsonEncoder::FORMAT |
|
152
|
|
|
); |
|
153
|
|
|
|
|
154
|
|
|
$json .= "\n"; |
|
155
|
|
|
|
|
156
|
|
|
foreach ($this->transactions as $transaction) { |
|
157
|
|
|
$json .= "\n"; |
|
158
|
|
|
$json .= $this->serializer->encode( |
|
159
|
|
|
['transaction' => $this->serializer->normalize($transaction)], |
|
160
|
|
|
JsonEncoder::FORMAT |
|
161
|
|
|
); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
$json .= "\n"; |
|
165
|
|
|
|
|
166
|
|
|
foreach ($this->spans as $span) { |
|
167
|
|
|
$json .= "\n"; |
|
168
|
|
|
$json .= $this->serializer->encode( |
|
169
|
|
|
['span' => $this->serializer->normalize($span)], |
|
170
|
|
|
JsonEncoder::FORMAT |
|
171
|
|
|
); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
$json .= "\n"; |
|
175
|
|
|
|
|
176
|
|
|
foreach ($this->errors as $error) { |
|
177
|
|
|
$json .= "\n"; |
|
178
|
|
|
$json .= $this->serializer->encode( |
|
179
|
|
|
['error' => $this->serializer->normalize($error)], |
|
180
|
|
|
JsonEncoder::FORMAT |
|
181
|
|
|
); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
return $json; |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|