<commit_before>﻿using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace DatadogSharp.DogStatsd
{
    public class DatadogStats : IDisposable
    {
        public static DatadogStats Default { get; private set; } = new DatadogStats();

        public static void ConfigureDefault(string address, int port = 8125, string metricNamePrefix = null, string[] defaultTags = null)
        {
            Default.Dispose();
            Default = new DatadogStats(address, port, metricNamePrefix, defaultTags);
        }

        bool isNull;
        string metricNamePrefix = null;
        string[] defaultTags = null;
        IPEndPoint endPoint;
        Socket udpSocket;
        Encoding utf8 = Encoding.UTF8;

        DatadogStats()
        {
            isNull = true;
        }

        public DatadogStats(string address, int port, string metricNamePrefix = null, string[] defaultTags = null)
        {
            this.metricNamePrefix = metricNamePrefix;
            if (defaultTags != null)
            {
                this.defaultTags = defaultTags;
            }

            this.udpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            this.endPoint = new IPEndPoint(IPAddress.Parse(address), port);
            this.isNull = false;
        }

        public string WithPrefix(string name)
        {
            if (string.IsNullOrEmpty(metricNamePrefix)) return name;

            return metricNamePrefix + "." + name;
        }

        public string[] WithDefaultTag(string[] tag)
        {
            if (defaultTags == null || defaultTags.Length == 0)
            {
                return tag;
            }
            else
            {
                if (tag == null || tag.Length == 0) return defaultTags;

                var joinedTags = new string[defaultTags.Length + tag.Length];
                Array.Copy(defaultTags, 0, joinedTags, 0, defaultTags.Length);
                Array.Copy(tag, 0, joinedTags, defaultTags.Length, tag.Length);
                return joinedTags;
            }
        }

        public void Send(string command)
        {
            Send(utf8.GetBytes(command));
        }

        public void Send(byte[] command)
        {
            udpSocket.SendTo(command, 0, command.Length, SocketFlags.None, endPoint);
        }

        // Metrics

        public void Increment(string metricName, long value = 1, double sampleRate = 1.0, string[] tags = null)
        {
            Counter(WithPrefix(metricName), value, sampleRate, tags);
        }

        public void Decrement(string metricName, long value = 1, double sampleRate = 1.0, string[] tags = null)
        {
            Counter(WithPrefix(metricName), -value, sampleRate, tags);
        }

        public void Counter(string metricName, long value, double sampleRate = 1.0, string[] tags = null)
        {
            if (isNull) return;
            if (sampleRate == 1.0 || sampleRate < ThreadSafeUtil.ThreadStaticRandom.NextDouble())
            {
                var command = DogStatsDFormatter.Counter(WithPrefix(metricName), value, sampleRate, WithDefaultTag(tags));
                Send(command);
            }
        }
<commit_msg>Fix Incr, Decr outputs duplicate prefix<commit_after>﻿using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace DatadogSharp.DogStatsd
{
    public class DatadogStats : IDisposable
    {
        public static DatadogStats Default { get; private set; } = new DatadogStats();

        public static void ConfigureDefault(string address, int port = 8125, string metricNamePrefix = null, string[] defaultTags = null)
        {
            Default.Dispose();
            Default = new DatadogStats(address, port, metricNamePrefix, defaultTags);
        }

        bool isNull;
        string metricNamePrefix = null;
        string[] defaultTags = null;
        IPEndPoint endPoint;
        Socket udpSocket;
        Encoding utf8 = Encoding.UTF8;

        DatadogStats()
        {
            isNull = true;
        }

        public DatadogStats(string address, int port, string metricNamePrefix = null, string[] defaultTags = null)
        {
            this.metricNamePrefix = metricNamePrefix;
            if (defaultTags != null)
            {
                this.defaultTags = defaultTags;
            }

            this.udpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            this.endPoint = new IPEndPoint(IPAddress.Parse(address), port);
            this.isNull = false;
        }

        public string WithPrefix(string name)
        {
            if (string.IsNullOrEmpty(metricNamePrefix)) return name;

            return metricNamePrefix + "." + name;
        }

        public string[] WithDefaultTag(string[] tag)
        {
            if (defaultTags == null || defaultTags.Length == 0)
            {
                return tag;
            }
            else
            {
                if (tag == null || tag.Length == 0) return defaultTags;

                var joinedTags = new string[defaultTags.Length + tag.Length];
                Array.Copy(defaultTags, 0, joinedTags, 0, defaultTags.Length);
                Array.Copy(tag, 0, joinedTags, defaultTags.Length, tag.Length);
                return joinedTags;
            }
        }

        public void Send(string command)
        {
            Send(utf8.GetBytes(command));
        }

        public void Send(byte[] command)
        {
            udpSocket.SendTo(command, 0, command.Length, SocketFlags.None, endPoint);
        }

        // Metrics

        public void Increment(string metricName, long value = 1, double sampleRate = 1.0, string[] tags = null)
        {
            Counter(metricName, value, sampleRate, tags);
        }

        public void Decrement(string metricName, long value = 1, double sampleRate = 1.0, string[] tags = null)
        {
            Counter(metricName, -value, sampleRate, tags);
        }

        public void Counter(string metricName, long value, double sampleRate = 1.0, string[] tags = null)
        {
            if (isNull) return;
            if (sampleRate == 1.0 || sampleRate < ThreadSafeUtil.ThreadStaticRandom.NextDouble())
            {
                var command = DogStatsDFormatter.Counter(WithPrefix(metricName), value, sampleRate, WithDefaultTag(tags));
                Send(command);
            }
        }

{'fraction_non_alphanumeric': 0.09252944823310601, 'fraction_numerical': 0.00712957222566646, 'mean_word_length': 1.8911290322580645, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1994711', 'n_tokens_mistral': 1759, 'n_tokens_neox': 1660, 'n_words': 433}