Skip to content

Commit

Permalink
fixes for py.test failures on travis
Browse files Browse the repository at this point in the history
  • Loading branch information
jay-johnson committed Jul 2, 2018
1 parent 1d3b865 commit 0860ae2
Show file tree
Hide file tree
Showing 15 changed files with 337 additions and 149 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ script:
- echo "Running Lint Tests"
- flake8
- pycodestyle
- echo "Running Unit Tests"
- py.test
- echo "Running Tox"
- tox
78 changes: 70 additions & 8 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,52 @@ Below is a video showing how to tag your application's logs using the ``LOG_NAME

.. raw:: html

<a href="https://asciinema.org/a/189634?autoplay=1" target="_blank"><img src="https://asciinema.org/a/189634.png"/></a>
<a href="https://asciinema.org/a/189711?autoplay=1" target="_blank"><img src="https://asciinema.org/a/189711.png"/></a>

Commands from the video:

#. Set an Application Log Name

::

export LOG_NAME=payments

#. Search for Logs in Splunk

::

sp -q 'index="antinex" AND name=payments | head 5 | reverse'
No matches for search={
"search": "search index=\"antinex\" AND name=payments | head 5 | reverse"
} response={
"init_offset": 0,
"messages": [],
"post_process_count": 0,
"preview": false,
"results": []
}

#. Send Test Logs to Splunk

::

test_logging.py
2018-07-02 09:18:22,197 - helloworld - INFO - testing INFO message_id=93e33f10-ebbf-49a1-a87a-a76858448c71
2018-07-02 09:18:22,199 - helloworld - ERROR - testing ERROR message_id=3b3f0362-f146-47b4-9fff-c6cc3b165279
2018-07-02 09:18:22,200 - helloworld - CRITICAL - testing CRITICAL message_id=8870f39e-82b5-4071-b19a-80ce6cfefbd6
2018-07-02 09:18:22,201 - helloworld - WARNING - testing WARNING message_id=6ab745cb-8a14-41ae-b16e-13c0c80c4963
2018-07-02 09:18:22,201 - helloworld - ERROR - Testing EXCEPTION with ex=Throw for testing exceptions message_id=26b3c421-46b7-49d2-960b-1ca2ed7b8e03

#. Search for Test Logs in Splunk

::

sp -q 'index="antinex" AND name=payments | head 5 | reverse'
2018-07-02 09:18:22,197 helloworld - INFO - testing INFO message_id=93e33f10-ebbf-49a1-a87a-a76858448c71
2018-07-02 09:18:22,199 helloworld - ERROR - testing ERROR message_id=3b3f0362-f146-47b4-9fff-c6cc3b165279
2018-07-02 09:18:22,200 helloworld - CRITICAL - testing CRITICAL message_id=8870f39e-82b5-4071-b19a-80ce6cfefbd6
2018-07-02 09:18:22,201 helloworld - WARNING - testing WARNING message_id=6ab745cb-8a14-41ae-b16e-13c0c80c4963
2018-07-02 09:18:22,201 helloworld - ERROR - Testing EXCEPTION with ex=Throw for testing exceptions message_id=26b3c421-46b7-49d2-960b-1ca2ed7b8e03

Get Splunk Logs from the Command Line Tool
------------------------------------------
Expand Down Expand Up @@ -576,20 +621,27 @@ And you can view log the full JSON dictionaries using the ``-j`` argument on the
}
done

Debug the Logger
----------------
Available Environment Variables
-------------------------------

Drill down fields
=================

Export this variable before creating a logger.
Splunk drill down fields with environment variables:

::

export SPLUNK_DEBUG=1
export LOG_NAME="<application log name>"
export DEPLOY_CONFIG="<application deployed config like k8 filename>"
export ENV_NAME="<environment name for this application>"

Available Environment Variables
-------------------------------
Common Environment Variables
============================

::

export SPLUNK_USER="<splunk host>"
export SPLUNK_PASSWORD="<splunk host>"
export SPLUNK_HOST="<splunk host>"
export SPLUNK_PORT="<splunk port: 8088>"
export SPLUNK_API_PORT="<splunk port: 8089>"
Expand All @@ -605,7 +657,17 @@ Available Environment Variables
export SPLUNK_SLEEP_INTERVAL="<sleep in seconds per batch>"
export SPLUNK_RETRY_COUNT="<attempts per log to retry publishing>"
export SPLUNK_RETRY_BACKOFF="<cooldown in seconds per failed POST>"
export SPLUNK_DEBUG="<1 enable debug|0 off>"
export SPLUNK_DEBUG="<debug the publisher - 1 enable debug|0 off>"
export SPLUNK_VERBOSE="<debug the sp command line tool - 1 enable|0 off>"

