-
Notifications
You must be signed in to change notification settings - Fork 2
Add support to collapse JavaStackTrace into a single event #39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| name: Test Package | ||
|
|
||
| on: | ||
| pull_request: | ||
| branches: [ "main" ] | ||
|
|
||
| permissions: | ||
| id-token: write | ||
| contents: write | ||
| packages: write | ||
| deployments: write | ||
|
|
||
| jobs: | ||
| build: | ||
| runs-on: ubuntu-latest | ||
|
|
||
| steps: | ||
| - name: Checkout | ||
| uses: actions/checkout@v4 | ||
|
|
||
| - name: Set up environment | ||
| uses: actions/setup-python@v5 | ||
| with: | ||
| python-version: '3.12' | ||
|
|
||
| - name: Test | ||
| run: ./test.sh |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| from typing import Union | ||
| from queue import Queue | ||
|
|
||
| AGGREGATED_TERMINATION_MARKER = None | ||
|
|
||
|
|
||
| class Aggregator: | ||
|
|
||
| _NO_AGGREGATED_LINE = None | ||
|
|
||
| def add_line(self, line: Union[str, None]): | ||
| raise NotImplementedError() | ||
|
|
||
| def get_complete_aggregated_line(self): | ||
| raise NotImplementedError() | ||
|
|
||
| def complete(self): | ||
| self.add_line(AGGREGATED_TERMINATION_MARKER) | ||
|
|
||
| def has_complete_aggregated_line(self): | ||
| raise NotImplementedError() | ||
|
|
||
|
|
||
| class NoopAggregator(Aggregator): | ||
|
|
||
| def __init__(self): | ||
| self.lines = Queue(maxsize=2) | ||
|
|
||
| def add_line(self, line: Union[str, None]): | ||
| if self.has_complete_aggregated_line(): | ||
| raise Exception('Complete aggregated lines must be retrieved first') | ||
| if line is not None and line == '': | ||
| return Aggregator._NO_AGGREGATED_LINE | ||
| self.lines.put(line) | ||
| return | ||
|
|
||
| def get_complete_aggregated_line(self): | ||
| if self.has_complete_aggregated_line(): | ||
| return self.lines.get() | ||
| return Aggregator._NO_AGGREGATED_LINE | ||
|
|
||
| def has_complete_aggregated_line(self): | ||
| return self.lines.full() | ||
|
|
||
|
|
||
| class JavaStackTraceAggregator(Aggregator): | ||
| """ Inspired from https://www.elastic.co/docs/reference/beats/filebeat/multiline-examples#_java_stack_traces """ | ||
|
|
||
| def __init__(self): | ||
| self.lines = Queue(maxsize=2) | ||
|
|
||
| def add_line(self, line): | ||
| if self.lines.full(): | ||
| raise Exception('Aggregator queue is full') | ||
| if line is None: | ||
| current_line = line | ||
| else: | ||
| stripped_line = line.lstrip() | ||
| if stripped_line == '': | ||
| return | ||
| if (stripped_line.startswith('at ') or stripped_line.startswith('Caused by: ') or | ||
| stripped_line.startswith('...')): | ||
| current_line = self.lines.get().rstrip('\n') | ||
| current_line += f'\\n{line}' | ||
| else: | ||
| current_line = line | ||
| self.lines.put(current_line) | ||
| return | ||
|
|
||
| def get_complete_aggregated_line(self): | ||
| if self.has_complete_aggregated_line(): | ||
| return self.lines.get() | ||
| return Aggregator._NO_AGGREGATED_LINE | ||
|
|
||
| def has_complete_aggregated_line(self): | ||
| return self.lines.full() | ||
|
|
||
|
|
||
| class AggregatorFactory: | ||
|
|
||
| @staticmethod | ||
| def get_aggregator(aggregator_name) -> Aggregator: | ||
| if aggregator_name == 'java_stack_trace': | ||
| return JavaStackTraceAggregator() | ||
| return NoopAggregator() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| from typing import Dict, List | ||
| from aggregator import Aggregator | ||
| from clients import BrontoClient, Batch | ||
| from parser import Parser | ||
|
|
||
|
|
||
| class BrontoExporter: | ||
|
|
||
| def __init__(self, client: BrontoClient, parser: Parser, batch: Batch, aggregator: Aggregator, | ||
| attributes: Dict[str, str]): | ||
| self.client = client | ||
| self.parser = parser | ||
| self.batch = batch | ||
| self.aggregator = aggregator | ||
| self.attributes = attributes | ||
|
|
||
| def export(self): | ||
| for line in self.parser.get_parsed_lines(): | ||
| self.aggregator.add_line(line) | ||
| if not self.aggregator.has_complete_aggregated_line(): | ||
| continue | ||
| _line = self.aggregator.get_complete_aggregated_line() | ||
| self.batch.add(_line) | ||
| if self.batch.get_batch_size() > self.batch.max_size: | ||
| self.client.send_data(self.batch, self.attributes) | ||
| self.batch.reset() | ||
| self.aggregator.complete() | ||
| if self.aggregator.has_complete_aggregated_line(): | ||
| _line = self.aggregator.get_complete_aggregated_line() | ||
| self.batch.add(_line) | ||
| if self.batch.get_batch_size() > 0: | ||
| self.client.send_data(self.batch, self.attributes) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| set -e | ||
|
|
||
| PROJECT_ROOT_DIR="${PWD}" | ||
| TEST_REQUIREMENT_FILE="${PROJECT_ROOT_DIR}/test_requirements.txt" | ||
|
|
||
| rm -rf test_env | ||
| python3 -m venv test_env | ||
| "${PROJECT_ROOT_DIR}/test_env/bin/python" -m pip install --upgrade pip | ||
| test_env/bin/pip install -r test_requirements.txt | ||
|
|
||
| cd "${PROJECT_ROOT_DIR}/tests" || exit 1 | ||
|
|
||
| echo "Running Tests" | ||
| PYTHONPATH="${PROJECT_ROOT_DIR}/log_forwarder" "${PROJECT_ROOT_DIR}/test_env/bin/python" -m pytest -v -s |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| pytest | ||
| boto3 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this seems small? im presuming this is the number of lines in the stacktrace?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I need to rename or introduce an intermediary class as I agree that the current implementation doesn't help understand the intent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed the Aggregator class API. We can probably do better still but at least it should be clearer now. The bottom line is that we keep a line in the aggregator that is not ready to be consumed until we know that the aggregation is completed. In general, the aggregation for a line is complete if we the next doesn't look like a stacktrace one. We also need to
completethe aggregation for the last line encountered, as there is no next one in that case.