github.com/m3db/m3@v1.5.0/src/query/api/v1/handler/json/write.go (about)

     1  // Copyright (c) 2018 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package json
    22  
    23  import (
    24  	"encoding/json"
    25  	"fmt"
    26  	"io/ioutil"
    27  	"net/http"
    28  
    29  	"github.com/m3db/m3/src/query/api/v1/options"
    30  	"github.com/m3db/m3/src/query/api/v1/route"
    31  	"github.com/m3db/m3/src/query/models"
    32  	"github.com/m3db/m3/src/query/storage"
    33  	"github.com/m3db/m3/src/query/storage/m3/storagemetadata"
    34  	"github.com/m3db/m3/src/query/ts"
    35  	"github.com/m3db/m3/src/query/util"
    36  	"github.com/m3db/m3/src/query/util/logging"
    37  	xerrors "github.com/m3db/m3/src/x/errors"
    38  	"github.com/m3db/m3/src/x/instrument"
    39  	xhttp "github.com/m3db/m3/src/x/net/http"
    40  	xtime "github.com/m3db/m3/src/x/time"
    41  
    42  	"go.uber.org/zap"
    43  )
    44  
    45  const (
    46  	// WriteJSONURL is the url for the write json handler
    47  	WriteJSONURL = route.Prefix + "/json/write"
    48  
    49  	// JSONWriteHTTPMethod is the HTTP method used with this resource.
    50  	JSONWriteHTTPMethod = http.MethodPost
    51  )
    52  
    53  // WriteJSONHandler represents a handler for the write json endpoint
    54  type WriteJSONHandler struct {
    55  	opts           options.HandlerOptions
    56  	store          storage.Storage
    57  	instrumentOpts instrument.Options
    58  }
    59  
    60  // NewWriteJSONHandler returns a new instance of handler.
    61  func NewWriteJSONHandler(opts options.HandlerOptions) http.Handler {
    62  	return &WriteJSONHandler{
    63  		opts:           opts,
    64  		store:          opts.Storage(),
    65  		instrumentOpts: opts.InstrumentOpts(),
    66  	}
    67  }
    68  
    69  // WriteQuery represents the write request from the user
    70  // NB(braskin): support only writing one datapoint for now
    71  // TODO: build this out to be a legitimate batched endpoint, change
    72  // Tags to take a list of tag structs
    73  type WriteQuery struct {
    74  	Tags      map[string]string `json:"tags" validate:"nonzero"`
    75  	Timestamp string            `json:"timestamp" validate:"nonzero"`
    76  	Value     float64           `json:"value" validate:"nonzero"`
    77  }
    78  
    79  func (h *WriteJSONHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    80  	req, rErr := parseRequest(r)
    81  	if rErr != nil {
    82  		xhttp.WriteError(w, rErr)
    83  		return
    84  	}
    85  
    86  	writeQuery, err := h.newWriteQuery(req)
    87  	if err != nil {
    88  		logger := logging.WithContext(r.Context(), h.instrumentOpts)
    89  		logger.Error("parsing error",
    90  			zap.String("remoteAddr", r.RemoteAddr),
    91  			zap.Error(err))
    92  		xhttp.WriteError(w, err)
    93  	}
    94  
    95  	if err := h.store.Write(r.Context(), writeQuery); err != nil {
    96  		logger := logging.WithContext(r.Context(), h.instrumentOpts)
    97  		logger.Error("write error",
    98  			zap.String("remoteAddr", r.RemoteAddr),
    99  			zap.Error(err))
   100  		xhttp.WriteError(w, err)
   101  	}
   102  }
   103  
   104  func (h *WriteJSONHandler) newWriteQuery(req *WriteQuery) (*storage.WriteQuery, error) {
   105  	parsedTime, err := util.ParseTimeString(req.Timestamp)
   106  	if err != nil {
   107  		return nil, err
   108  	}
   109  
   110  	tags := models.NewTags(len(req.Tags), h.opts.TagOptions())
   111  	for n, v := range req.Tags {
   112  		tags = tags.AddTag(models.Tag{Name: []byte(n), Value: []byte(v)})
   113  	}
   114  
   115  	return storage.NewWriteQuery(storage.WriteQueryOptions{
   116  		Tags: tags,
   117  		Datapoints: ts.Datapoints{
   118  			{
   119  				Timestamp: xtime.ToUnixNano(parsedTime),
   120  				Value:     req.Value,
   121  			},
   122  		},
   123  		Unit:       xtime.Millisecond,
   124  		Annotation: nil,
   125  		Attributes: storagemetadata.Attributes{
   126  			MetricsType: storagemetadata.UnaggregatedMetricsType,
   127  		},
   128  	})
   129  }
   130  
   131  func parseRequest(r *http.Request) (*WriteQuery, error) {
   132  	body := r.Body
   133  	if r.Body == nil {
   134  		return nil, xerrors.NewInvalidParamsError(fmt.Errorf("empty request body"))
   135  	}
   136  
   137  	defer body.Close()
   138  
   139  	js, err := ioutil.ReadAll(body)
   140  	if err != nil {
   141  		return nil, err
   142  	}
   143  
   144  	var writeQuery *WriteQuery
   145  	if err = json.Unmarshal(js, &writeQuery); err != nil {
   146  		return nil, xerrors.NewInvalidParamsError(err)
   147  	}
   148  
   149  	return writeQuery, nil
   150  }