예제 #1
0
        public async Task <PutResult> PutHttpAsync(DataPoint dataPoint)
        {
            PutResult result = new PutResult();

            try
            {
                if (_useDiagnostics)
                {
                    DiagnosticCounters.SentDatapoints++;
                }


                var uri = new Uri(_ServerUri + "/api/put" + HttpPutOptions.ToString());
                var spm = ServicePointManager.FindServicePoint(uri);
                spm.Expect100Continue = false;
                HttpWebRequest http = (HttpWebRequest)WebRequest.Create(uri);
                http.SendChunked = false;

                http.Method = "POST";

                http.ContentType = "application/json";

                Encoding encoder = Encoding.UTF8;
                byte[]   data    = encoder.GetBytes(dataPoint.ToJson());

                http.Method        = "POST";
                http.ContentType   = "application/json; charset=utf-8";
                http.ContentLength = data.Length;
                using (Stream stream = http.GetRequestStream())
                {
                    stream.Write(data, 0, data.Length);
                    stream.Close();
                }

                this.DiagnosticCounters.PendingPutRequests++;
                WebResponse response = await http.GetResponseAsync();

                var          streamOutput = response.GetResponseStream();
                StreamReader sr           = new StreamReader(streamOutput);
                string       content      = sr.ReadToEnd();

                PutDetails details = null;
                if (HttpPutOptions.Details || HttpPutOptions.Summary)
                {
                    //try to parse the response

                    details        = Newtonsoft.Json.JsonConvert.DeserializeObject <PutDetails>(content);
                    result.details = details;
                }
                if (_useDiagnostics)
                {
                    DiagnosticCounters.SuccessfulSentDatapoints++;
                    DiagnosticCounters.LastSuccessfulPut = DateTime.Now;
                }
            }
            catch (WebException exc)
            {
                LastException = null;
                if (exc.Response != null)
                {
                    StreamReader reader  = new StreamReader(exc.Response.GetResponseStream());
                    var          content = reader.ReadToEnd();
                    var          error   = JsonConvert.DeserializeObject <ErrorContainer>(content);;
                    if (error != null)
                    {
                        LastException      = error.error;
                        LastException.time = DateTime.Now;
                    }
                }
                if (_useDiagnostics)
                {
                    DiagnosticCounters.FailedSentDatapoints++;
                }
                if (OnOTSDBError != null)
                {
                    OnOTSDBError(LastException, exc);
                }
                result.HasErrors         = true;
                result.OtsbException     = LastException;
                result.originalException = exc;
            }
            catch (Exception exc)
            {
                if (_useDiagnostics)
                {
                    DiagnosticCounters.FailedSentDatapoints--;
                }
                if (OnOTSDBError != null)
                {
                    OnOTSDBError(null, exc);
                }
                result.HasErrors         = true;
                result.originalException = exc;
            }


            finally
            {
                this.DiagnosticCounters.PendingPutRequests--;
            }

            return(result);;
        }
예제 #2
0
        public async Task <PutResult> PutHttpAsync(DataPoint[] dataPoints)
        {
            PutResult result = new PutResult();

            try
            {
                if (_useDiagnostics)
                {
                    DiagnosticCounters.SentDatapoints += (ulong)dataPoints.Count();
                }



                var uri = new Uri(_ServerUri + "api/put" + HttpPutOptions.ToString());
                var spm = ServicePointManager.FindServicePoint(uri);
                spm.Expect100Continue = false;
                HttpWebRequest http = (HttpWebRequest)WebRequest.Create(uri);
                http.SendChunked = false;

                http.Method = "POST";

                http.ContentType = "application/json";

                Encoding encoder = Encoding.UTF8;

                var           dataPointCount = dataPoints.Count();
                StringBuilder sb             = new StringBuilder();
                sb.Append("[");


                foreach (var item in dataPoints)
                {
                    dataPointCount--;
                    sb.Append(item.ToJson());
                    if (dataPointCount > 0)
                    {
                        sb.Append(",");
                    }
                }

                sb.Append("]");

                //   Console.WriteLine(sb.ToString());

                byte[] data = encoder.GetBytes(sb.ToString());

                http.Method        = "POST";
                http.ContentType   = "application/json; charset=utf-8";
                http.ContentLength = data.Length;
                using (Stream stream = http.GetRequestStream())
                {
                    stream.Write(data, 0, data.Length);
                    stream.Close();
                }

                this.DiagnosticCounters.PendingPutRequests++;
                var sw = System.Diagnostics.Stopwatch.StartNew();

                //request -------------
                WebResponse response = await http.GetResponseAsync();

                sw.Stop();
                DiagnosticCounters.LastQueryElapsedMs = sw.ElapsedMilliseconds;

                var streamOutput = response.GetResponseStream();

                StreamReader sr      = new StreamReader(streamOutput);
                string       content = sr.ReadToEnd();

                DiagnosticCounters.LastQueryResponseSize = response.ContentLength;
                PutDetails details = null;
                if (HttpPutOptions.Details || HttpPutOptions.Summary)
                {
                    //try to parse the response

                    details        = Newtonsoft.Json.JsonConvert.DeserializeObject <PutDetails>(content);
                    result.details = details;
                }


                if (_useDiagnostics && details != null)
                {
                    DiagnosticCounters.SuccessfulSentDatapoints += (ulong)details.success;
                    DiagnosticCounters.FailedSentDatapoints     += (ulong)details.failed;
                    if (details.success > 0)
                    {
                        DiagnosticCounters.LastSuccessfulPut = DateTime.Now;
                    }
                }
                else
                {
                    DiagnosticCounters.SuccessfulSentDatapoints++;
                    DiagnosticCounters.LastSuccessfulPut = DateTime.Now;
                }
            }
            catch (WebException exc)
            {
                LastException = null;
                if (exc.Response != null)
                {
                    StreamReader reader  = new StreamReader(exc.Response.GetResponseStream());
                    var          content = reader.ReadToEnd();
                    var          error   = JsonConvert.DeserializeObject <ErrorContainer>(content);;
                    if (error != null)
                    {
                        LastException      = error.error;
                        LastException.time = DateTime.Now;
                    }
                }

                if (_useDiagnostics)
                {
                    DiagnosticCounters.FailedSentDatapoints += (ulong)dataPoints.Count();
                }
                if (OnOTSDBError != null)
                {
                    OnOTSDBError(LastException, exc);
                }

                result.HasErrors         = true;
                result.OtsbException     = LastException;
                result.originalException = exc;
            }
            catch (Exception exc)
            {
                if (_useDiagnostics)
                {
                    DiagnosticCounters.FailedSentDatapoints += (ulong)dataPoints.Count();
                }
                if (OnOTSDBError != null)
                {
                    OnOTSDBError(null, exc);
                }
                result.HasErrors         = true;
                result.originalException = exc;
            }
            finally
            {
                this.DiagnosticCounters.PendingPutRequests--;
            }

            return(result);
        }