Debug the Publishers
====================

Export this variable before creating a logger to see the publisher logs:

::

export SPLUNK_DEBUG=1

Login to Splunk from a Browser
------------------------------
Expand Down
4 changes: 2 additions & 2 deletions docs/source/api_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -214,15 +214,15 @@ The ``build_colorized_logger`` calls the ``setup_logging`` method that builds th
Using Threads to Publish to Splunk
----------------------------------

The Splunk Publisher handles sending logs to the configured Splunk server. It was originally inspired from https://github.com/zach-taylor/splunk_handler but after encountering issues within Celery tasks this class was created to maintain a stable logger from inside a Celery task.
Here is the code for the Splunk Publisher that uses a thread to send logs to the configured Splunk server.

.. automodule:: spylunking.splunk_publisher
:members: SplunkPublisher

Using Multiprocesing to Publish to Splunk
-----------------------------------------

The Splunk Publisher handles sending logs to the configured Splunk server. It was originally inspired from https://github.com/zach-taylor/splunk_handler but this one uses multiiprocessing instead of threads. This approach will not work in Celery tasks.
Here is the code for the Splunk Publisher that uses a multiprocessing to send logs to the configured Splunk server.

.. automodule:: spylunking.mp_splunk_publisher
:members: MPSplunkPublisher
Expand Down
78 changes: 70 additions & 8 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,52 @@ Below is a video showing how to tag your application's logs using the ``LOG_NAME

.. raw:: html

<a href="https://asciinema.org/a/189634?autoplay=1" target="_blank"><img src="https://asciinema.org/a/189634.png"/></a>
<a href="https://asciinema.org/a/189711?autoplay=1" target="_blank"><img src="https://asciinema.org/a/189711.png"/></a>

Commands from the video:

#. Set an Application Log Name

::

export LOG_NAME=payments

#. Search for Logs in Splunk

::

sp -q 'index="antinex" AND name=payments | head 5 | reverse'
No matches for search={
"search": "search index=\"antinex\" AND name=payments | head 5 | reverse"
} response={
"init_offset": 0,
"messages": [],
"post_process_count": 0,
"preview": false,
"results": []
}

#. Send Test Logs to Splunk

::

test_logging.py
2018-07-02 09:18:22,197 - helloworld - INFO - testing INFO message_id=93e33f10-ebbf-49a1-a87a-a76858448c71
2018-07-02 09:18:22,199 - helloworld - ERROR - testing ERROR message_id=3b3f0362-f146-47b4-9fff-c6cc3b165279
2018-07-02 09:18:22,200 - helloworld - CRITICAL - testing CRITICAL message_id=8870f39e-82b5-4071-b19a-80ce6cfefbd6
2018-07-02 09:18:22,201 - helloworld - WARNING - testing WARNING message_id=6ab745cb-8a14-41ae-b16e-13c0c80c4963
2018-07-02 09:18:22,201 - helloworld - ERROR - Testing EXCEPTION with ex=Throw for testing exceptions message_id=26b3c421-46b7-49d2-960b-1ca2ed7b8e03

#. Search for Test Logs in Splunk

::

sp -q 'index="antinex" AND name=payments | head 5 | reverse'
2018-07-02 09:18:22,197 helloworld - INFO - testing INFO message_id=93e33f10-ebbf-49a1-a87a-a76858448c71
2018-07-02 09:18:22,199 helloworld - ERROR - testing ERROR message_id=3b3f0362-f146-47b4-9fff-c6cc3b165279
2018-07-02 09:18:22,200 helloworld - CRITICAL - testing CRITICAL message_id=8870f39e-82b5-4071-b19a-80ce6cfefbd6
2018-07-02 09:18:22,201 helloworld - WARNING - testing WARNING message_id=6ab745cb-8a14-41ae-b16e-13c0c80c4963
2018-07-02 09:18:22,201 helloworld - ERROR - Testing EXCEPTION with ex=Throw for testing exceptions message_id=26b3c421-46b7-49d2-960b-1ca2ed7b8e03

Get Splunk Logs from the Command Line Tool
------------------------------------------
Expand Down Expand Up @@ -581,20 +626,27 @@ And you can view log the full JSON dictionaries using the ``-j`` argument on the
}
done

Debug the Logger
----------------
Available Environment Variables
-------------------------------

Drill down fields
=================

