private string GetWriteJsonString(IList <MetricEntity> logs) { List <InfluxRecord> data = new List <InfluxRecord>(); foreach (var item in logs) { IEnumerable <string> columns = new string[] { "value" }; IEnumerable <JsonValue> points = new JsonValue[] { new DoubleJsonValue(item.Value) }; if (item.Tags != null && item.Tags.Count > 0) { var tagKeys = item.Tags.Keys.ToArray(); JsonValue[] tagVals = new JsonValue[item.Tags.Count]; columns = Enumerable.Concat(columns, tagKeys); for (int i = 0; i < tagKeys.Length; i++) { var tagVal = item.Tags[tagKeys[i]]; tagVals[i] = new StringJsonValue(tagVal); } points = Enumerable.Concat(points, tagVals); } var record = new InfluxRecord(item.Name, item.Time, columns, points); data.Add(record); } var jsonstr = new CollectionJsonValue(data.Select(d => d.Json)).AsJson(); return(jsonstr); }
/// <summary> /// Creates a new <see cref="InfluxRecord"/> from the specified name, item name, tags, and fields. /// This uses the timestamp defined on this metrics converter instance. /// </summary> /// <param name="name">The measurement or series name. This value is required and cannot be null or empty.</param> /// <param name="itemName">The set item name. Can contain comma-separated key/value pairs.</param> /// <param name="tags">The optional tags to associate with this record.</param> /// <param name="fields">The <see cref="InfluxField"/> values for the output fields.</param> /// <returns>A new <see cref="InfluxRecord"/> from the specified name, tags, and fields.</returns> public InfluxRecord GetRecord(String name, String itemName, MetricTags tags, IEnumerable <InfluxField> fields) { var jtags = InfluxUtils.JoinTags(itemName, GlobalTags, tags); // global tags must be first so they can get overridden var record = new InfluxRecord(name, jtags, fields, Timestamp); return(record); }
/// <summary> /// Writes the record to the InfluxDB server. If batching is used, the record will be added to the /// batch buffer but will not immediately be written to the server. If the number of buffered records /// is greater than or equal to the BatchSize, then the batch will be flushed to the underlying writer. /// </summary> /// <param name="record">The record to write.</param> public virtual void Write(InfluxRecord record) { if (record == null) { throw new ArgumentNullException(nameof(record)); } batch.Add(record); if (batchSize > 0 && batch.Count >= batchSize) { Flush(); // flush if batch is full } }
private static JsonObject ToJsonObject(InfluxRecord record) { if (record == null) { throw new ArgumentNullException(nameof(record)); } if (String.IsNullOrWhiteSpace(record.Name)) { throw new ArgumentNullException(nameof(record.Name), "The measurement name must be specified."); } if (record.Fields.Count == 0) { throw new ArgumentNullException(nameof(record.Fields), $"Must specify at least one field. Metric name: {record.Name}"); } var cols = record.Tags.Select(t => t.Key).Concat(record.Fields.Select(f => f.Key)); var data = record.Tags.Select(t => t.Value).Concat(record.Fields.Select(f => f.Value)).Select(v => FormatValue(v)); return(ToJsonObject(record.Name, record.Timestamp ?? DateTime.Now, cols, data)); }
/// <summary> /// Formats the measurement name, tag keys, and field keys on the specified <see cref="InfluxRecord"/> /// with the defined tag and key formatters and returns the same record instance. /// </summary> /// <param name="record">The <see cref="InfluxRecord"/> to format the tag and field keys for.</param> /// <returns>The same <see cref="InfluxRecord"/> instance with the tag and field keys formatted.</returns> public virtual InfluxRecord FormatRecord(InfluxRecord record) { record.Name = FormatMetricName(null, record.Name, Unit.None, null) ?? record.Name; for (int i = 0; i < record.Tags.Count; i++) { InfluxTag tag = record.Tags[i]; String fmtKey = FormatTagKey(tag.Key); record.Tags[i] = new InfluxTag(fmtKey, tag.Value); } for (int i = 0; i < record.Fields.Count; i++) { InfluxField field = record.Fields[i]; String fmtKey = FormatFieldKey(field.Key); record.Fields[i] = new InfluxField(fmtKey, field.Value); } return(record); }
/// <summary> /// 描述:异步将LogMetric写入Influxdb数据库 /// 作者:徐明祥 /// 日期:20150531 /// </summary> /// <param name="logs"></param> public void WriteAsync(IList <MetricEntity> logs) { if (logs == null || logs.Count == 0) { return; } List <InfluxRecord> data = new List <InfluxRecord>(); foreach (var item in logs) { IEnumerable <string> columns = new string[] { "value" }; IEnumerable <JsonValue> points = new JsonValue[] { new DoubleJsonValue(item.Value) }; if (item.Tags != null && item.Tags.Count > 0) { var tagKeys = item.Tags.Keys.ToArray(); JsonValue[] tagVals = new JsonValue[item.Tags.Count]; columns = Enumerable.Concat(columns, tagKeys); for (int i = 0; i < tagKeys.Length; i++) { var tagVal = item.Tags[tagKeys[i]]; tagVals[i] = new StringJsonValue(tagVal); } points = Enumerable.Concat(points, tagVals); } var record = new InfluxRecord(item.Name, item.Time, columns, points); data.Add(record); } using (var client = new WebClient()) { var jsonstr = new CollectionJsonValue(data.Select(d => d.Json)).AsJson(); client.UploadStringAsync(this.influxdb, jsonstr); } }