func (decoder *JSONDecoderV2) Read(ctx context.Context, req *http.Request) error { dec := json.NewDecoder(req.Body) var d JSONDatapointV2 if err := dec.Decode(&d); err != nil { return err } dps := make([]*datapoint.Datapoint, 0, len(d)) for metricType, datapoints := range d { mt, ok := com_signalfx_metrics_protobuf.MetricType_value[strings.ToUpper(metricType)] if !ok { log.WithField("metricType", metricType).Warn("Unknown metric type") continue } for _, jsonDatapoint := range datapoints { v, err := ValueToValue(jsonDatapoint.Value) if err != nil { log.WithField("err", err).Warn("Unable to get value for datapoint") } else { dp := datapoint.New(jsonDatapoint.Metric, jsonDatapoint.Dimensions, v, fromMT(com_signalfx_metrics_protobuf.MetricType(mt)), fromTs(jsonDatapoint.Timestamp)) dps = append(dps, dp) } } } return decoder.Sink.AddDatapoints(ctx, dps) }
func TestConver(t *testing.T) { assert.Panics(t, func() { toMT(datapoint.MetricType(1001)) }) assert.Panics(t, func() { fromMT(com_signalfx_metrics_protobuf.MetricType(1001)) }) }
func (handler *metricHandler) ServeHTTP(writter http.ResponseWriter, req *http.Request) { dec := json.NewDecoder(req.Body) var d []MetricCreationStruct if err := dec.Decode(&d); err != nil { log.WithField("err", err).Info("Invalid metric creation request") writter.WriteHeader(http.StatusBadRequest) writter.Write([]byte(`{msg:"Invalid creation request"}`)) return } log.WithField("d", d).Debug("Got metric types") handler.metricCreationsMapMutex.Lock() defer handler.metricCreationsMapMutex.Unlock() ret := []MetricCreationResponse{} for _, m := range d { metricType, ok := com_signalfx_metrics_protobuf.MetricType_value[m.MetricType] if !ok { writter.WriteHeader(http.StatusBadRequest) writter.Write([]byte(`{msg:"Invalid metric type"}`)) return } handler.metricCreationsMap[m.MetricName] = com_signalfx_metrics_protobuf.MetricType(metricType) ret = append(ret, MetricCreationResponse{Code: 409}) } unmarshal := handler.jsonMarshal if unmarshal == nil { unmarshal = json.Marshal } toWrite, err := unmarshal(ret) if err != nil { log.WithField("err", err).Warn("Unable to marshal json") writter.WriteHeader(http.StatusBadRequest) writter.Write([]byte(`{msg:"Unable to marshal json!"}`)) return } writter.WriteHeader(http.StatusOK) writter.Write([]byte(toWrite)) }
// GetMetricTypeFromMap returns the wrapped metric type object func (c ConstTypeGetter) GetMetricTypeFromMap(metricName string) com_signalfx_metrics_protobuf.MetricType { return com_signalfx_metrics_protobuf.MetricType(c) }