Skip to main content
 首页 » 编程设计

python之Datadog dogstream 自定义解析器错误

2024年10月25日16TianFang

我想通过 dogstream 添加我的自定义日志解析器,但是在重新启动 datadog 代理时出现异常:

2015-04-29 19:19:06 MSK | INFO | dd.collector | checks.collector(datadog.py:117) | Instantiating function-based dogstream 
2015-04-29 19:19:06 MSK | INFO | dd.collector | checks.collector(datadog.py:124) | dogstream: parsing /var/www/api.clearspending.ru_v3/api/var/log/tornadoCS9031.log with <function parse_api_response_time at 0x7f9d0981a398> (requested dogstream/clearspending_parser:parse_api_response_time) 
2015-04-29 19:19:06 MSK | INFO | dd.collector | checks.collector(datadog.py:67) | Dogstream parsers: [<checks.datadog.Dogstream object at 0x7f9d09820410>] 
2015-04-29 19:19:07 MSK | INFO | dd.collector | checks.collector(collector.py:486) | Hostnames: {'socket-hostname': 'clearspending.ru', 'hostname': 'clearspending.ru', 'socket-fqdn': 'clearspending.ru'}, tags: {} 
2015-04-29 19:19:07 MSK | ERROR | dd.collector | checks.collector(unix.py:370) | Cannot extract IO statistics 
Traceback (most recent call last): 
  File "/opt/datadog-agent/agent/checks/system/unix.py", line 284, in check 
    io.update(self._parse_linux2(stdout)) 
  File "/opt/datadog-agent/agent/checks/system/unix.py", line 185, in _parse_linux2 
    recentStats = output.split('Device:')[2].split('\n') 
IndexError: list index out of range 

解析器代码:

def parse_api_response_time(logger, line): 
    """ 
    Just parser API log :) 
    parse_api_response_time(None, "16-02-2015 15:46:36     INFO     200 GET /apishechka/v3/contracts/select/?customerregion=29&get_report=True&sort=-price (127.0.0.1) 28102.86ms") 
    ('api_time', 1424090796.0, 28102.86, {}) 
    """ 
    metric_name = "api_time" 
    date, status, time_response = re.match(API_LOG_REGEX, line).groups() 
    date = mktime(datetime.strptime(date, "%d-%m-%Y %H:%M:%S").timetuple()) 
    attr_dict = {} 
    # Convert the time_response value into a float 
    metric_value = float(time_response[:-2]) 
    # Return the output as a tuple 
    return (metric_name, date, metric_value, attr_dict) 

有人知道为什么会这样吗?有什么想法吗?

请您参考如下方法:

您看到的异常与 IO 系统指标收集有关,与您的自定义 dogstream 解析器无关。

如果您查看堆栈跟踪,它表明它无法应用 _parse_linux2 函数。要进一步排除故障,您应该查看

的输出
/opt/datadog-agent/embedded/bin/iostat -d 1 2 -x -k 

这是代理启动的命令。请随时在代理 GitHub 存储库上提出错误。

引用资料: