Ejemplo n.º 1
0
        public void Dispose()
        {
            if (sw != null)
            {
                sw.Stop();

                switch (type)
                {
                case MeasureType.Timer:
                    ds.Timer(metricName, sw.Elapsed.TotalMilliseconds, sampleRate, tags);
                    break;

                case MeasureType.Gauge:
                    ds.Gauge(metricName, sw.Elapsed.TotalMilliseconds, sampleRate, tags);
                    break;

                case MeasureType.Histogram:
                    ds.Histogram(metricName, sw.Elapsed.TotalMilliseconds, sampleRate, tags);
                    break;

                default:
                    break;
                }

                ThreadSafeUtil.ReturnStopwatch(sw);
                sw = null;
                ds = null;
            }
        }
Ejemplo n.º 2
0
        public void Dispose()
        {
            var stopwatch = Interlocked.Exchange(ref duration, null);

            if (stopwatch == null)
            {
                return;
            }
            stopwatch.Stop();

            var span = new Span
            {
                TraceId  = TraceId,
                SpanId   = SpanId,
                Name     = Name,
                Resource = Resource,
                Service  = Service,
                Type     = Type,
                Start    = start,
                Duration = Span.ToNanoseconds(stopwatch.Elapsed),
                ParentId = null,
                Error    = error,
                Meta     = meta,
            };

            ThreadSafeUtil.ReturnStopwatch(stopwatch);

            Span[] array;
            lock (gate)
            {
                spans.Add(ref span);
                array = spans.ToArray();
            }
            manager.EnqueueToWorker(array);
        }
Ejemplo n.º 3
0
        // metric.name:value|type|@sample_rate|#tag1:value,tag2

        // metric.name should be a String with no colons, bars or @ characters and fit our naming policy.
        // value should be a number
        // type should be c for Counter, g for Gauge, h for Histogram, ms for Timer or s for Set.
        // sample rate is optional and should be a float between 0 and 1 inclusive.
        // tags are optional, and should be a comma seperated list of tags.Colons are used for key value tags.Note that the key device is reserved, tags like “device:xyc” will be dropped by Datadog.

        static string BuildMetrics(string metricName, string value, string type, double sampleRate, string[] tags)
        {
            if (sampleRate == 1.0)
            {
                if (tags == null || tags.Length == 0)
                {
                    return(metricName + ":" + value + "|" + type);
                }
                else
                {
                    var sb = ThreadSafeUtil.RentThreadStaticStringBuilder();
                    sb.Append(metricName);
                    sb.Append(":");
                    sb.Append(value);
                    sb.Append("|");
                    sb.Append(type);
                    sb.Append("|#");
                    for (int i = 0; i < tags.Length; i++)
                    {
                        if (i != 0)
                        {
                            sb.Append(",");
                        }
                        sb.Append(tags[i]);
                    }

                    return(sb.ToString());
                }
            }
            else
            {
                if (tags == null || tags.Length == 0)
                {
                    return(metricName + ":" + value + "|" + type + "|@" + sampleRate.ToString(InvaliantCultrue));
                }
                else
                {
                    var sb = ThreadSafeUtil.RentThreadStaticStringBuilder();
                    sb.Append(metricName);
                    sb.Append(":");
                    sb.Append(value);
                    sb.Append("|");
                    sb.Append(type);
                    sb.Append("|@");
                    sb.Append(sampleRate.ToString(InvaliantCultrue));
                    sb.Append("|#");
                    for (int i = 0; i < tags.Length; i++)
                    {
                        if (i != 0)
                        {
                            sb.Append(",");
                        }
                        sb.Append(tags[i]);
                    }

                    return(sb.ToString());
                }
            }
        }
Ejemplo n.º 4
0
        // _sc|name|status|metadata

        public static string ServiceCheck(string name, string status, int?timestamp = null, string hostName = null, string[] tags = null, string serviceCheckMessage = null, bool truncateText = true)
        {
            var sb = ThreadSafeUtil.RentThreadStaticStringBuilder();

            sb.Append("_sc|");
            sb.Append(name);
            sb.Append("|");
            sb.Append(status);

            sb.Replace("\r", "").Replace("\n", "\\n");

            // Optional
            if (timestamp != null)
            {
                sb.Append("|d:");
                sb.Append(timestamp.Value.ToString(InvaliantCultrue));
            }

            if (hostName != null)
            {
                sb.Append("|h:");
                sb.Append(hostName);
            }

            if (tags != null && tags.Length != 0)
            {
                sb.Append("|#");
                for (int i = 0; i < tags.Length; i++)
                {
                    if (i != 0)
                    {
                        sb.Append(",");
                    }
                    sb.Append(tags[i]);
                }
            }

            if (serviceCheckMessage != null)
            {
                sb.Append("|m");
                if (serviceCheckMessage.Length > 4096)
                {
                    sb.Append(serviceCheckMessage, 0, 4096);
                }
                else
                {
                    sb.Append(serviceCheckMessage);
                }
            }

            return(sb.ToString());
        }
