コード例 #1
0
        public override bool Input(ICollectContext context)
        {
            foreach (var o in _fieldOptions)
            {
                var src = context.Fields[o.SrcField];

                if (string.IsNullOrEmpty(src))
                {
                    context.Fields[o.DstField] = "";
                }
                else
                {
                    var match = Regex.Match(src, o.Pattern);

                    if (match.Success)
                    {
                        var gs = match.Groups;

                        context.Fields[o.DstField] = gs[gs.Count - 1].Value;
                    }
                    else
                    {
                        context.Fields[o.DstField] = "";
                    }
                }
            }

            return(OutputContext(context));
        }
コード例 #2
0
        public override void Collect(ICollectContext ctx)
        {
            // Load object from memory
            var obj = ctx.Load <Dictionary <string, int> >("stored_object");

            obj["counter"]++;
            ctx.Store("stored_object", obj);

            // Log messages with information from stored object
            ctx.Log(LogLevel.Info, "Log message from C#", new Dictionary <string, string>
            {
                { "language", "c#" },
                { "counter", $"{obj["counter"]}" }
            });
            ctx.AddWarning("Warning from C#");

            // List requested metrics
            var reqMts = ctx.RequestedMetrics();

            if (reqMts.Count > 0)
            {
                Console.WriteLine("Requested metrics: ");
                foreach (var mt in reqMts)
                {
                    Console.WriteLine($"- {mt}");
                }
            }

            // Add metrics
            ctx.AlwaysApply("/example/group1/*", Modifiers.Tags(new Dictionary <string, string>
            {
                { "virtualization", "VirtualBox" }
            }));

            ctx.AddMetric("/example/group1/metric1", 12.4,
                          Modifiers.Tags(new Dictionary <string, string>
            {
                { "origin", "C# lang" },
                { "system", "Windows" }
            }),
                          Modifiers.Description("new custom description")
                          );

            ctx.AddMetric("/example/group1/metric2", 20);
            ctx.AddMetric("/example/group1/metric3", (uint)30);

            if (ctx.ShouldProcess("/example/group2/metric4"))
            {
                ctx.AddMetric("/example/group2/metric4", true);
            }

            ctx.AddMetric("/example/group2/metric5", "string value");
        }
コード例 #3
0
        public override bool Input(ICollectContext context)
        {
            foreach (var to in _tidyFields)
            {
                if (to.Type == "combine")
                {
                    CombineField(context, to);
                }
                else if (to.Type == "rename")
                {
                    RenameField(context, to);
                }
            }

            return(OutputContext(context));
        }
コード例 #4
0
        public override bool Input(ICollectContext context)
        {
            lock (this)
            {
                if (_queue.Count == 500)
                {
                    _isFull = true;
                    _logger.LogInformation($"{this} 待发送的数据已经达到 500,管道开始堵塞");
                    return(false);
                }

                _queue.Enqueue(context);
                mre_addContext.Set();

                return(true);
            }
        }
コード例 #5
0
        private void CombineField(ICollectContext context, TidyOptions options)
        {
            var srcFields = options.SrcField.Split(' ');
            var dstValue  = "";

            foreach (var sf in srcFields)
            {
                var src = context.Fields[sf];

                if (!string.IsNullOrEmpty(src))
                {
                    dstValue += $"{src}_";
                }
            }

            if (dstValue.Length > 1)
            {
                dstValue = dstValue.Remove(dstValue.Length - 1, 1);
            }

            context.Fields[options.DstField] = dstValue;
        }
コード例 #6
0
        private void SendContext(ICollectContext context)
        {
            var body = new JObject();

            foreach (var f in _options.Fields)
            {
                body[f] = context.Fields[f];
            }

            var index = context.Fields[_options.Index];

            if (string.IsNullOrEmpty(index))
            {
                return;
            }

            body["apptype"] = context.Fields[_options.Type];

            var url = $"http://{_options.Host}/logcollect_{index}/_doc";

            var content = new StringContent(body.ToString());

            content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");

            try
            {
                var t = _http.PostAsync(url, content);
                t.Wait();

                var rsp = t.Result;

                _logger.LogInformation($"{url} StatusCode[{(int)rsp.StatusCode}]({rsp.Headers})({content.Headers})");
            }
            catch (Exception ex)
            {
                _logger.LogWarning($"{ex}");
            }
        }
コード例 #7
0
        private void StartSendingThread()
        {
            Task.Run(() =>
            {
                while (State == FilterState.Running)
                {
                    ICollectContext context = null;

                    lock (this)
                    {
                        if (_queue.Count > 0)
                        {
                            context = _queue.Dequeue();
                            //_logger.LogTrace($"{this} 队列缓存 {_queue.Count}");
                        }

                        if (_queue.Count < 200 && _isFull)
                        {
                            _isFull = false;
                            _logger.LogInformation($"{this} 管道开始畅通");
                            Task.Run(() => this.NotiifyPipelineEmpty());
                        }
                    }

                    if (context != null)
                    {
                        //SendContext(context);
                        System.Threading.Thread.Sleep(TimeSpan.FromMilliseconds(20));
                    }

                    if (context == null)
                    {
                        mre_addContext.WaitOne();
                    }
                }
            });
        }
コード例 #8
0
        private void RenameField(ICollectContext context, TidyOptions options)
        {
            context.Fields[options.SrcField] = context.Fields[options.DstField];

            context.Fields.Remove(options.DstField);
        }