[Groonga-commit] groonga/fluent-plugin-groonga-query-log at 055808b [master] Add time_format option (#5)

Back to archive index
Yasuhiro Horimoto null+****@clear*****
Fri Feb 8 10:36:08 JST 2019


Yasuhiro Horimoto	2019-02-08 10:36:08 +0900 (Fri, 08 Feb 2019)

  Revision: 055808be84b0cc7f3289f0aad0d10dafc74a1459
  https://github.com/groonga/fluent-plugin-groonga-query-log/commit/055808be84b0cc7f3289f0aad0d10dafc74a1459

  Message:
    Add time_format option (#5)
    
    Patch by Yasuhiro Horimoto. Thanks!!!

  Modified files:
    lib/fluent/plugin/filter_groonga_query_log.rb
    test/test_filter_groonga_query_log.rb

  Modified: lib/fluent/plugin/filter_groonga_query_log.rb (+18 -4)
===================================================================
--- lib/fluent/plugin/filter_groonga_query_log.rb    2019-02-06 15:25:50 +0900 (e8893a5)
+++ lib/fluent/plugin/filter_groonga_query_log.rb    2019-02-08 10:36:08 +0900 (23caedb)
@@ -29,6 +29,7 @@ module Fluent
     config_param :timezone,                 :enum,
                                             :list => [:utc, :localtime],
                                             :default => :utc
+    config_param :time_format,              :string, :default => "iso8601"
 
     def configure(conf)
       super
@@ -37,6 +38,7 @@ module Fluent
         :slow_operation_threshold => @slow_operation_threshold,
         :slow_response_threshold => @slow_response_threshold,
         :timezone => @timezone,
+        :time_format => @time_format,
       }
       @parser = GroongaQueryLog::Parser.new(options)
     end
@@ -66,11 +68,23 @@ module Fluent
     end
 
     def format_time(time)
-      case @timezone
-      when :localtime
-        time.iso8601(6)
+      time.utc if @timezone == :utc
+      time.strftime(resolve_time_format)
+    end
+
+    def resolve_time_format
+      case @time_format
+      when "iso8601"
+        format = "%Y-%m-%dT%H:%M:%S.%6N"
+        if @timezone == :utc
+          format << "Z"
+        else
+          format << "%:z"
+        end
+      when "sql"
+        format = "%Y-%m-%d %H:%M:%S.%6N"
       else
-        time.utc.iso8601(6)
+        @time_format
       end
     end
 

  Modified: test/test_filter_groonga_query_log.rb (+252 -42)
===================================================================
--- test/test_filter_groonga_query_log.rb    2019-02-06 15:25:50 +0900 (e09d3d9)
+++ test/test_filter_groonga_query_log.rb    2019-02-08 10:36:08 +0900 (17f5dbd)
@@ -41,6 +41,7 @@ class GroongaQueryLogFilterTest < Test::Unit::TestCase
                      :flatten                  => false,
                      :flatten_separator        => nil,
                      :timezone                 => :utc,
+                     :time_format              => "iso8601",
                    },
                    {
                      :raw_data_column_name     => filter.raw_data_column_name,
@@ -49,6 +50,7 @@ class GroongaQueryLogFilterTest < Test::Unit::TestCase
                      :flatten                  => filter.flatten,
                      :flatten_separator        => filter.flatten_separator,
                      :timezone                 => filter.timezone,
+                     :time_format              => filter.time_format,
                    })
     end
 
@@ -87,6 +89,12 @@ class GroongaQueryLogFilterTest < Test::Unit::TestCase
       filter = driver.instance
       assert_equal(:localtime, filter.timezone)
     end
+
+    test "time_format" do
+      driver = create_driver("time_format %Y-%m-%d %H:%M:%S.%6N")
+      filter = driver.instance
+      assert_equal("%Y-%m-%d %H:%M:%S.%6N", filter.time_format)
+    end
   end
 
   sub_test_case "filter_stream" do
