{"id":447,"date":"2021-03-15T14:08:56","date_gmt":"2021-03-15T06:08:56","guid":{"rendered":"https:\/\/twocups.cn\/?p=447"},"modified":"2021-06-28T19:28:14","modified_gmt":"2021-06-28T11:28:14","slug":"33","status":"publish","type":"post","link":"https:\/\/twocups.cn\/index.php\/2021\/03\/15\/33\/","title":{"rendered":"\u3010Elastic Stack\u7cfb\u5217\u3011\u7b2c\u56db\u7ae0\uff1a\u6e90\u7801\u5206\u6790(\u4e8c) Metricbeat\u7bc7"},"content":{"rendered":"\n<h4><img width=\"80\" src=\"https:\/\/twocups.cn\/wp-content\/uploads\/2019\/12\/logo\u6a2a.png\" alt=\"\" class=\"wp-image-152\" srcset=\"https:\/\/twocups.cn\/wp-content\/uploads\/2019\/12\/logo\u6a2a.png 3337w, https:\/\/twocups.cn\/wp-content\/uploads\/2019\/12\/logo\u6a2a-300x125.png 300w, https:\/\/twocups.cn\/wp-content\/uploads\/2019\/12\/logo\u6a2a-1024x426.png 1024w, https:\/\/twocups.cn\/wp-content\/uploads\/2019\/12\/logo\u6a2a-768x320.png 768w, https:\/\/twocups.cn\/wp-content\/uploads\/2019\/12\/logo\u6a2a-1536x639.png 1536w, https:\/\/twocups.cn\/wp-content\/uploads\/2019\/12\/logo\u6a2a-2048x852.png 2048w\" sizes=\"(max-width: 3337px) 100vw, 3337px\" \/>\u7248\u6743\u58f0\u660e\uff1a\u672c\u6587\u4e3a\u535a\u4e3b\u539f\u521b\u6587\u7ae0\uff0c\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u51fa\u5904\uff1a<a href=\"https:\/\/twocups.cn\/index.php\/2021\/03\/15\/33\/\">https:\/\/twocups.cn\/index.php\/2021\/03\/15\/33\/<\/a><\/h4>\n\n\n\n<p><strong>Metricbeat \u6e90\u7801\u5730\u5740\uff1a<a href=\"https:\/\/github.com\/elastic\/beats\/tree\/master\/metricbeat\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/github.com\/elastic\/beats\/tree\/master\/metricbeat<\/a><\/strong><\/p>\n\n\n\n<h2>\u5199\u5728\u524d\u9762<\/h2>\n\n\n\n<p>\u4e0a\u4e00\u7bc7\u6211\u4eec\u5206\u6790\u4e86 Filebeat \u7684\u6e90\u7801\uff0c\u8fd9\u4e00\u7bc7\u6211\u4eec\u6765\u5206\u6790 <strong>Metricbeat <\/strong>\u7684\u6e90\u7801\u3002\u5728\u6211\u8bfb Metricbeat \u7684\u6e90\u7801\u4e4b\u524d\uff0c\u4ee5\u4e3a Metricbeat \u548c Filebeat \u7684\u6e90\u7801\u5728\u5b9e\u73b0\u5c42\u9762\u662f\u76f8\u8fd1\u7684\uff0c\u7ed3\u679c\u8bfb\u5b8c\u540e\u53d1\u73b0\u5176\u5b9e\u5b83\u4eec\u9664\u4e86 libbeat \u5e93\u4e2d\u7528\u5230\u7684\u4e1c\u897f\u548c\u6700\u540e\u53d1\u5e03\u5230\u540e\u7aef\u7684\u6d41\u7a0b\u5dee\u4e0d\u591a\u4e4b\u5916\uff0c\u5176\u4ed6\u65b9\u9762\u5173\u7cfb\u8fd8\u771f\u7684\u4e0d\u662f\u5f88\u5927\u3002\u800c\u4e14\u4e0d\u77e5\u9053\u662f\u4e0d\u662f\u6211\u7684\u9519\u89c9\uff0cMetricbeat \u7684\u6e90\u7801\u98ce\u683c\u548c Filebeat \u4e0d\u592a\u4e00\u6837\uff0c\u5176\u4e2d\u9762\u5411\u5bf9\u8c61\u7684\u601d\u60f3\u7279\u522b\u6d53\u70c8\uff0c\u5e76\u4e14\u5728\u7ba1\u9053\u7684\u7279\u6027\u548c\u4f7f\u7528\u4e0a\u76f8\u5bf9\u4e8e Filebeat \u6765\u8bf4\u6ca1\u6709\u90a3\u4e48\u591a\u3002\u8fd9\u6b21\u7684\u6e90\u7801\u5206\u6790\u548c\u4e0a\u6b21\u4e00\u6837\uff0c\u5206\u4e3a\u5de5\u4f5c\u539f\u7406\u548c\u8fd0\u884c\u6d41\u7a0b\u3002\u5e76\u4e14\uff0c\u8fd0\u884c\u6d41\u7a0b\u4e5f\u540c\u6837\u662f\u4e00\u4e2a\u4e3b\u6d41\u7a0b\uff0c\u5f88\u591a\u6697\u7ebf\u4e5f\u6ca1\u6709\u63d0\u5230\u3002<\/p>\n\n\n\n<h2>Metricbeat \u7684\u5de5\u4f5c\u539f\u7406<\/h2>\n\n\n\n<p><strong>\u5de5\u4f5c\u539f\u7406\u7684\u5b98\u65b9\u6587\u6863\uff1a<a href=\"https:\/\/www.elastic.co\/guide\/en\/beats\/metricbeat\/current\/how-metricbeat-works.html\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.elastic.co\/guide\/en\/beats\/metricbeat\/current\/how-metricbeat-works.html<\/a><\/strong><\/p>\n\n\n\n<p>Metricbeat \u4e2d\u6700\u91cd\u8981\u7684\u4e24\u4e2a\u7ec4\u4ef6\u662f <strong>Module <\/strong>\u548c <strong>Metricset<\/strong>\u3002Module \u5b9a\u4e49\u4e86 Metricbeat \u91c7\u96c6\u6570\u636e\u7684\u5bf9\u8c61\uff0c\u5373\u53bb\u54ea\u91cc\u6536\u96c6\u6570\u636e\uff0c\u4f8b\u5982 MySQL\u3001Redis\u3001System \u7b49\uff1b\u800c Metricset \u5b9a\u4e49\u4e86 Metricbeat \u91c7\u96c6\u5bf9\u8c61\u4e2d\u9700\u8981\u91c7\u96c6\u7684\u6307\u6807\uff0c\u5373\u6536\u96c6\u54ea\u4e9b\u6570\u636e\uff0c\u4f8b\u5982 System \u4e2d\u7684 cpu\u3001\u5185\u5b58\u3001\u786c\u76d8\u3001\u7f51\u7edc\u7b49\u3002<\/p>\n\n\n\n<p>\u5f88\u660e\u663e\uff0cMetricset \u4e0e Module \u662f\u4ece\u5c5e\u5173\u7cfb\uff0c\u5373\u6bcf\u4e00\u4e2a Module \u90fd\u5305\u542b\u8bb8\u591a Metricset\u3002<strong>Module <\/strong>\u662f\u5c5e\u4e8e\u670d\u52a1\u5c42\u9762\u7684\uff0c\u5b83\u6307\u5b9a\u4e0e\u670d\u52a1\u6709\u5173\u7684\u8be6\u7ec6\u4fe1\u606f\uff0c\u4f8b\u5982 Metricbeat \u548c\u670d\u52a1\u5982\u4f55\u8fde\u63a5\u3001Metricbeat \u91c7\u96c6\u670d\u52a1\u6307\u6807\u7684\u9891\u7387\u3001Metricbeat \u91c7\u96c6\u54ea\u4e9b\u6307\u6807\u7b49\u3002<strong>Metricset <\/strong>\u662f Module \u4e2d\u8d1f\u8d23\u83b7\u53d6\u548c\u6784\u5efa\u6570\u636e\u7684\u7ec4\u4ef6\uff0c\u5b83\u53d1\u9001\u4e00\u6b21\u8bf7\u6c42\u5c31\u53ef\u4ee5\u4ece\u670d\u52a1\u4e2d\u53d6\u56de\u4e00\u7cfb\u5217\u7684\u6307\u6807\u3002\u5c31\u50cf\u6211\u4eec\u5728\u5b9e\u9645\u90e8\u7f72\u90a3\u4e00\u7ae0\u4e3e\u8fc7\u7684\u4f8b\u5b50\uff0cRedis \u7684 Module \u7ec4\u4ef6\u4e2d\u5305\u542b\u4e86\u4e00\u4e2a INFO Metricset\uff0c\u800c\u8fd9\u4e2a Metricset \u5c31\u662f\u670d\u52a1\u6536\u96c6 Redis \u4e2d\u7684\u76f8\u5173\u6307\u6807\u7684\uff0c\u7136\u540e\u518d\u628a\u6536\u96c6\u5230\u7684\u6307\u6807\u53d1\u9001\u5230 Elasticsearch\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" src=\"https:\/\/twocups.cn\/wp-content\/uploads\/2021\/03\/ES\u7b2c\u56db\u7ae0\uff08\u4e8c\uff09_metricbeat\u4f8b\u5b50.png\" alt=\"\" class=\"wp-image-448\" width=\"663\" height=\"281\" srcset=\"https:\/\/twocups.cn\/wp-content\/uploads\/2021\/03\/ES\u7b2c\u56db\u7ae0\uff08\u4e8c\uff09_metricbeat\u4f8b\u5b50.png 884w, https:\/\/twocups.cn\/wp-content\/uploads\/2021\/03\/ES\u7b2c\u56db\u7ae0\uff08\u4e8c\uff09_metricbeat\u4f8b\u5b50-300x127.png 300w, https:\/\/twocups.cn\/wp-content\/uploads\/2021\/03\/ES\u7b2c\u56db\u7ae0\uff08\u4e8c\uff09_metricbeat\u4f8b\u5b50-768x325.png 768w\" sizes=\"(max-width: 663px) 100vw, 663px\" \/><\/figure><\/div>\n\n\n\n<h2>Metricbeat \u542f\u52a8\u6d41\u7a0b<\/h2>\n\n\n\n<p>\u6211\u753b\u4e86\u4e00\u4e2a Metricbeat \u8fd0\u884c\u7684<strong>\u6d41\u7a0b\u56fe<\/strong>\uff0c\u91cc\u9762\u63cf\u8ff0\u4e86 Metricbeat \u7684\u4e3b\u8981\u7ec4\u4ef6\u91c7\u96c6\u65e5\u5fd7\u6570\u636e\u7684\u8fc7\u7a0b\u3002\u5927\u5bb6\u53ef\u4ee5\u5148\u770b\u770b\u56fe\uff0c\u6709\u4e2a\u5927\u6982\u7684\u5370\u8c61\uff0c\u6211\u4eec\u518d\u8be6\u7ec6\u5f80\u4e0b\u8bf4\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" src=\"https:\/\/twocups.cn\/wp-content\/uploads\/2021\/03\/Metricbeat\u6e90\u7801\u5206\u6790_\u5173\u7cfb\u56fe.png\" alt=\"\" class=\"wp-image-449\" width=\"1150\" height=\"500\" srcset=\"https:\/\/twocups.cn\/wp-content\/uploads\/2021\/03\/Metricbeat\u6e90\u7801\u5206\u6790_\u5173\u7cfb\u56fe.png 2300w, https:\/\/twocups.cn\/wp-content\/uploads\/2021\/03\/Metricbeat\u6e90\u7801\u5206\u6790_\u5173\u7cfb\u56fe-300x130.png 300w, https:\/\/twocups.cn\/wp-content\/uploads\/2021\/03\/Metricbeat\u6e90\u7801\u5206\u6790_\u5173\u7cfb\u56fe-1024x445.png 1024w, https:\/\/twocups.cn\/wp-content\/uploads\/2021\/03\/Metricbeat\u6e90\u7801\u5206\u6790_\u5173\u7cfb\u56fe-768x334.png 768w, https:\/\/twocups.cn\/wp-content\/uploads\/2021\/03\/Metricbeat\u6e90\u7801\u5206\u6790_\u5173\u7cfb\u56fe-1536x668.png 1536w, https:\/\/twocups.cn\/wp-content\/uploads\/2021\/03\/Metricbeat\u6e90\u7801\u5206\u6790_\u5173\u7cfb\u56fe-2048x890.png 2048w\" sizes=\"(max-width: 1150px) 100vw, 1150px\" \/><\/figure><\/div>\n\n\n\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u8be6\u7ec6\u770b\u770b Metricbeat \u662f\u5982\u4f55\u8fd0\u884c\u7684\u3002<\/p>\n\n\n\n<p>\u9996\u5148\uff0cMetricbeat \u6587\u4ef6\u5939\u6839\u76ee\u5f55\u4e0b\u7684 main.go \u6587\u4ef6\u4e3b\u65b9\u6cd5\u8fd0\u884c\u3002\u5176\u4e2d\uff0c\u91cd\u8981\u7684\u662f\u5f15\u5165\u4e86 \/cmd\/root.go \u6587\u4ef6\u4e2d\u7684 <strong>RootCmd<\/strong> \u53d8\u91cf \uff0c\u8be5\u53d8\u91cf\u7684\u53d8\u91cf\u7c7b\u578b\u662f BeatsRootCmd\uff0c\u7ed3\u6784\u4f53\u5728\u4e0b\u9762\u7ed9\u51fa\u4e86\u3002BeatsRootCmd \u7684\u529f\u80fd\u662f\u5904\u7406\u6240\u6709\u5e94\u7528\u7a0b\u5e8f\u7684\u547d\u4ee4\u884c\u9875\u9762\uff0c\u540c\u65f6\u89e3\u6790\u7528\u6237\u6807\u5fd7\u5e76\u8fd0\u884c\u5b50\u547d\u4ee4\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">func main() {\n\tif err := cmd.RootCmd.Execute(); err != nil {\n\t\tos.Exit(1)\n\t}\n}\n\/\/ BeatsRootCmd\u7ed3\u6784\u4f53\ntype BeatsRootCmd struct {\n\tcobra.Command\n\tRunCmd        *cobra.Command\n\tSetupCmd      *cobra.Command\n\tVersionCmd    *cobra.Command\n\tCompletionCmd *cobra.Command\n\tExportCmd     *cobra.Command\n\tTestCmd       *cobra.Command\n\tKeystoreCmd   *cobra.Command\n}<\/pre>\n\n\n\n<p>\u65e2\u7136\u5f15\u5165\u4e86 \/cmd\/root.go \u6587\u4ef6\uff0c\u90a3\u6587\u4ef6\u4e2d\u7684<strong>\u521d\u59cb\u5316<\/strong>\u65b9\u6cd5\u4e5f\u4f1a\u8fd0\u884c\u3002\u53d8\u91cf RootCmd \u7684\u58f0\u660e\u5728\u8be5\u6587\u4ef6\u7684\u5f00\u5934\u7ed9\u51fa\u4e86\uff0c\u4e0b\u9762\u7684\u521d\u59cb\u5316\u65b9\u6cd5\u53ea\u662f\u8be5\u53d8\u91cf\u7684\u8d4b\u503c\u3002\u53d8\u91cf RootCmd \u7684\u503c\u5c06\u7531\u521d\u59cb\u5316\u65b9\u6cd5 Initialize \u5f97\u5230\uff0c\u800c\u8be5\u65b9\u6cd5\u7684\u53c2\u6570\u662f Metricbeat \u7684\u9ed8\u8ba4\u53c2\u6570\uff0c\u7531\u65b9\u6cd5 MetricbeatSettings \u5f97\u5230\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">func init() {\n\tRootCmd = Initialize(MetricbeatSettings())\n}\n\/\/ \u8bbe\u7f6eMetricbeat \u7684\u9ed8\u8ba4\u53c2\u6570\nfunc MetricbeatSettings() instance.Settings {\n\tvar runFlags = pflag.NewFlagSet(Name, pflag.ExitOnError)\n\trunFlags.AddGoFlag(flag.CommandLine.Lookup(\"system.hostfs\"))\n\treturn instance.Settings{\n\t\tRunFlags:      runFlags,\n\t\tName:          Name,\n\t\tHasDashboards: true,\n\t\tProcessing:    processing.MakeDefaultSupport(true, withECSVersion, processing.WithHost, processing.WithAgentMeta()),\n\t}\n}<\/pre>\n\n\n\n<p>\u65b9\u6cd5 <strong>Initialize <\/strong>\u8d1f\u8d23\u521d\u59cb\u5316 Metricbeat \u7684\u5165\u53e3\u547d\u4ee4\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">func Initialize(settings instance.Settings) *cmd.BeatsRootCmd {\n\trootCmd := cmd.GenRootCmdWithSettings(beater.DefaultCreator(), settings)\n\trootCmd.AddCommand(cmd.GenModulesCmd(Name, \"\", BuildModulesManager))\n\trootCmd.TestCmd.AddCommand(test.GenTestModulesCmd(Name, \"\", beater.DefaultTestModulesCreator()))\n\treturn rootCmd\n}<\/pre>\n\n\n\n<p>\u65b9\u6cd5 <strong>GenRootCmdWithSettings <\/strong>\u8fdb\u4e00\u6b65\u5904\u7406\u547d\u4ee4\uff0c\u5e76\u4e14\u8fd4\u56de\u6211\u4eec\u4f7f\u7528\u7684 Metricbeat \u7684\u6839\u547d\u4ee4\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">func GenRootCmdWithSettings(beatCreator beat.Creator, settings instance.Settings) *BeatsRootCmd {\n\tif settings.IndexPrefix == \"\" {\n\t\tsettings.IndexPrefix = settings.Name\n\t}\n\trootCmd := &amp;BeatsRootCmd{}\n\trootCmd.Use = settings.Name\n\t\/\/ \u7531beat\u7684\u540d\u79f0\u6765\u83b7\u53d6\u9ed8\u8ba4\u914d\u7f6e\uff0c\u8fd9\u91cc\u7684beat\u540d\u79f0\u6307\u7684\u5c31\u662fmetricbeat\n\terr := cfgfile.ChangeDefaultCfgfileFlag(settings.Name)\n\tif err != nil {\n\t\tpanic(fmt.Errorf(\"failed to set default config file path: %v\", err))\n\t}\n\t\/\/ \u6839\u636e\u914d\u7f6e\u5904\u7406\u547d\u4ee4\u884c\u7684\u547d\u4ee4\n\trootCmd.RunCmd = genRunCmd(settings, beatCreator)\n\trootCmd.ExportCmd = genExportCmd(settings)\n\trootCmd.TestCmd = genTestCmd(settings, beatCreator)\n\trootCmd.SetupCmd = genSetupCmd(settings, beatCreator)\n\trootCmd.KeystoreCmd = genKeystoreCmd(settings)\n\trootCmd.VersionCmd = GenVersionCmd(settings)\n\trootCmd.CompletionCmd = genCompletionCmd(settings, rootCmd)\n\trootCmd.Run = rootCmd.RunCmd.Run\n\t\/\/ Persistent flags\u662f\u6301\u4e45\u8868\u793a\uff0c\u5728\u6240\u6709\u5b50\u547d\u4ee4\u4e2d\u90fd\u901a\u7528\n\trootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup(\"E\"))\n\trootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup(\"c\"))\n\trootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup(\"d\"))\n\trootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup(\"v\"))\n\trootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup(\"e\"))\n\trootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup(\"environment\"))\n\trootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup(\"path.config\"))\n\trootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup(\"path.data\"))\n\trootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup(\"path.logs\"))\n\trootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup(\"path.home\"))\n\trootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup(\"strict.perms\"))\n\tif f := flag.CommandLine.Lookup(\"plugin\"); f != nil {\n\t\trootCmd.PersistentFlags().AddGoFlag(f)\n\t}\n\t\/\/ \u7ee7\u627f\u6839flags\n\trootCmd.Flags().AddFlagSet(rootCmd.RunCmd.Flags())\n\t\/\/ \u6ce8\u518c\u6240\u6709beat\u5171\u6709\u7684\u5b50\u547d\u4ee4\n\trootCmd.AddCommand(rootCmd.RunCmd)\n\trootCmd.AddCommand(rootCmd.SetupCmd)\n\trootCmd.AddCommand(rootCmd.VersionCmd)\n\trootCmd.AddCommand(rootCmd.CompletionCmd)\n\trootCmd.AddCommand(rootCmd.ExportCmd)\n\trootCmd.AddCommand(rootCmd.TestCmd)\n\trootCmd.AddCommand(rootCmd.KeystoreCmd)\n\treturn rootCmd\n}<\/pre>\n\n\n\n<p>\u65b9\u6cd5 <strong>genRunCmd <\/strong>\u662f libbeat \u5e93\u4e2d \/libbeat\/cmd\/run.go \u4e2d\u7684\u552f\u4e00\u65b9\u6cd5\uff0c\u5b83\u7684\u4efb\u52a1\u5c31\u662f\u8fd0\u884c\u6839\u547d\u4ee4\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">func genRunCmd(settings instance.Settings, beatCreator beat.Creator) *cobra.Command {\n\tname := settings.Name\n\trunCmd := cobra.Command{\n\t\tUse:   \"run\",\n\t\tShort: \"Run \" + name,\n\t\tRun: func(cmd *cobra.Command, args []string) {\n\t\t\terr := instance.Run(settings, beatCreator)\n\t\t\tif err != nil {\n\t\t\t\tos.Exit(1)\n\t\t\t}\n\t\t},\n\t}\n\t\/\/ \u8fd0\u884c\u5b50\u547d\u4ee4\u7684flags\n\trunCmd.Flags().AddGoFlag(flag.CommandLine.Lookup(\"N\"))\n\trunCmd.Flags().AddGoFlag(flag.CommandLine.Lookup(\"httpprof\"))\n\trunCmd.Flags().AddGoFlag(flag.CommandLine.Lookup(\"cpuprofile\"))\n\trunCmd.Flags().AddGoFlag(flag.CommandLine.Lookup(\"memprofile\"))\n\tif settings.RunFlags != nil {\n\t\trunCmd.Flags().AddFlagSet(settings.RunFlags)\n\t}\n\treturn &amp;runCmd\n}<\/pre>\n\n\n\n<p>\u65b9\u6cd5 <strong>Run <\/strong>\u7684\u529f\u80fd\u662f\u521d\u59cb\u5316\u5e76\u4e14\u5b9e\u4f8b\u5316\u4e00\u4e2abeat\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">func Run(settings Settings, bt beat.Creator) error {\n\terr := setUmaskWithSettings(settings)\n\tif err != nil &amp;&amp; err != errNotImplemented {\n\t\treturn errw.Wrap(err, \"could not set umask\")\n\t}\n    \/\/ \u51c6\u5907\u597d\u5c06\u8981\u5b9e\u4f8b\u5316\u7684beat\u7684\u53c2\u6570\n    \/\/ \u8fd9\u91cc\u7684name\u7684\u503c\u5c31\u662fmetricbeat\n\tname := settings.Name\n\tidxPrefix := settings.IndexPrefix\n\tversion := settings.Version\n\telasticLicensed := settings.ElasticLicensed\n\treturn handleError(func() error {\n\t\tdefer func() {\n\t\t\tif r := recover(); r != nil {\n\t\t\t\tlogp.NewLogger(name).Fatalw(\"Failed due to panic.\",\n\t\t\t\t\t\"panic\", r, zap.Stack(\"stack\"))\n\t\t\t}\n\t\t}()\n        \/\/ \u5b9e\u4f8b\u5316\u4e00\u4e2abeat\n\t\tb, err := NewBeat(name, idxPrefix, version, elasticLicensed)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\t\/\/ \u5c06\u5b9e\u4f8b\u5316\u7684beat\u7684\u4fe1\u606f\u6dfb\u52a0\u8fdb\u6ce8\u518c\u8868\u548c\u76d1\u89c6\u5668\n\t\tregistry := monitoring.GetNamespace(\"info\").GetRegistry()\n\t\tmonitoring.NewString(registry, \"version\").Set(b.Info.Version)\n\t\tmonitoring.NewString(registry, \"beat\").Set(b.Info.Beat)\n\t\tmonitoring.NewString(registry, \"name\").Set(b.Info.Name)\n\t\tmonitoring.NewString(registry, \"hostname\").Set(b.Info.Hostname)\n\t\t\/\/ \u6dfb\u52a0\u989d\u5916\u4fe1\u606f\u5230\u7ba1\u7406state\u7684\u6ce8\u518c\u8868\uff0c\u4e4b\u540e\u53d1\u9001\u5230\u76d1\u89c6\u5668\n\t\tstateRegistry := monitoring.GetNamespace(\"state\").GetRegistry()\n\t\tserviceRegistry := stateRegistry.NewRegistry(\"service\")\n\t\tmonitoring.NewString(serviceRegistry, \"version\").Set(b.Info.Version)\n\t\tmonitoring.NewString(serviceRegistry, \"name\").Set(b.Info.Beat)\n\t\tbeatRegistry := stateRegistry.NewRegistry(\"beat\")\n\t\tmonitoring.NewString(beatRegistry, \"name\").Set(b.Info.Name)\n\t\tmonitoring.NewFunc(stateRegistry, \"host\", host.ReportInfo, monitoring.Report)\n        \/\/ \u542f\u52a8\u8fd9\u4e2abeat\n\t\treturn b.launch(settings, bt)\n\t}())\n}<\/pre>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u521a\u624d\u521b\u5efa\u7684 Beat \u5b9e\u4f8b\u7684<strong>\u542f\u52a8<\/strong>\u6d41\u7a0b\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">func (b *Beat) launch(settings Settings, bt beat.Creator) error {\n\tdefer logp.Sync()\n\tdefer logp.Info(\"%s stopped.\", b.Info.Beat)\n    \/\/ beat\u521d\u59cb\u5316\n\terr := b.InitWithSettings(settings)\n\tif err != nil {\n\t\treturn err\n\t}\n\tdefer func() {\n\t\tif err := b.processing.Close(); err != nil {\n\t\t\tlogp.Warn(\"Failed to close global processing: %v\", err)\n\t\t}\n\t}()\n\t\/\/ \u5c06\u670d\u52a1\u6807\u8bb0\u4e3a\u505c\u6b62\n\tdefer svc.NotifyTermination()\n\t\/\/ \u4e0a\u6392\u4ed6\u9501\n\tbl := newLocker(b)\n\terr = bl.lock()\n\tif err != nil {\n\t\treturn err\n\t}\n\tdefer bl.unlock()\n\t\/\/ \u4ece\u6ce8\u518c\u8868\u4e2d\u53d8\u91cf\u4e2d\u8bbe\u7f6ebeat\u7684id\n\tinfoRegistry := monitoring.GetNamespace(\"info\").GetRegistry()\n\tmonitoring.NewString(infoRegistry, \"uuid\").Set(b.Info.ID.String())\n\tserviceRegistry := monitoring.GetNamespace(\"state\").GetRegistry().GetRegistry(\"service\")\n\tmonitoring.NewString(serviceRegistry, \"id\").Set(b.Info.ID.String())\n\tsvc.BeforeRun()\n\tdefer svc.Cleanup()\n\t\/\/ \u5728Seccomp\u9501\u5b9a\u4e4b\u524d\u542f\u52a8API\u670d\u52a1\u5668\uff0c\u8fd9\u6837\u662f\u4e3a\u4e86\u521b\u5efaUnix\u5957\u63a5\u5b57\u96c6\uff0c\u4ee5\u5bf9Unix\u57df\u6587\u4ef6\u8bbe\u7f6e\u9002\u5f53\u7684\u6743\u9650\n\tif b.Config.HTTP.Enabled() {\n\t\ts, err := api.NewWithDefaultRoutes(logp.NewLogger(\"\"), b.Config.HTTP, monitoring.GetNamespace)\n\t\tif err != nil {\n\t\t\treturn errw.Wrap(err, \"could not start the HTTP server for the API\")\n\t\t}\n\t\ts.Start()\n\t\tdefer s.Stop()\n\t}\n\tif err = seccomp.LoadFilter(b.Config.Seccomp); err != nil {\n\t\treturn err\n\t}\n\tbeater, err := b.createBeater(bt)\n\tif err != nil {\n\t\treturn err\n\t}\n\tr, err := b.setupMonitoring(settings)\n\tif err != nil {\n\t\treturn err\n\t}\n\tif r != nil {\n\t\tdefer r.Stop()\n\t}\n\tif b.Config.MetricLogging == nil || b.Config.MetricLogging.Enabled() {\n\t\treporter, err := log.MakeReporter(b.Info, b.Config.MetricLogging)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\tdefer reporter.Stop()\n\t}\n\tctx, cancel := context.WithCancel(context.Background())\n\tvar stopBeat = func() {\n\t\tb.Instrumentation.Tracer().Close()\n\t\tbeater.Stop()\n\t}\n\tsvc.HandleSignals(stopBeat, cancel)\n\terr = b.loadDashboards(ctx, false)\n\tif err != nil {\n\t\treturn err\n\t}\n\tlogp.Info(\"%s start running.\", b.Info.Beat)\n\t\/\/ \u542f\u52a8\u8be5beat\u7684\u914d\u7f6e\u7ba1\u7406\u5668\n\tb.Manager.Start(beater.Stop)\n\tdefer b.Manager.Stop()\n    \/\/ \u8fd0\u884c\u8be5beat\n\treturn beater.Run(&amp;b.Beat)\n}<\/pre>\n\n\n\n<p>\u4ece\u8fd9\u91cc\u5f00\u59cb\u79bb\u5f00 libbeat \u5e93\u4e86\uff0c\u6b63\u5f0f\u5f00\u59cb <strong>Metricbeat <\/strong>\u672c\u8eab\u7684\u903b\u8f91\u4e86\u3002\u65b9\u6cd5 Run \u542f\u52a8 Metricbeat \u7684\u5de5\u4f5c\u7a0b\u5e8f\uff0c\u5e76\u4e14\u5728\u6700\u540e\u7b49\u5f85\u961f\u5217\u8fdb\u884c\u5835\u585e\uff0c\u76f4\u5230\u8c03\u7528\u4e86\u65b9\u6cd5 Stop\u5e76\u5b8c\u6210\u5de5\u4f5c\u4e3a\u6b62\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">func (bt *Metricbeat) Run(b *beat.Beat) error {\n\tvar wg sync.WaitGroup\n\tfor _, r := range bt.runners {\n        \/\/ \u542f\u52a8metricbeat\u5b9e\u4f8b\u7684runner\n\t\tr.Start()\n        \/\/ \u7b49\u5f85\u961f\u5217\u52a0\u4e00\n\t\twg.Add(1)\n\t\tthatRunner := r\n        \/\/ \u5bf9\u4e8e\u4e0eMetribeat\u5173\u8054\u7684\u6bcf\u4e2a\u4e3b\u673a\u90fd\u6709\u5176\u81ea\u5df1\u7684goroutine\u6765\u83b7\u53d6\u6570\u636e\n\t\tgo func() {\n\t\t\tdefer wg.Done()\n\t\t\t&lt;-bt.done\n\t\t\tthatRunner.Stop()\n\t\t}()\n\t}\n\t\/\/ \u7ba1\u7406modules\n\tfactory := module.NewFactory(b.Info, bt.moduleOptions...)\n\tmodules := cfgfile.NewRunnerList(management.DebugK, factory, b.Publisher)\n\treload.Register.MustRegisterList(b.Info.Beat+\".modules\", modules)\n\twg.Add(1)\n\tgo func() {\n\t\tdefer wg.Done()\n\t\t&lt;-bt.done\n\t\tmodules.Stop()\n\t}()\n\tif bt.config.ConfigModules.Enabled() {\n\t\tmoduleReloader := cfgfile.NewReloader(b.Publisher, bt.config.ConfigModules)\n\t\tif err := moduleReloader.Check(factory); err != nil {\n\t\t\treturn err\n\t\t}\n\t\tgo moduleReloader.Run(factory)\n\t\twg.Add(1)\n\t\tgo func() {\n\t\t\tdefer wg.Done()\n\t\t\t&lt;-bt.done\n\t\t\tmoduleReloader.Stop()\n\t\t}()\n\t}\n\tif bt.autodiscover != nil {\n\t\tbt.autodiscover.Start()\n\t\twg.Add(1)\n\t\tgo func() {\n\t\t\tdefer wg.Done()\n\t\t\t&lt;-bt.done\n\t\t\tbt.autodiscover.Stop()\n\t\t}()\n\t}\n\twg.Wait()\n\treturn nil\n}<\/pre>\n\n\n\n<p>\u4e4b\u540e\u542f\u52a8 Metricbeat \u5b9e\u4f8b\u7684 <strong>runner<\/strong>\u3002\u8fd9\u91cc\u662f Metricbeat \u5f88\u91cd\u8981\u7684\u4e00\u4e2a\u5206\u6c34\u5cad\uff0c\u8be5\u65b9\u6cd5\u9996\u5148\u4ece\u670d\u52a1\u4e2d\u83b7\u5f97\u4e86\u4e00\u7cfb\u5217\u6307\u6807\u6570\u636e\uff0c\u4e4b\u540e\u518d\u5c06\u8fd9\u4e9b\u6307\u6807\u53c2\u6570\u53d1\u9001\u5230\u6307\u5b9a\u7684\u540e\u7aef\uff0c\u4f8b\u5982 Elasticsearch\u3002\u90a3\u4e48\u6211\u4eec\u63a5\u4e0b\u6765\u4e5f\u4f9d\u6b21\u53bb\u9605\u8bfb\u8fd9\u4e24\u4e2a\u90e8\u5206\u7684\u4ee3\u7801\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">func (mr *runner) Start() {\n    \/\/ startOnce\u662frunner\u7684\u540c\u6b65\u9501\uff0c\u4e0b\u9762\u7ed9\u51farunner\u7684\u7ed3\u6784\u4f53\n\tmr.startOnce.Do(func() {\n        \/\/ \u91c7\u96c6\u6570\u636e\n\t\toutput := mr.mod.Start(mr.done)\n        \/\/ runner\u7684\u7b49\u5f85\u961f\u5217\u52a0\u4e00\n\t\tmr.wg.Add(1)\n\t\tmoduleList.Add(mr.mod.Name())\n\t\tgo func() {\n\t\t\tdefer mr.wg.Done()\n            \/\/ \u53d1\u9001\u6570\u636e\n\t\t\tPublishChannels(mr.client, output)\n\t\t}()\n\t})\n}\n\/\/ runner\u7ed3\u6784\u4f53\ntype runner struct {\n\tdone      chan struct{}\n\twg        sync.WaitGroup\n\tstartOnce sync.Once\n\tstopOnce  sync.Once\n\tmod       *Wrapper\n\tclient    beat.Client\n}<\/pre>\n\n\n\n<h2>Metricbeat \u6570\u636e\u91c7\u96c6\u6d41\u7a0b<\/h2>\n\n\n\n<p><strong>Wrapper <\/strong>\u662fMetricbeat \u4e2d\u5f88\u91cd\u8981\u7684\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u770b\u5b83\u7684\u540d\u5b57\u5c31\u80fd\u731c\u5230\uff0c\u5b83\u662f\u4e2a\u5305\u88c5\u5668\uff0c\u5305\u88c5\u4e86 Metricbeat \u4e2d\u6700\u91cd\u8981\u7684\u4e24\u4e2a\u7ec4\u4ef6\uff1aModule \u548c Metricset\u3002Wrapper \u7684 Start \u65b9\u6cd5\u5b9e\u8d28\u4e0a\u662f\u542f\u52a8\u4e86 Module \u4e2d\u7684 Metricset \u5de5\u4f5c\u5668\uff0c\u800c\u8fd9\u4e2a\u5de5\u4f5c\u5668\u7684\u4f5c\u7528\u5c31\u662f\u83b7\u53d6\uff08Fetch\uff09\u6307\u6807\u6570\u636e\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">func (mw *Wrapper) Start(done &lt;-chan struct{}) &lt;-chan beat.Event {\n\tdebugf(\"Starting %s\", mw)\n    \/\/ \u5efa\u7acb\u5171\u4eab\u7ba1\u9053\uff0c\u7528\u6765\u5b58\u50a8\u7ed3\u679c\u6570\u636e\n\tout := make(chan beat.Event, 1)\n\tvar wg sync.WaitGroup\n\twg.Add(len(mw.metricSets))\n\tfor _, msw := range mw.metricSets {\n        \/\/ \u6bcf\u4e2ametricset\u90fd\u548c\u4e00\u4e2a\u4e3b\u673a\u642d\u914d\uff0c\u542f\u52a8\u4e00\u4e2a\u7ebf\u7a0b\n\t\tgo func(msw *metricSetWrapper) {\n\t\t\tmetricsPath := msw.ID()\n\t\t\tregistry := monitoring.GetNamespace(\"dataset\").GetRegistry()\n\t\t\tdefer registry.Remove(metricsPath)\n\t\t\tdefer releaseStats(msw.stats)\n\t\t\tdefer wg.Done()\n\t\t\tdefer msw.close()\n\t\t\tregistry.Add(metricsPath, msw.Metrics(), monitoring.Full)\n\t\t\tmonitoring.NewString(msw.Metrics(), \"starttime\").Set(common.Time(time.Now()).String())\n            \/\/ \u8fd0\u884cmetricSetWrapper\uff0c\u5c06\u7ed3\u679c\u6570\u636e\u653e\u5165\u5171\u4eab\u7ba1\u9053\n\t\t\tmsw.run(done, out)\n\t\t}(msw)\n\t}\n\t\/\/ \u5355\u5f00\u4e00\u6761\u7ebf\u7a0b\uff0c\u8d1f\u8d23\u5728\u6240\u6709\u5199\u5165\u7ed3\u675f\u65f6\u5173\u95ed\u5171\u4eab\u7ba1\u9053\n\tgo func() {\n\t\twg.Wait()\n\t\tclose(out)\n\t\tdebugf(\"Stopped %s\", mw)\n\t}()\n\treturn out\n}\n\/\/ Wrapper\u7ed3\u6784\u4f53\ntype Wrapper struct {\n    \/\/ module\n\tmb.Module\n    \/\/ metricset\u5217\u8868\n\tmetricSets []*metricSetWrapper\n\t\/\/ \u76f8\u5173\u8bbe\u7f6e\n\tmaxStartDelay  time.Duration\n\teventModifiers []mb.EventModifier\n}<\/pre>\n\n\n\n<p>MetricSetWrapper \u7684 <strong>run <\/strong>\u65b9\u6cd5\u7528\u4e8e\u4ece\u6307\u5b9a\u7684\u4e3b\u673a\u4e2d\u83b7\u53d6\u6307\u6807\u6570\u636e\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">func (msw *metricSetWrapper) run(done &lt;-chan struct{}, out chan&lt;- beat.Event) {\n\tdefer logp.Recover(fmt.Sprintf(\"recovered from panic while fetching \"+\n\t\t\"'%s\/%s' for host '%s'\", msw.module.Name(), msw.Name(), msw.Host()))\n\t\/\/ \u5728\u9650\u5236\u65f6\u95f4maxStartDelay\u5185\u968f\u673a\u542f\u52a8\u6bcf\u4e00\u4e2ametricset\n\tif msw.module.maxStartDelay &gt; 0 {\n\t\tdelay := time.Duration(rand.Int63n(int64(msw.module.maxStartDelay)))\n\t\tdebugf(\"%v\/%v will start after %v\", msw.module.Name(), msw.Name(), delay)\n\t\tselect {\n\t\tcase &lt;-done:\n\t\t\treturn\n\t\tcase &lt;-time.After(delay):\n\t\t}\n\t}\n\tdebugf(\"Starting %s\", msw)\n\tdefer debugf(\"Stopped %s\", msw)\n\t\/\/ reporter\u7528\u4e8e\u62a5\u544aevent\u548c\u9519\u8bef\n\treporter := &amp;eventReporter{\n\t\tmsw:  msw,\n\t\tout:  out,\n\t\tdone: done,\n\t}\n\tswitch ms := msw.MetricSet.(type) {\n\tcase mb.PushMetricSet:\n\t\tms.Run(reporter.V1())\n\tcase mb.PushMetricSetV2:\n\t\tms.Run(reporter.V2())\n\tcase mb.PushMetricSetV2WithContext:\n\t\tms.Run(&amp;channelContext{done}, reporter.V2())\n\tcase mb.EventFetcher, mb.EventsFetcher,\n\t\tmb.ReportingMetricSet, mb.ReportingMetricSetV2, mb.ReportingMetricSetV2Error, mb.ReportingMetricSetV2WithContext:\n        \/\/ \u5f00\u59cb\u5b9a\u671f\u53d6\u56de\u6307\u6807\u6570\u636e\n\t\tmsw.startPeriodicFetching(&amp;channelContext{done}, reporter)\n\tdefault:\n\t\tlogp.Err(\"MetricSet '%s\/%s' does not implement an event producing interface\",\n\t\t\tmsw.Module().Name(), msw.Name())\n\t}\n}<\/pre>\n\n\n\n<p>\u65b9\u6cd5 <strong>startPeriodicFetching <\/strong>\u8d1f\u8d23\u5b9a\u671f\u53d6\u56de\u6307\u6807\u6570\u636e\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">func (msw *metricSetWrapper) startPeriodicFetching(ctx context.Context, reporter reporter) {\n\t\/\/ \u8868\u793a\u5b83\u4f5c\u4e3a\u5b9a\u671f\u53d6\u56de\u6570\u636e\u7684\u7a0b\u5e8f\u542f\u52a8\n\tmsw.periodic = true\n\t\/\/ \u9996\u5148\u7acb\u5373\u53d6\u56de\u4e00\u6b21\u6570\u636e\n\tmsw.fetch(ctx, reporter)\n\t\/\/ \u542f\u52a8\u5b9a\u65f6\u5668\n\tt := time.NewTicker(msw.Module().Config().Period)\n\tdefer t.Stop()\n    \/\/ \u6bcf\u9694\u4e00\u6bb5\u65f6\u95f4\u53d6\u56de\u4e00\u6b21\u6570\u636e\n\tfor {\n\t\tselect {\n\t\tcase &lt;-reporter.V2().Done():\n\t\t\treturn\n\t\tcase &lt;-t.C:\n\t\t\tmsw.fetch(ctx, reporter)\n\t\t}\n\t}\n}<\/pre>\n\n\n\n<p>\u65b9\u6cd5 <strong>fetch <\/strong>\u7684\u529f\u80fd\u5c31\u662f\u6839\u636e\u4e0d\u540c\u7684\u60c5\u51b5\u8c03\u7528\u66f4\u4e3a\u5177\u4f53\u7684\u53d6\u56de\uff08Fetch\uff09\u65b9\u6cd5\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">func (msw *metricSetWrapper) fetch(ctx context.Context, reporter reporter) {\n\tswitch fetcher := msw.MetricSet.(type) {\n\tcase mb.EventFetcher:\n        \/\/ \u5355event\u53d6\u56de\n\t\tmsw.singleEventFetch(fetcher, reporter)\n\tcase mb.EventsFetcher:\n        \/\/ \u591aevent\u53d6\u56de\n\t\tmsw.multiEventFetch(fetcher, reporter)\n\tcase mb.ReportingMetricSet:\n\t\treporter.StartFetchTimer()\n\t\tfetcher.Fetch(reporter.V1())\n\tcase mb.ReportingMetricSetV2:\n\t\treporter.StartFetchTimer()\n\t\tfetcher.Fetch(reporter.V2())\n\tcase mb.ReportingMetricSetV2Error:\n\t\treporter.StartFetchTimer()\n\t\terr := fetcher.Fetch(reporter.V2())\n\t\tif err != nil {\n\t\t\treporter.V2().Error(err)\n\t\t\tlogp.Info(\"Error fetching data for metricset %s.%s: %s\", msw.module.Name(), msw.Name(), err)\n\t\t}\n\tcase mb.ReportingMetricSetV2WithContext:\n\t\treporter.StartFetchTimer()\n\t\terr := fetcher.Fetch(ctx, reporter.V2())\n\t\tif err != nil {\n\t\t\treporter.V2().Error(err)\n\t\t\tlogp.Info(\"Error fetching data for metricset %s.%s: %s\", msw.module.Name(), msw.Name(), err)\n\t\t}\n\tdefault:\n\t\tpanic(fmt.Sprintf(\"unexpected fetcher type for %v\", msw))\n\t}\n}\n\/\/ reporter\u7ed3\u6784\u4f53\ntype reporter interface {\n\tStartFetchTimer()\n\tV1() mb.PushReporter\n\tV2() mb.PushReporterV2\n}<\/pre>\n\n\n\n<p>\u65b9\u6cd5 <strong>multiEventFetch <\/strong>\u80fd\u591f\u53d6\u56de\u591a\u4e2aevent\uff0c\u539f\u7406\u548c\u65b9\u6cd5 singleEventFetch \u662f\u4e00\u6837\u7684\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">func (msw *metricSetWrapper) multiEventFetch(fetcher mb.EventsFetcher, reporter reporter) {\n\treporter.StartFetchTimer()\n\tevents, err := fetcher.Fetch()\n\tif len(events) == 0 {\n\t\treporter.V1().ErrorWith(err, nil)\n\t} else {\n\t\tfor _, event := range events {\n\t\t\treporter.V1().ErrorWith(err, event)\n\t\t}\n\t}\n}<\/pre>\n\n\n\n<p>\u65b9\u6cd5 <strong>ErrorWith <\/strong>\u6307\u7684\u662f\u5982\u679c\u6709\u9519\u8bef\uff0c\u4f1a\u7531\u8fd9\u4e2a\u65b9\u6cd5\u8fdb\u884c\u5904\u7406\u3002\u8fd9\u4e2a\u65b9\u6cd5\u53ea\u662f\u4e2a\u60c5\u51b5\u5224\u65ad\uff0c\u5177\u4f53\u903b\u8f91\u5728\u65b9\u6cd5 Event \u4e2d\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">func (r reporterV1) ErrorWith(err error, meta common.MapStr) bool {\n\tif err == nil &amp;&amp; meta == nil {\n\t\treturn true\n\t}\n    \/\/ \u5c06common.MapStr\u8f6c\u6362\u4e3aevent\n\treturn r.v2.Event(mb.TransformMapStrToEvent(r.module, meta, err))\n}\nfunc (r reporterV2) Event(event mb.Event) bool {\n    \/\/\u5148\u8fdb\u884c\u4e00\u7cfb\u5217\u5f02\u5e38\u60c5\u51b5\u5224\u65ad\n\tif event.Took == 0 &amp;&amp; !r.start.IsZero() {\n\t\tevent.Took = time.Since(r.start)\n\t}\n    \/\/ \u68c0\u6d4b\u662f\u5426\u4e3a\u5b9a\u671f\u542f\u52a8\u7a0b\u5e8f\uff0c\u4e4b\u524d\u6211\u4eec\u8bf4\u8fc7\u7684\n\tif r.msw.periodic {\n\t\tevent.Period = r.msw.Module().Config().Period\n\t}\n\tif event.Timestamp.IsZero() {\n\t\tif !r.start.IsZero() {\n\t\t\tevent.Timestamp = r.start\n\t\t} else {\n\t\t\tevent.Timestamp = time.Now().UTC()\n\t\t}\n\t}\n\tif event.Host == \"\" {\n\t\tevent.Host = r.msw.HostData().SanitizedURI\n\t}\n\tif event.Error == nil {\n\t\tr.msw.stats.success.Add(1)\n\t} else {\n\t\tr.msw.stats.failures.Add(1)\n\t}\n\tif event.Namespace == \"\" {\n\t\tevent.Namespace = r.msw.Registration().Namespace\n\t}\n    \/\/ \u628aevent\u5305\u88c5\u4e00\u4e0b\uff0c\u6539\u6539\u6570\u636e\uff0c\u751f\u6210beatEvent\n\tbeatEvent := event.BeatEvent(r.msw.module.Name(), r.msw.MetricSet.Name(), r.msw.module.eventModifiers...)\n    \/\/ \u628a\u751f\u6210\u7684beatEvent\u53d1\u9001\u5230\u4e4b\u524d\u521b\u5efa\u7684\u5171\u4eab\u7ba1\u9053\u91cc\n\tif !writeEvent(r.done, r.out, beatEvent) {\n\t\treturn false\n\t}\n\tr.msw.stats.events.Add(1)\n\treturn true\n}\n\/\/ \u4e00\u4e2a\u5de5\u5177\u7c7b\uff0c\u4f5c\u7528\u662f\u5c06\u4e0a\u9762\u5f97\u5230beatEvent\u53d1\u9001\u5230\u4e4b\u524d\u6211\u4eec\u521b\u5efa\u7684\u5171\u4eab\u7ba1\u9053\u91cc\nfunc writeEvent(done &lt;-chan struct{}, out chan&lt;- beat.Event, event beat.Event) bool {\n\tselect {\n\tcase &lt;-done:\n\t\treturn false\n\tcase out &lt;- event:\n\t\treturn true\n\t}\n}<\/pre>\n\n\n\n<p>\u65b9\u6cd5 <strong>BeatEvent <\/strong>\u5bf9 event  \u8fdb\u884c\u4e00\u4e2a\u5305\u88c5\uff0c\u5e76\u4e14\u80fd\u591f\u66f4\u6539 event \u4e2d\u7684\u57fa\u7840\u6570\u636e\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">func (e *Event) BeatEvent(module, metricSet string, modifiers ...EventModifier) beat.Event {\n\tif e.RootFields == nil {\n\t\te.RootFields = common.MapStr{}\n\t}\n\tfor _, modify := range modifiers {\n\t\tmodify(module, metricSet, e)\n\t}\n\tb := beat.Event{\n\t\tTimestamp:  e.Timestamp,\n\t\tFields:     e.RootFields,\n\t\tTimeSeries: !e.DisableTimeSeries,\n\t}\n\tif len(e.ModuleFields) &gt; 0 {\n\t\tb.Fields.Put(module, e.ModuleFields)\n\t\te.ModuleFields = nil\n\t}\n\tif e.Service == \"\" {\n\t\te.Service = module\n\t}\n\te.RootFields.Put(\"service.type\", e.Service)\n\tif len(e.MetricSetFields) &gt; 0 {\n\t\tswitch e.Namespace {\n\t\tcase \".\":\n\t\t\t\/\/ \".\"\u7684\u610f\u601d\u5c31\u662f\u4e0a\u4e00\u7ea7\u76ee\u5f55\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u7684\u610f\u601d\u5c31\u662f\u5c06fields\u6dfb\u52a0\u5230\u5f53\u524d\u6839\u76ee\u5f55\n\t\t\tb.Fields.DeepUpdate(e.MetricSetFields)\n\t\tcase \"\":\n\t\t\tb.Fields.Put(module+\".\"+metricSet, e.MetricSetFields)\n\t\tdefault:\n\t\t\tb.Fields.Put(e.Namespace, e.MetricSetFields)\n\t\t}\n\t\te.MetricSetFields = nil\n\t}\n\t\/\/ \u8bbe\u7f6e\u7d22\u5f15\u524d\u7f00\u6765\u8986\u76d6\u9ed8\u8ba4\u503c\n\tif e.Index != \"\" {\n\t\tb.Meta = common.MapStr{\"index\": e.Index}\n\t}\n\tif e.ID != \"\" {\n\t\tb.SetID(e.ID)\n\t}\n\tif e.Error != nil {\n\t\tb.Fields[\"error\"] = common.MapStr{\n\t\t\t\"message\": e.Error.Error(),\n\t\t}\n\t}\n\treturn b\n}<\/pre>\n\n\n\n<h2>Metricbeat \u6570\u636e\u53d1\u9001\u6d41\u7a0b<\/h2>\n\n\n\n<p>\u73b0\u5728\u6211\u4eec\u56de\u5230 runner \u5b9e\u4f8b\u7684 Start \u65b9\u6cd5\u3002\u521a\u624d\u6211\u4eec\u8d70\u7684\u662f\u91c7\u96c6\u6570\u636e\u7684\u90a3\u6761\u8def\uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u7ee7\u7eed\u8d70<strong>\u53d1\u9001\u6570\u636e<\/strong>\u7684\u90a3\u6761\u8def\u3002\u8fd9\u5c31\u76f8\u5bf9\u7b80\u5355\u5f88\u591a\u4e86\uff0c\u548c\u4e4b\u524d\u8bf4\u7684 Filebeat \u5dee\u4e0d\u591a\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">func (mr *runner) Start() {\n    \/\/ startOnce\u662frunner\u7684\u540c\u6b65\u9501\uff0c\u4e0b\u9762\u7ed9\u51farunner\u7684\u7ed3\u6784\u4f53\n\tmr.startOnce.Do(func() {\n        \/\/ \u91c7\u96c6\u6570\u636e\n\t\toutput := mr.mod.Start(mr.done)\n        \/\/ runner\u7684\u7b49\u5f85\u961f\u5217\u52a0\u4e00\n\t\tmr.wg.Add(1)\n\t\tmoduleList.Add(mr.mod.Name())\n\t\tgo func() {\n\t\t\tdefer mr.wg.Done()\n            \/\/ \u53d1\u9001\u6570\u636e\n\t\t\tPublishChannels(mr.client, output)\n\t\t}()\n\t})\n}<\/pre>\n\n\n\n<p>Metricbeat \u4e5f\u662f\u4f1a\u5c06\u6536\u96c6\u5230\u7684\u6307\u6807\u6570\u636e\u901a\u8fc7\u7ba1\u9053\u8fdb\u884c\u4f20\u8f93\u3002\u65b9\u6cd5 <strong>PublishChannels <\/strong>\u4f1a\u5c06\u6bcf\u4e2a\u7ba1\u9053\u8bfb\u53d6\u5230\u7684 event \u53d1\u9001\u5230\u6307\u5b9a\u7684\u5ba2\u6237\u7aef\u3002\u5173\u4e8e\u5ba2\u6237\u7aef Client \u53ca\u5176\u53d1\u5e03 Publish \u7684\u95ee\u9898\uff0c\u4e5f\u548c Filebeat \u4e2d\u4e00\u6837\uff0c\u662f\u7531\u5404\u4e2a\u540e\u7aef\u6765\u5b9e\u73b0\u63a5\u53e3\u7684\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">func PublishChannels(client beat.Client, cs ...&lt;-chan beat.Event) {\n\tvar wg sync.WaitGroup\n\t\/\/ output\u4ecebeat.Event\u7ba1\u9053\u53d1\u5e03\u4e86event\u76f4\u5230\u7ba1\u9053\u5173\u95ed\n\toutput := func(c &lt;-chan beat.Event) {\n\t\tdefer wg.Done()\n\t\tfor event := range c {\n\t\t\tclient.Publish(event)\n\t\t}\n\t}\n\t\/\/ \u4e3a\u7ba1\u9053\u4e2d\u7684\u6bcf\u4e00\u4e2a\u8f93\u5165\u901a\u9053\u5f00\u542f\u4e00\u4e2aoutput\u7684\u7ebf\u7a0b\n\twg.Add(len(cs))\n\tfor _, c := range cs {\n\t\tgo output(c)\n\t}\n\twg.Wait()\n}<\/pre>\n\n\n\n<p>Client \u63a5\u53e3\u5982\u4e0b\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">type Client interface {\n\tPublish(Event)\n\tPublishAll([]Event)\n\tClose() error\n}<\/pre>\n\n\n\n<h2>\u5199\u5728\u6700\u540e<\/h2>\n\n\n\n<p>\u5f00\u5934\u4e5f\u8bf4\u4e86\uff0c\u8fd9\u6b21 Metricbeat \u7684\u6e90\u7801\u5c55\u793a\u7684\u662f\u6700\u4e3b\u8981\u7684\u90a3\u6761\u6d41\u7a0b\uff0c\u5176\u4ed6\u6697\u7ebf\u4e5f\u6709\u63d0\u5230\uff0c\u4f46\u662f\u4e0d\u591a\u3002\u540e\u9762\u7684\u8bdd\uff0c\u6211\u4f1a\u9488\u5bf9\u6bd4\u8f83<strong>\u5177\u4f53\u7684\u573a\u666f<\/strong>\u6765\u8fdb\u884c\u6e90\u7801\u5206\u6790\uff0c\u6bd4\u5982\u8bf4 Beats \u4e2d\u6dfb\u52a0\u66f4\u52a0\u5177\u4f53\u7684\u8fc7\u6ee4\u548c\u68b3\u7406\u6570\u636e\u7684\u6a21\u5757\uff0c\u800c\u4e0d\u662f\u76f4\u63a5\u7528 Logstash\uff0c\u6bd5\u7adf\u5b83\u662f\u8dd1\u5728 JVM \u91cc\u9762\u7684\u3002\u90a3\u4e48\u9488\u5bf9\u6dfb\u52a0\u65b0\u6a21\u5757\uff0c\u6211\u4f1a\u5c55\u793a\u5982\u4f55\u5b9a\u4f4d\u5230\u4e0e\u65b0\u529f\u80fd\u76f8\u5173\u7684\u4ee3\u7801\uff0c\u7136\u540e\u5bf9\u5176\u5206\u6790\u5e76\u8fdb\u884c\u4fee\u6539\u548c\u6dfb\u52a0\uff0c\u4e4b\u540e\u518d\u8003\u8651\u80fd\u4e0d\u80fd\u7ee7\u7eed\u6539\u8fdb\u6216\u4f18\u5316\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7248\u6743\u58f0\u660e\uff1a\u672c\u6587\u4e3a\u535a\u4e3b\u539f\u521b\u6587\u7ae0\uff0c\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u51fa\u5904\uff1ahttps:\/\/twocups.cn\/index.php\/202 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":234,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[99],"tags":[100,112,110],"_links":{"self":[{"href":"https:\/\/twocups.cn\/index.php\/wp-json\/wp\/v2\/posts\/447"}],"collection":[{"href":"https:\/\/twocups.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/twocups.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/twocups.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/twocups.cn\/index.php\/wp-json\/wp\/v2\/comments?post=447"}],"version-history":[{"count":0,"href":"https:\/\/twocups.cn\/index.php\/wp-json\/wp\/v2\/posts\/447\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/twocups.cn\/index.php\/wp-json\/wp\/v2\/media\/234"}],"wp:attachment":[{"href":"https:\/\/twocups.cn\/index.php\/wp-json\/wp\/v2\/media?parent=447"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/twocups.cn\/index.php\/wp-json\/wp\/v2\/categories?post=447"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/twocups.cn\/index.php\/wp-json\/wp\/v2\/tags?post=447"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}