Export this variable before creating a logger.
Splunk drill down fields with environment variables:

::

export SPLUNK_DEBUG=1
export LOG_NAME="<application log name>"
export DEPLOY_CONFIG="<application deployed config like k8 filename>"
export ENV_NAME="<environment name for this application>"

Available Environment Variables
-------------------------------
Common Environment Variables
============================

::

export SPLUNK_USER="<splunk host>"
export SPLUNK_PASSWORD="<splunk host>"
export SPLUNK_HOST="<splunk host>"
export SPLUNK_PORT="<splunk port: 8088>"
export SPLUNK_API_PORT="<splunk port: 8089>"
Expand All @@ -610,7 +662,17 @@ Available Environment Variables
export SPLUNK_SLEEP_INTERVAL="<sleep in seconds per batch>"
export SPLUNK_RETRY_COUNT="<attempts per log to retry publishing>"
export SPLUNK_RETRY_BACKOFF="<cooldown in seconds per failed POST>"
export SPLUNK_DEBUG="<1 enable debug|0 off>"
export SPLUNK_DEBUG="<debug the publisher - 1 enable debug|0 off>"
export SPLUNK_VERBOSE="<debug the sp command line tool - 1 enable|0 off>"

Debug the Publishers
====================

Export this variable before creating a logger to see the publisher logs:

::

export SPLUNK_DEBUG=1

Login to Splunk from a Browser
------------------------------
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def handle_exit():
setup(
name='spylunking',
cmdclass={'test': PyTest},
version='1.0.28',
version='1.0.29',
description=(
'Spylunking - Drill down into your logs with an integrated, '
'colorized logger and search tools. Includes a Splunk sandbox '
Expand Down
82 changes: 52 additions & 30 deletions spylunking/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,19 @@
NOT_DONE = 6


def get_status(
status):
"""get_status
Return the string label for an integer status code
which should be one of the ones above.
:param status: integer status code
"""
if status == SUCCESS:
return 'SUCCESS'
elif status == FAILED:
return 'FAILED'
elif status == ERR:
return 'ERR'
elif status == EX:
return 'EX'
elif status == NOT_RUN:
return 'NOT_RUN'
elif status == INVALID:
return 'INVALID'
elif status == NOT_DONE:
return 'NOT_DONE'
else:
return 'unsupported status={}'.format(
status)
# end of get_status


LOG_HANDLER_NAME = os.getenv(
'LOG_HANDLER_NAME',
'console').strip()
SPLUNK_USER = os.getenv(
'SPLUNK_USER',
None)
if SPLUNK_USER:
SPLUNK_USER = SPLUNK_USER.strip()
SPLUNK_PASSWORD = os.getenv(
'SPLUNK_PASSWORD',
None)
if SPLUNK_PASSWORD:
SPLUNK_PASSWORD = SPLUNK_PASSWORD.strip()
SPLUNK_HOST = os.getenv(
'SPLUNK_HOST',
'splunkenterprise').strip()
Expand Down Expand Up @@ -87,6 +67,9 @@ def get_status(
SPLUNK_VERIFY = bool(os.getenv(
'SPLUNK_VERIFY',
'0').strip() == '1')
SPLUNK_VERBOSE = bool(os.getenv(
'SPLUNK_VERBOSE',
'0').strip() == '1')
SPLUNK_TIMEOUT = float(os.getenv(
'SPLUNK_TIMEOUT',
'10.0').strip())
Expand All @@ -112,3 +95,42 @@ def get_status(
SPLUNK_HANDLER_NAME = os.getenv(
'SPLUNK_HANDLER_NAME',
'splunk').strip()
SPLUNK_LOG_NAME = os.getenv(
'LOG_NAME',
'').strip()
SPLUNK_DEPLOY_CONFIG = os.getenv(
'DEPLOY_CONFIG',
'').strip()
SPLUNK_ENV_NAME = os.getenv(
'ENV_NAME',
'').strip()


def get_status(
status):
"""get_status
Return the string label for an integer status code
which should be one of the ones above.
:param status: integer status code
"""
if status == SUCCESS:
return 'SUCCESS'
elif status == FAILED:
return 'FAILED'
elif status == ERR:
return 'ERR'
elif status == EX:
return 'EX'
elif status == NOT_RUN:
return 'NOT_RUN'
elif status == INVALID:
return 'INVALID'
elif status == NOT_DONE:
return 'NOT_DONE'
else:
return 'unsupported status={}'.format(
status)
# end of get_status
Loading

0 comments on commit 0860ae2

Please sign in to comment.