@@ -332,52 +340,254 @@ class GroongaQueryLogFilterTest < Test::Unit::TestCase
       assert_equal([[@now, statistics]], event_stream.to_a)
     end
 
-    test "timezone" do
-      messages = [
-        "2015-08-12 15:50:40.130990|0x7fb07d113da0|>/d/select?table=Entries&match_columns=name&query=xml",
-        "2015-08-12 15:50:40.296165|0x7fb07d113da0|:000000165177838 filter(10)",
-        "2015-08-12 15:50:40.296172|0x7fb07d113da0|:000000165184723 select(10)",
-        "2015-08-12 15:50:41.228129|0x7fb07d113da0|:000001097153433 output(10)",
-        "2015-08-12 15:50:41.228317|0x7fb07d113da0|<000001097334986 rc=0",
-      ]
-      statistic = {
-        "start_time"  => "2015-08-12T15:50:40.130990+09:00",
-        "last_time"   => "2015-08-12T15:50:41.228324+09:00",
-        "elapsed"     => 1.0973349860000001,
-        "return_code" => 0,
-        "slow"        => true,
-        "command" => {
-          "raw" => "/d/select?table=Entries&match_columns=name&query=xml",
-          "name" => "select",
-          "parameters" => [
-            {"key" => :table,         "value" => "Entries"},
-            {"key" => :match_columns, "value" => "name"},
-            {"key" => :query,         "value" => "xml"},
+    sub_test_case "timezone" do
+      test "localtime" do
+        messages = [
+          "2015-08-12 15:50:40.130990|0x7fb07d113da0|>/d/select?table=Entries&match_columns=name&query=xml",
+          "2015-08-12 15:50:40.296165|0x7fb07d113da0|:000000165177838 filter(10)",
+          "2015-08-12 15:50:40.296172|0x7fb07d113da0|:000000165184723 select(10)",
+          "2015-08-12 15:50:41.228129|0x7fb07d113da0|:000001097153433 output(10)",
+          "2015-08-12 15:50:41.228317|0x7fb07d113da0|<000001097334986 rc=0",
+        ]
+        statistic = {
+          "start_time"  => "2015-08-12T15:50:40.130990+09:00",
+          "last_time"   => "2015-08-12T15:50:41.228324+09:00",
+          "elapsed"     => 1.0973349860000001,
+          "return_code" => 0,
+          "slow"        => true,
+          "command" => {
+            "raw" => "/d/select?table=Entries&match_columns=name&query=xml",
+            "name" => "select",
+            "parameters" => [
+              {"key" => :table,         "value" => "Entries"},
+              {"key" => :match_columns, "value" => "name"},
+              {"key" => :query,         "value" => "xml"},
+            ],
+          },
+          "operations" => [
+            {
+              "context"          => "query: xml",
+              "name"             => "filter",
+              "relative_elapsed" => 0.165177838,
+              "slow"             => true,
+            },
+            {
+              "context"          => nil,
+              "name"             => "select",
+              "relative_elapsed" => 6.884999999999999e-06,
+              "slow"             => false,
+            },
+            {
+              "context"          => nil,
+              "name"             => "output",
+              "relative_elapsed" => 0.93196871,
+              "slow"             => true,
+            },
           ],
-        },
-        "operations" => [
-          {
-            "context"          => "query: xml",
-            "name"             => "filter",
-            "relative_elapsed" => 0.165177838,
-            "slow"             => true,
+        }
+        event_stream = emit("timezone localtime", messages)
+        assert_equal([[@now, statistic]], event_stream.to_a)
+      end
+    end
+
+    sub_test_case "time_format" do
+      test "sql" do
+        messages = [
+          "2015-08-12 15:50:40.130990|0x7fb07d113da0|>/d/select?table=Entries&match_columns=name&query=xml",
+          "2015-08-12 15:50:40.296165|0x7fb07d113da0|:000000165177838 filter(10)",
+          "2015-08-12 15:50:40.296172|0x7fb07d113da0|:000000165184723 select(10)",
+          "2015-08-12 15:50:41.228129|0x7fb07d113da0|:000001097153433 output(10)",
+          "2015-08-12 15:50:41.228317|0x7fb07d113da0|<000001097334986 rc=0",
+        ]
+        statistic = {
+          "start_time"  => "2015-08-12 06:50:40.130990",
+          "last_time"   => "2015-08-12 06:50:41.228324",
+          "elapsed"     => 1.0973349860000001,
+          "return_code" => 0,
+          "slow"        => true,
+          "command" => {
+            "raw" => "/d/select?table=Entries&match_columns=name&query=xml",
+            "name" => "select",
+            "parameters" => [
+              {"key" => :table,         "value" => "Entries"},
+              {"key" => :match_columns, "value" => "name"},
+              {"key" => :query,         "value" => "xml"},
+            ],
           },
-          {
-            "context"          => nil,
-            "name"             => "select",
-            "relative_elapsed" => 6.884999999999999e-06,
-            "slow"             => false,
+          "operations" => [
+            {
+              "context"          => "query: xml",
+              "name"             => "filter",
+              "relative_elapsed" => 0.165177838,
+              "slow"             => true,
+            },
+            {
+              "context"          => nil,
+              "name"             => "select",
+              "relative_elapsed" => 6.884999999999999e-06,
+              "slow"             => false,
+            },
+            {
+              "context"          => nil,
+              "name"             => "output",
+              "relative_elapsed" => 0.93196871,
+              "slow"             => true,
+            },
+          ],
+        }
+        event_stream = emit("time_format sql", messages)
+        assert_equal([[@now, statistic]], event_stream.to_a)
+      end
+
+      test "string" do
+        messages = [
+          "2015-08-12 15:50:40.130990|0x7fb07d113da0|>/d/select?table=Entries&match_columns=name&query=xml",
+          "2015-08-12 15:50:40.296165|0x7fb07d113da0|:000000165177838 filter(10)",
+          "2015-08-12 15:50:40.296172|0x7fb07d113da0|:000000165184723 select(10)",
+          "2015-08-12 15:50:41.228129|0x7fb07d113da0|:000001097153433 output(10)",
+          "2015-08-12 15:50:41.228317|0x7fb07d113da0|<000001097334986 rc=0",
+        ]
+        statistic = {
+          "start_time"  => "2015-08-12 06:50:40.130990",
+          "last_time"   => "2015-08-12 06:50:41.228324",
+          "elapsed"     => 1.0973349860000001,
+          "return_code" => 0,
+          "slow"        => true,
+          "command" => {
+            "raw" => "/d/select?table=Entries&match_columns=name&query=xml",
+            "name" => "select",
+            "parameters" => [
+              {"key" => :table,         "value" => "Entries"},
+              {"key" => :match_columns, "value" => "name"},
+              {"key" => :query,         "value" => "xml"},
+            ],
           },
-          {
-            "context"          => nil,
-            "name"             => "output",
-            "relative_elapsed" => 0.93196871,
-            "slow"             => true,
+          "operations" => [
+            {
+              "context"          => "query: xml",
+              "name"             => "filter",
+              "relative_elapsed" => 0.165177838,
+              "slow"             => true,
+            },
+            {
+              "context"          => nil,
+              "name"             => "select",
+              "relative_elapsed" => 6.884999999999999e-06,
+              "slow"             => false,
+            },
+            {
+              "context"          => nil,
+              "name"             => "output",
+              "relative_elapsed" => 0.93196871,
+              "slow"             => true,
+            },
+          ],
+        }
+        event_stream = emit("time_format %Y-%m-%d %H:%M:%S.%6N", messages)
+        assert_equal([[@now, statistic]], event_stream.to_a)
+      end
+
+      test "sql and localtime" do
+        messages = [
+          "2015-08-12 15:50:40.130990|0x7fb07d113da0|>/d/select?table=Entries&match_columns=name&query=xml",
+          "2015-08-12 15:50:40.296165|0x7fb07d113da0|:000000165177838 filter(10)",
+          "2015-08-12 15:50:40.296172|0x7fb07d113da0|:000000165184723 select(10)",
+          "2015-08-12 15:50:41.228129|0x7fb07d113da0|:000001097153433 output(10)",
+          "2015-08-12 15:50:41.228317|0x7fb07d113da0|<000001097334986 rc=0",
+        ]
+        statistic = {
+          "start_time"  => "2015-08-12 15:50:40.130990",
+          "last_time"   => "2015-08-12 15:50:41.228324",
+          "elapsed"     => 1.0973349860000001,
+          "return_code" => 0,
+          "slow"        => true,
+          "command" => {
+            "raw" => "/d/select?table=Entries&match_columns=name&query=xml",
+            "name" => "select",
+            "parameters" => [
+              {"key" => :table,         "value" => "Entries"},
+              {"key" => :match_columns, "value" => "name"},
+              {"key" => :query,         "value" => "xml"},
+            ],
           },
-        ],
-      }
-      event_stream = emit("timezone localtime", messages)
-      assert_equal([[@now, statistic]], event_stream.to_a)
+          "operations" => [
+            {
+              "context"          => "query: xml",
+              "name"             => "filter",
+              "relative_elapsed" => 0.165177838,
+              "slow"             => true,
+            },
+            {
+              "context"          => nil,
+              "name"             => "select",
+              "relative_elapsed" => 6.884999999999999e-06,
+              "slow"             => false,
+            },
+            {
+              "context"          => nil,
+              "name"             => "output",
+              "relative_elapsed" => 0.93196871,
+              "slow"             => true,
+            },
+          ],
+        }
+        event_stream = emit(<<-CONFIGURATION, messages)
+          timezone localtime
+          time_format sql
+        CONFIGURATION
+        assert_equal([[@now, statistic]], event_stream.to_a)
+      end
+
+      test "string and localtime" do
+        messages = [
+          "2015-08-12 15:50:40.130990|0x7fb07d113da0|>/d/select?table=Entries&match_columns=name&query=xml",
+          "2015-08-12 15:50:40.296165|0x7fb07d113da0|:000000165177838 filter(10)",
+          "2015-08-12 15:50:40.296172|0x7fb07d113da0|:000000165184723 select(10)",
+          "2015-08-12 15:50:41.228129|0x7fb07d113da0|:000001097153433 output(10)",
+          "2015-08-12 15:50:41.228317|0x7fb07d113da0|<000001097334986 rc=0",
+        ]
+        statistic = {
+          "start_time"  => "2015-08-12 15:50:40.130990",
+          "last_time"   => "2015-08-12 15:50:41.228324",
+          "elapsed"     => 1.0973349860000001,
+          "return_code" => 0,
+          "slow"        => true,
+          "command" => {
+            "raw" => "/d/select?table=Entries&match_columns=name&query=xml",
+            "name" => "select",
+            "parameters" => [
+              {"key" => :table,         "value" => "Entries"},
+              {"key" => :match_columns, "value" => "name"},
+              {"key" => :query,         "value" => "xml"},
+            ],
+          },
+          "operations" => [
+            {
+              "context"          => "query: xml",
+              "name"             => "filter",
+              "relative_elapsed" => 0.165177838,
+              "slow"             => true,
+            },
+            {
+              "context"          => nil,
+              "name"             => "select",
+              "relative_elapsed" => 6.884999999999999e-06,
+              "slow"             => false,
+            },
+            {
+              "context"          => nil,
+              "name"             => "output",
+              "relative_elapsed" => 0.93196871,
+              "slow"             => true,
+            },
+          ],
+        }
+        event_stream = emit(<<-CONFIGURATION, messages)
+          timezone localtime
+          time_format %Y-%m-%d %H:%M:%S.%6N
+        CONFIGURATION
+        assert_equal([[@now, statistic]], event_stream.to_a)
+      end
     end
   end
 end
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.osdn.me/mailman/archives/groonga-commit/attachments/20190208/f3e7c11e/attachment-0001.html>


More information about the Groonga-commit mailing list
Back to archive index