Skip to content

Commit

Permalink
fix: Fixes batch url computation
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkheir committed Nov 3, 2023
1 parent bdf09f6 commit cf2674d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- Fixes batch url computation

## [1.6.0] - 2023-10-20

### Added
Expand Down
7 changes: 5 additions & 2 deletions sekoia_automation/connector/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from concurrent.futures import wait as wait_futures
from datetime import datetime, time
from functools import cached_property
from os.path import join as urljoin
from typing import Any
from urllib.parse import urljoin

import orjson
import requests
Expand All @@ -16,8 +16,10 @@
from requests import Response
from tenacity import (
Retrying,
stop_after_attempt,
stop_after_delay,
wait_exponential,
wait_none,
)

from sekoia_automation.constants import CHUNK_BYTES_MAX_SIZE, EVENT_BYTES_MAX_SIZE
Expand Down Expand Up @@ -91,6 +93,7 @@ def stop(self, *args, **kwargs):
self._executor.shutdown(wait=True)

def _retry(self):
return Retrying(wait=wait_none, stop=stop_after_attempt(1))
return Retrying(
stop=stop_after_delay(3600), # 1 hour without being able to send events
wait=wait_exponential(multiplier=1, min=1, max=10),
Expand Down Expand Up @@ -150,7 +153,7 @@ def push_events_to_intakes(
self._error_count = 0
self._last_events_time = datetime.utcnow()
intake_host = os.getenv("INTAKE_URL", self.configuration.intake_server)
batch_api = urljoin(intake_host, "/batch")
batch_api = urljoin(intake_host, "batch")

# Dict to collect event_ids for the API
collect_ids: dict[int, list] = {}
Expand Down
22 changes: 22 additions & 0 deletions tests/connectors/test_connector.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
from unittest.mock import Mock, PropertyMock, patch

import pytest
Expand Down Expand Up @@ -124,6 +125,27 @@ def test_push_event_to_intake_with_chunks(test_connector, mocked_trigger_logs):
assert result == ["001", "002", "003", "004"]


def test_push_event_to_intake_custom_url(test_connector, mocked_trigger_logs):
url = "https://fra2.app.sekoia.io/v1/intake-http/batch"
batch_mock = mocked_trigger_logs.post(
url, json={"event_ids": ["001"]}, additional_matcher=match_events("foo")
)
# With trailing slash
with patch.dict(
os.environ, {"INTAKE_URL": "https://fra2.app.sekoia.io/v1/intake-http/"}
):
test_connector.push_events_to_intakes(["foo"])
assert batch_mock.call_count == 1

# Without trailing slash
mocked_trigger_logs.reset_mock()
with patch.dict(
os.environ, {"INTAKE_URL": "https://fra2.app.sekoia.io/v1/intake-http"}
):
test_connector.push_events_to_intakes(["foo"])
assert batch_mock.call_count == 1


def test_push_event_to_intake_with_chunks_executor_stopped(
test_connector, mocked_trigger_logs
):
Expand Down

0 comments on commit cf2674d

Please sign in to comment.