Skip to content

Commit

Permalink
CentryCore logging support
Browse files Browse the repository at this point in the history
  • Loading branch information
LifeDJIK committed Jan 25, 2024
1 parent e7f506a commit 236e874
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
Empty file.
86 changes: 86 additions & 0 deletions dusty/reporters/centry_logs/reporter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#!/usr/bin/python3
# coding=utf-8
# pylint: disable=I0011,R0902,E0401

# Copyright 2024 getcarrier.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.

"""
Reporter: CentryCore logging support
"""

import logging
import pkg_resources

from centry_logging.handlers.eventnode import EventNodeLogHandler # pylint: disable=E0401

from dusty.tools import log
from dusty.models.module import DependentModuleModel
from dusty.models.reporter import ReporterModel


class Reporter(DependentModuleModel, ReporterModel):
""" Log to CentryCore logging hub """

def __init__(self, context):
""" Initialize reporter instance """
super().__init__()
self.context = context
self.config = \
self.context.config["reporters"][__name__.split(".")[-2]]
self._enable_loki_logging()

def _enable_loki_logging(self):
handler = EventNodeLogHandler(self.config)
handler.setFormatter(log.filter_formatters[0])
#
logging.getLogger("").addHandler(handler)
log.info(
"Enabled Centry logging for Dusty %s",
pkg_resources.require("dusty")[0].version
)

def flush(self):
""" Flush """
for handler in logging.getLogger("").handlers:
handler.flush()

@staticmethod
def fill_config(data_obj):
""" Make sample config """

@staticmethod
def validate_config(config):
""" Validate config """
required = ["event_node", "labels"]
not_set = [item for item in required if item not in config]
if not_set:
error = f"Required configuration options not set: {', '.join(not_set)}"
log.error(error)
raise ValueError(error)

@staticmethod
def run_after():
""" Return optional depencies """
return []

@staticmethod
def get_name():
""" Reporter name """
return "Centry Logs"

@staticmethod
def get_description():
""" Reporter description """
return "Logs reporter for Centry"
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ packaging==20.4
# Reporting
#

# Centry
git+https://github.com/centry-core/centry_logging.git

# Loki
python-logging-loki==0.1.0

Expand Down

0 comments on commit 236e874

Please sign in to comment.