Skip to content
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

Adding dogstream filename globbing support. Fixes #753. #1550

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 47 additions & 26 deletions checks/datadog.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# stdlib
import glob
import os
import sys
import traceback
Expand Down Expand Up @@ -36,33 +37,10 @@ class Dogstreams(object):
@classmethod
def init(cls, logger, config):
dogstreams_config = config.get('dogstreams', None)
dogstreams = []
if dogstreams_config:
# Expecting dogstreams config value to look like:
# <dogstream value>, <dog stream value>, ...
# Where <dogstream value> looks like:
# <log path>
# or
# <log path>:<module>:<parser function>

# Create a Dogstream object for each <dogstream value>
for config_item in dogstreams_config.split(','):
try:
config_item = config_item.strip()
parts = windows_friendly_colon_split(config_item)
if len(parts) == 1:
dogstreams.append(Dogstream.init(logger, log_path=parts[0]))
elif len(parts) == 2:
logger.warn("Invalid dogstream: %s" % ':'.join(parts))
elif len(parts) >= 3:
dogstreams.append(Dogstream.init(
logger,
log_path=parts[0],
parser_spec=':'.join(parts[1:3]),
parser_args=parts[3:],
config=config))
except Exception:
logger.exception("Cannot build dogstream")
dogstreams = cls._instantiate_dogstreams(logger, config, dogstreams_config)
else:
dogstreams = []

logger.info("Dogstream parsers: %s" % repr(dogstreams))

Expand All @@ -72,6 +50,49 @@ def __init__(self, logger, dogstreams):
self.logger = logger
self.dogstreams = dogstreams

@classmethod
def _instantiate_dogstreams(cls, logger, config, dogstreams_config):
"""
Expecting dogstreams config value to look like:
<dogstream value>, <dog stream value>, ...
Where <dogstream value> looks like:
<log path>
or
<log path>:<module>:<parser function>
"""
dogstreams = []
# Create a Dogstream object for each <dogstream value>
for config_item in dogstreams_config.split(','):
try:
config_item = config_item.strip()
parts = windows_friendly_colon_split(config_item)
if len(parts) == 1:
# If the dogstream includes a wildcard, we'll add every
# matching path.
for path in cls._get_dogstream_log_paths(parts[0]):
dogstreams.append(Dogstream.init(logger, log_path=path))
elif len(parts) == 2:
logger.warn("Invalid dogstream: %s" % ':'.join(parts))
elif len(parts) >= 3:
dogstreams.append(Dogstream.init(
logger,
log_path=parts[0],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any particular reason why we are not applying the same logic for <log path>:<module>:<parser function> dogstream configs ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No specific reason, more so my unfamiliarity with the codebase.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the quick reply :) If you don't mind I'll rebase your PR on top of master and bring your logic to this part of the code too.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be awesome, Yann. Thanks!
On Jun 5, 2015 3:01 PM, "Yann" [email protected] wrote:

In checks/datadog.py
#1550 (comment):

  •    # Create a Dogstream object for each <dogstream value>
    
  •    for config_item in dogstreams_config.split(','):
    
  •        try:
    
  •            config_item = config_item.strip()
    
  •            parts = windows_friendly_colon_split(config_item)
    
  •            if len(parts) == 1:
    
  •                # If the dogstream includes a wildcard, we'll add every
    
  •                # matching path.
    
  •                for path in cls._get_dogstream_log_paths(parts[0]):
    
  •                    dogstreams.append(Dogstream.init(logger, log_path=path))
    
  •            elif len(parts) == 2:
    
  •                logger.warn("Invalid dogstream: %s" % ':'.join(parts))
    
  •            elif len(parts) >= 3:
    
  •                dogstreams.append(Dogstream.init(
    
  •                    logger,
    
  •                    log_path=parts[0],
    

Thanks for the quick reply :) If you don't mind I'll rebase your PR on top
of master and bring your logic to this part of the code too.


Reply to this email directly or view it on GitHub
https://github.com/DataDog/dd-agent/pull/1550/files#r31855564.

parser_spec=':'.join(parts[1:3]),
parser_args=parts[3:],
config=config))
except Exception:
logger.exception("Cannot build dogstream")
return dogstreams

@classmethod
def _get_dogstream_log_paths(cls, path):
"""
Paths may include wildcard *'s and ?'s.
"""
if '*' not in path:
return [path]
return glob.glob(path)

def check(self, agentConfig, move_end=True):
if not self.dogstreams:
return {}
Expand Down
21 changes: 20 additions & 1 deletion tests/test_datadog.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
import unittest
from tempfile import NamedTemporaryFile
from tempfile import NamedTemporaryFile, gettempdir
import re
import os

Expand Down Expand Up @@ -191,6 +191,25 @@ def test_dogstream_ancient_function_plugin(self):
plugdog = Dogstreams.init(self.logger, {'dogstreams': '%s:tests.test_datadog:parse_ancient_function_plugin' % self.log_file.name})
actual_output = plugdog.check(self.config, move_end=False)

def test_dogstream_log_path_globbing(self):
"""Make sure that globbed dogstream logfile matching works."""
# Create a tmpfile to serve as a prefix for the other temporary
# files we'll be globbing.
first_tmpfile = NamedTemporaryFile()
tmp_fprefix = os.path.basename(first_tmpfile.name)
all_tmp_filenames = set([first_tmpfile.name])
# We stick the file objects in here to avoid garbage collection (and
# tmpfile deletion). Not sure why this was happening, but it's working
# with this hack in.
avoid_gc = []
for i in range(3):
new_tmpfile = NamedTemporaryFile(prefix=tmp_fprefix)
all_tmp_filenames.add(new_tmpfile.name)
avoid_gc.append(new_tmpfile)
dogstream_glob = os.path.join(gettempdir(), tmp_fprefix + '*')
paths = Dogstreams._get_dogstream_log_paths(dogstream_glob)
self.assertEqual(set(paths), all_tmp_filenames)

def test_dogstream_function_plugin(self):
"""Ensure that non-class-based stateful plugins work"""
log_data = [
Expand Down