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

[dogshell] add hostname by default to event/metric posts #122

Merged
merged 2 commits into from
Mar 10, 2016
Merged
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
4 changes: 2 additions & 2 deletions datadog/api/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ def request(cls, method, path, body=None, attach_host_name=False, response_forma
if 'series' in body:
# Adding the host name to all objects
for obj_params in body['series']:
if 'host' not in obj_params:
if obj_params.get('host', "") == "":
obj_params['host'] = _host_name
else:
if 'host' not in body:
if body.get('host', "") == "":
body['host'] = _host_name

# If defined, make sure tags are defined as a comma-separated string
Expand Down
14 changes: 0 additions & 14 deletions datadog/dogshell/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import os
import sys
import logging
import socket

# datadog
from datadog.util.compat import is_p3k, configparser, IterableUserDict,\
Expand Down Expand Up @@ -35,19 +34,6 @@ def report_warnings(res):
return False


memoized_hostname = None


def find_localhost():
try:
global memoized_hostname
if memoized_hostname is None:
memoized_hostname = socket.getfqdn()
return memoized_hostname
except Exception:
logging.exception("Cannot determine local hostname")


class DogshellConfig(IterableUserDict):

def load(self, config_file, api_key, app_key):
Expand Down
16 changes: 14 additions & 2 deletions datadog/dogshell/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ def setup_parser(cls, subparsers):
post_parser.add_argument('--related_event_id', help="event to post as a child of."
" if unset, posts a top-level event")
post_parser.add_argument('--tags', help="comma separated list of tags")
post_parser.add_argument('--host', help="related host")
post_parser.add_argument('--host', help="related host (default to the local host name)",
default="")
post_parser.add_argument('--no_host', help="no host is associated with the event"
" (overrides --host))", action='store_true')
post_parser.add_argument('--device', help="related device (e.g. eth0, /dev/sda1)")
post_parser.add_argument('--aggregation_key', help="key to aggregate the event with")
post_parser.add_argument('--type', help="type of event, e.g. nagios, jenkins, etc.")
Expand Down Expand Up @@ -113,6 +116,9 @@ def setup_parser(cls, subparsers):

@classmethod
def _post(cls, args):
"""
Post an event.
"""
api._timeout = args.timeout
format = args.format
message = args.message
Expand All @@ -122,14 +128,20 @@ def _post(cls, args):
tags = [t.strip() for t in args.tags.split(',')]
else:
tags = None

host = None if args.no_host else args.host

# Submit event
res = api.Event.create(
title=args.title, text=message,
# TODO FXIME
# date_happened=args.date_happened,
handle=args.handle, priority=args.priority,
related_event_id=args.related_event_id, tags=tags, host=args.host,
related_event_id=args.related_event_id, tags=tags, host=host,
device=args.device, aggregation_key=args.aggregation_key,
source_type_name=args.type, alert_type=args.alert_type)

# Report
report_warnings(res)
report_errors(res)
if format == 'pretty':
Expand Down
40 changes: 31 additions & 9 deletions datadog/dogshell/metric.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# stdlib
from collections import defaultdict

# datadog
from datadog import api
from datadog.dogshell.common import report_errors, report_warnings, find_localhost
from datadog.dogshell.common import report_errors, report_warnings


class MetricClient(object):
Expand All @@ -14,31 +17,50 @@ def setup_parser(cls, subparsers):
post_parser.add_argument('name', help="metric name")
post_parser.add_argument('value', help="metric value (integer or decimal value)",
type=float)
post_parser.add_argument('--host', help="scopes your metric to a specific host",
default=None)
post_parser.add_argument('--host', help="scopes your metric to a specific host "
"(default to the local host name)",
default="")
post_parser.add_argument('--no_host', help="no host is associated with the metric"
" (overrides --host))", action='store_true')
post_parser.add_argument('--device', help="scopes your metric to a specific device",
default=None)
post_parser.add_argument('--tags', help="comma-separated list of tags", default=None)
post_parser.add_argument('--localhostname', help="same as --host=`hostname`"
" (overrides --host)", action='store_true')
post_parser.add_argument('--localhostname', help="deprecated, used to force `--host`"
" to the local hostname "
"(now default when no `--host` is specified)", action='store_true')
post_parser.add_argument('--type', help="type of the metric - gauge(32bit float)"
" or counter(64bit integer)", default=None)
parser.set_defaults(func=cls._post)

@classmethod
def _post(cls, args):
"""
Post a metric.
"""
# Format parameters
api._timeout = args.timeout
if args.localhostname:
host = find_localhost()
else:
host = args.host

host = None if args.no_host else args.host

if args.tags:
tags = sorted(set([t.strip() for t in
args.tags.split(',') if t]))
else:
tags = None

# Submit metric
res = api.Metric.send(
metric=args.name, points=args.value, host=host,
device=args.device, tags=tags, metric_type=args.type)

# Report
res = defaultdict(list, res)

if args.localhostname:
# Warn about`--localhostname` command line flag deprecation
res['warnings'].append(
u"`--localhostname` command line flag is deprecated, made default when no `--host` "
u"is specified. See the `--host` option for more information."
)
report_warnings(res)
report_errors(res)