Ejemplo n.º 5
0
 internal SpanScope(string name, string resource, string service, string type, ulong parentId, TracingScope rootScope, bool isAmbientGlobal)
 {
     this.TraceId         = rootScope.TraceId;
     this.Name            = name;
     this.Resource        = resource;
     this.Service         = service;
     this.Type            = type;
     this.parentId        = parentId;
     this.start           = Span.ToNanoseconds(DateTime.UtcNow);
     this.duration        = ThreadSafeUtil.RentStopwatchStartNew();
     this.SpanId          = Span.BuildRandomId();
     this.rootScope       = rootScope;
     this.isAmbientGlobal = isAmbientGlobal;
 }
Ejemplo n.º 6
0
 public TracingScope(string name, string resource, string service, string type, TracingManager manager, ulong traceId, ulong?parentId)
 {
     this.Name     = name;
     this.Resource = resource;
     this.Service  = service;
     this.Type     = type;
     this.TraceId  = traceId;
     this.SpanId   = Span.BuildRandomId();
     this.start    = Span.ToNanoseconds(DateTime.UtcNow);
     this.duration = ThreadSafeUtil.RentStopwatchStartNew();
     this.manager  = manager;
     this.spans    = new StructBuffer <Span>(4);
     this.parentId = parentId;
 }
Ejemplo n.º 7
0
        public void Dispose()
        {
            var stopwatch = Interlocked.Exchange(ref duration, null);

            if (stopwatch == null)
            {
                return;
            }

            stopwatch.Stop();

            var span = new Span
            {
                TraceId  = TraceId,
                SpanId   = SpanId,
                Name     = Name,
                Resource = Resource,
                Service  = Service,
                Type     = Type,
                Start    = start,
                Duration = Span.ToNanoseconds(stopwatch.Elapsed),
                ParentId = parentId,
                Error    = error,
                Meta     = meta,
            };

            if (isAmbientGlobal)
            {
                rootScope.AddSpanAndRemoveAmbient(span, this);
            }
            else
            {
                rootScope.AddSpan(span);
            }

            ThreadSafeUtil.ReturnStopwatch(stopwatch);
        }
Ejemplo n.º 8
0
 public static MeasureElapsedScope StartNew(DatadogStats dogstats, MeasureType type, string metricName, double sampleRate, string[] tags)
 {
     return(new MeasureElapsedScope {
         sw = ThreadSafeUtil.RentStopwatchStartNew(), ds = dogstats, type = type, metricName = metricName, tags = tags, sampleRate = sampleRate
     });
 }
Ejemplo n.º 9
0
        // _e{title.length,text.length}:title|text|d:date_happened|h:hostname|p:priority|t:alert_type|#tag1,tag2

        public static string Event(string title, string text, int?dateHappened = null, string hostName = null, string aggregationKey = null, Priority priority = Priority.Normal, string sourceTypeName = null, AlertType alertType = AlertType.Info, string[] tags = null, bool truncateText = true)
        {
            var sb = ThreadSafeUtil.RentThreadStaticStringBuilder();

            // note: should more improve
            var escapeTitle = title.Replace("\r", "").Replace("\n", "\\n");
            var escapeText  = text.Replace("\r", "").Replace("\n", "\\n");

            sb.Append("_e{");
            sb.Append(escapeTitle.Length.ToString(InvaliantCultrue));
            sb.Append(",");
            sb.Append((truncateText && escapeText.Length > 4096) ? "4096" : escapeText.Length.ToString(InvaliantCultrue));
            sb.Append("}:");

            sb.Append(escapeTitle);
            sb.Append("|");

            if (truncateText && escapeText.Length > 4096)
            {
                sb.Append(escapeText, 0, 4096);
            }
            else
            {
                sb.Append(escapeText);
            }

            // Optional
            if (dateHappened != null)
            {
                sb.Append("|d:");
                sb.Append(dateHappened.Value.ToString(InvaliantCultrue));
            }

            if (hostName != null)
            {
                sb.Append("|h:");
                sb.Append(hostName);
            }

            if (aggregationKey != null)
            {
                sb.Append("|k:");
                sb.Append(aggregationKey);
            }

            if (priority != Priority.Normal)
            {
                sb.Append("|p:");
                sb.Append(priority.ToFormatName());
            }

            if (sourceTypeName != null)
            {
                sb.Append("|s:");
                sb.Append(sourceTypeName);
            }

            if (alertType != AlertType.Info)
            {
                sb.Append("|t:");
                sb.Append(alertType.ToFormatName());
            }

            if (tags != null && tags.Length != 0)
            {
                sb.Append("|#");
                for (int i = 0; i < tags.Length; i++)
                {
                    if (i != 0)
                    {
                        sb.Append(",");
                    }
                    sb.Append(tags[i]);
                }
            }

            return(sb.ToString());
        }