github.com/mackerelio/mackerel-agent-plugins@v0.89.3/mackerel-plugin-fluentd/lib/fluentd_test.go (about)

     1  package mpfluentd
     2  
     3  import (
     4  	"reflect"
     5  	"regexp"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestGraphDefinition(t *testing.T) {
    12  	var fluentd FluentdPlugin
    13  
    14  	graphdef := fluentd.GraphDefinition()
    15  	if len(graphdef) != 3 {
    16  		t.Errorf("GetTempfilename: %d should be 3", len(graphdef))
    17  	}
    18  }
    19  
    20  func TestGraphDefinitionExtended(t *testing.T) {
    21  	var fluentd FluentdPlugin
    22  	fluentd.extendedMetrics = []string{"emit_records", "emit_count", "xxx"}
    23  
    24  	graphdef := fluentd.GraphDefinition()
    25  	if len(graphdef) != 5 { // default 3 + emit_records + emit_count, xxx is ignored
    26  		t.Errorf("GetTempfilename: %d should be 5", len(graphdef))
    27  	}
    28  }
    29  
    30  func TestGraphDefinitionMultiWorkers(t *testing.T) {
    31  	var fluentd FluentdPlugin
    32  	fluentd.Workers = 2
    33  
    34  	graphdef := fluentd.GraphDefinition()
    35  	for key := range graphdef {
    36  		last := key[len(key)-1]
    37  		assert.EqualValues(t, last, "#")
    38  	}
    39  }
    40  
    41  func TestNormalizePluginID(t *testing.T) {
    42  	testSets := [][]string{
    43  		{"foo/bar", "foo_bar"},
    44  		{"foo:bar", "foo_bar"},
    45  	}
    46  
    47  	for _, testSet := range testSets {
    48  		if normalizePluginID(testSet[0]) != testSet[1] {
    49  			t.Errorf("normalizeMetricName: '%s' should be normalized to '%s', but '%s'", testSet[0], testSet[1], normalizePluginID(testSet[0]))
    50  		}
    51  	}
    52  }
    53  
    54  func TestParse(t *testing.T) {
    55  	var fluentd FluentdPlugin
    56  	stub := `{"plugins":[{"plugin_id":"object:3feb368cfad0","plugin_category":"output","type":"mackerel","config":{"type":"mackerel","api_key":"aaa","service":"foo","metrics_name":"${[1]}-bar.${out_key}","remove_prefix":"","out_keys":"Latency","localtime":true},"output_plugin":true,"buffer_queue_length":0,"buffer_total_queued_size":53,"retry_count":0},{"plugin_id":"object:155633c","plugin_category":"input","type":"monitor_agent","config":{"type":"monitor_agent","bind":"0.0.0.0","port":"24220"},"output_plugin":false,"retry_count":null}]}`
    57  
    58  	fluentdStats := []byte(stub)
    59  
    60  	stat, err := fluentd.parseStats(fluentdStats)
    61  	assert.Nil(t, err)
    62  	// Fluentd Stats
    63  	assert.EqualValues(t, reflect.TypeOf(stat["buffer_total_queued_size.object_3feb368cfad0"]).String(), "float64")
    64  	assert.EqualValues(t, stat["buffer_total_queued_size.object_3feb368cfad0"].(float64), 53)
    65  	if _, ok := stat["buffer_total_queued_size.object_155633c"]; ok {
    66  		t.Errorf("parseStats: stats of other than the output plugin should not exist")
    67  	}
    68  }
    69  
    70  func TestParseExtended(t *testing.T) {
    71  	var fluentd FluentdPlugin
    72  	fluentd.extendedMetrics = []string{"emit_records"}
    73  	stub := `{
    74  	  "plugins": [
    75  		{
    76  			"plugin_id": "object:3feb368cfad0",
    77  			"plugin_category": "output",
    78  			"type": "mackerel",
    79  			"config": {"type":"mackerel","api_key":"aaa","service":"foo","metrics_name":"${[1]}-bar.${out_key}","remove_prefix":"","out_keys":"Latency","localtime":true},
    80  			"output_plugin": true,
    81  			"buffer_queue_length": 0,
    82  			"buffer_total_queued_size": 53,
    83  			"retry_count": 0,
    84  			"emit_records": 10,
    85  			"emit_count": 7
    86  		},
    87  		{
    88  			"plugin_id": "object:155633c",
    89  			"plugin_category": "input",
    90  			"type": "monitor_agent",
    91  			"config":{"type":"monitor_agent","bind":"0.0.0.0","port":"24220"},
    92  			"output_plugin": false,
    93  			"retry_count": null
    94  		}
    95  	]}`
    96  
    97  	fluentdStats := []byte(stub)
    98  
    99  	stat, err := fluentd.parseStats(fluentdStats)
   100  	assert.Nil(t, err)
   101  	// Fluentd Stats
   102  	assert.EqualValues(t, reflect.TypeOf(stat["buffer_total_queued_size.object_3feb368cfad0"]).String(), "float64")
   103  	assert.EqualValues(t, stat["buffer_total_queued_size.object_3feb368cfad0"].(float64), 53)
   104  	if _, ok := stat["buffer_total_queued_size.object_155633c"]; ok {
   105  		t.Errorf("parseStats: stats of other than the output plugin should not exist")
   106  	}
   107  	assert.EqualValues(t, stat["emit_records.object_3feb368cfad0"].(float64), 10)
   108  	if _, ok := stat["emit_count.object_3feb368cfad0"]; ok {
   109  		t.Errorf("parseStats: stats of other than the output plugin should not exist")
   110  	}
   111  }
   112  
   113  func TestPluginTypeOption(t *testing.T) {
   114  
   115  	stub := `{"plugins":[{"plugin_id":"out_file","plugin_category":"output","type":"file","config":{"buffer_chunk_limit":"12m","compress":"gzip","type":"file","path":"/path/to/log","time_slice_format":"%Y%m%d%H","time_slice_wait":"1m","buffer_type":"file","buffer_path":"/path/to/buffer"},"output_plugin":true,"buffer_queue_length":0,"buffer_total_queued_size":10940,"retry_count":0},{"plugin_id":"out_mackerel","plugin_category":"output","type":"mackerel","config":{"type":"mackerel","api_key":"aaa","service":"foo","metrics_name":"${[1]}-bar.${out_key}","remove_prefix":"","out_keys":"Latency","localtime":true},"output_plugin":true,"buffer_queue_length":0,"buffer_total_queued_size":53,"retry_count":0}]}`
   116  	fluentdStats := []byte(stub)
   117  
   118  	// Specify type option
   119  	var fluentd = FluentdPlugin{pluginType: "mackerel"}
   120  	stat, err := fluentd.parseStats(fluentdStats)
   121  
   122  	assert.Nil(t, err)
   123  	assert.EqualValues(t, reflect.TypeOf(stat["buffer_total_queued_size.out_mackerel"]).String(), "float64")
   124  	assert.EqualValues(t, stat["buffer_total_queued_size.out_mackerel"].(float64), 53)
   125  	if _, ok := stat["buffer_total_queued_size.out_file"]; ok {
   126  		t.Errorf("parseStats: stats of plugin that do not match the specified type should not exist")
   127  	}
   128  
   129  	// Not specify type option
   130  	fluentd = FluentdPlugin{}
   131  	stat, err = fluentd.parseStats(fluentdStats)
   132  
   133  	assert.Nil(t, err)
   134  	assert.EqualValues(t, reflect.TypeOf(stat["buffer_total_queued_size.out_mackerel"]).String(), "float64")
   135  	assert.EqualValues(t, stat["buffer_total_queued_size.out_mackerel"].(float64), 53)
   136  	assert.EqualValues(t, reflect.TypeOf(stat["buffer_total_queued_size.out_file"]).String(), "float64")
   137  	assert.EqualValues(t, stat["buffer_total_queued_size.out_file"].(float64), 10940)
   138  }
   139  
   140  func TestPluginIDPatternOption(t *testing.T) {
   141  
   142  	stub := `{"plugins":[{"plugin_id":"match_plugin_id","plugin_category":"output","type":"file","config":{"buffer_chunk_limit":"12m","compress":"gzip","type":"file","path":"/path/to/log","time_slice_format":"%Y%m%d%H","time_slice_wait":"1m","buffer_type":"file","buffer_path":"/path/to/buffer"},"output_plugin":true,"buffer_queue_length":0,"buffer_total_queued_size":10940,"retry_count":0},{"plugin_id":"do_not_match_plugin_id","plugin_category":"output","type":"mackerel","config":{"type":"mackerel","api_key":"aaa","service":"foo","metrics_name":"${[1]}-bar.${out_key}","remove_prefix":"","out_keys":"Latency","localtime":true},"output_plugin":true,"buffer_queue_length":0,"buffer_total_queued_size":53,"retry_count":0}]}`
   143  	fluentdStats := []byte(stub)
   144  
   145  	// Specify type option
   146  	var fluentd = FluentdPlugin{
   147  		pluginIDPattern: regexp.MustCompile("^match"),
   148  	}
   149  	stat, err := fluentd.parseStats(fluentdStats)
   150  
   151  	assert.Nil(t, err)
   152  	assert.EqualValues(t, reflect.TypeOf(stat["buffer_total_queued_size.match_plugin_id"]).String(), "float64")
   153  	assert.EqualValues(t, stat["buffer_total_queued_size.match_plugin_id"].(float64), 10940)
   154  	if _, ok := stat["buffer_total_queued_size.do_not_match_plugin_id"]; ok {
   155  		t.Errorf("parseStats: stats of plugin that do not match the specified id pattern should not exist")
   156  	}
   157  
   158  	// Not specify type option
   159  	fluentd = FluentdPlugin{}
   160  	stat, err = fluentd.parseStats(fluentdStats)
   161  
   162  	assert.Nil(t, err)
   163  	assert.EqualValues(t, reflect.TypeOf(stat["buffer_total_queued_size.match_plugin_id"]).String(), "float64")
   164  	assert.EqualValues(t, stat["buffer_total_queued_size.match_plugin_id"].(float64), 10940)
   165  	assert.EqualValues(t, reflect.TypeOf(stat["buffer_total_queued_size.do_not_match_plugin_id"]).String(), "float64")
   166  	assert.EqualValues(t, stat["buffer_total_queued_size.do_not_match_plugin_id"].(float64), 53)
   167  }