|
11 | 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 | 12 | # See the License for the specific language governing permissions and
|
13 | 13 | # limitations under the License.
|
| 14 | +import logging |
14 | 15 | import re
|
15 | 16 | from typing import Dict, Sequence
|
16 | 17 |
|
| 18 | +import requests |
| 19 | + |
| 20 | +import snappy |
17 | 21 | from opentelemetry.exporter.prometheus_remote_write.gen.remote_pb2 import (
|
18 | 22 | WriteRequest,
|
19 | 23 | )
|
|
35 | 39 | ValueObserverAggregator,
|
36 | 40 | )
|
37 | 41 |
|
| 42 | +logger = logging.getLogger(__name__) |
| 43 | + |
38 | 44 |
|
39 | 45 | class PrometheusRemoteWriteMetricsExporter(MetricsExporter):
|
40 | 46 | """
|
@@ -133,10 +139,13 @@ def headers(self, headers: Dict):
|
133 | 139 | def export(
|
134 | 140 | self, export_records: Sequence[ExportRecord]
|
135 | 141 | ) -> MetricsExportResult:
|
136 |
| - raise NotImplementedError() |
| 142 | + timeseries = self.convert_to_timeseries(export_records) |
| 143 | + message = self.build_message(timeseries) |
| 144 | + headers = self.get_headers() |
| 145 | + return self.send_message(message, headers) |
137 | 146 |
|
138 | 147 | def shutdown(self) -> None:
|
139 |
| - raise NotImplementedError() |
| 148 | + pass |
140 | 149 |
|
141 | 150 | def convert_to_timeseries(
|
142 | 151 | self, export_records: Sequence[ExportRecord]
|
@@ -265,12 +274,49 @@ def create_label(self, name: str, value: str) -> Label:
|
265 | 274 | return label
|
266 | 275 |
|
267 | 276 | def build_message(self, timeseries: Sequence[TimeSeries]) -> bytes:
|
268 |
| - raise NotImplementedError() |
| 277 | + write_request = WriteRequest() |
| 278 | + write_request.timeseries.extend(timeseries) |
| 279 | + serialized_message = write_request.SerializeToString() |
| 280 | + return snappy.compress(serialized_message) |
269 | 281 |
|
270 | 282 | def get_headers(self) -> Dict:
|
271 |
| - raise NotImplementedError() |
| 283 | + headers = { |
| 284 | + "Content-Encoding": "snappy", |
| 285 | + "Content-Type": "application/x-protobuf", |
| 286 | + "X-Prometheus-Remote-Write-Version": "0.1.0", |
| 287 | + } |
| 288 | + if hasattr(self, "headers"): |
| 289 | + for header_name, header_value in self.headers.items(): |
| 290 | + headers[header_name] = header_value |
| 291 | + |
| 292 | + if "Authorization" not in headers: |
| 293 | + if hasattr(self, "bearer_token"): |
| 294 | + headers["Authorization"] = "Bearer " + self.bearer_token |
| 295 | + elif hasattr(self, "bearer_token_file"): |
| 296 | + with open(self.bearer_token_file) as file: |
| 297 | + headers["Authorization"] = "Bearer " + file.readline() |
| 298 | + return headers |
272 | 299 |
|
273 | 300 | def send_message(
|
274 | 301 | self, message: bytes, headers: Dict
|
275 | 302 | ) -> MetricsExportResult:
|
276 |
| - raise NotImplementedError() |
| 303 | + auth = None |
| 304 | + if hasattr(self, "basic_auth"): |
| 305 | + basic_auth = self.basic_auth |
| 306 | + if "password" in basic_auth: |
| 307 | + auth = (basic_auth.username, basic_auth.password) |
| 308 | + else: |
| 309 | + with open(basic_auth.password_file) as file: |
| 310 | + auth = (basic_auth.username, file.readline()) |
| 311 | + response = requests.post( |
| 312 | + self.endpoint, data=message, headers=headers, auth=auth |
| 313 | + ) |
| 314 | + if response.status_code != 200: |
| 315 | + logger.warning( |
| 316 | + "POST request failed with status %s with reason: %s and content: %s", |
| 317 | + str(response.status_code), |
| 318 | + response.reason, |
| 319 | + str(response.content), |
| 320 | + ) |
| 321 | + return MetricsExportResult.FAILURE |
| 322 | + return MetricsExportResult.SUCCESS |
0 commit comments