github.com/m-lab/locate@v0.17.6/api/v2/api.go (about)

     1  // Package v2 defines the request API for the location service.
     2  //
     3  // While well provisioned, the M-Lab Platform is finite. On occasion, due to
     4  // peak usage, local service outages, or abnormal client behavior the location
     5  // service must decline to schedule new user requests. This is necesssary to
     6  // safegaurd measurement quality of your measurements and those of others. The
     7  // v2 API classifies user requests into three priorities.
     8  //
     9  //	API-key | Access Token | Priority
    10  //	--------------------------------------------------------
    11  //	YES     | YES          | API-Key, High Availability Pool
    12  //	YES     | NO           | API-Key, Best Effort Pool
    13  //	NO      | NO           | Global Best Effort Pool
    14  //
    15  // For highest priority access to the platform, register an API key and use the
    16  // NearestResult.NextRequest.URL when provided.
    17  package v2
    18  
    19  import "time"
    20  
    21  // NearestResult is returned by the location service in response to query
    22  // requests.
    23  type NearestResult struct {
    24  	// Error contains information about request failures.
    25  	Error *Error `json:"error,omitempty"`
    26  
    27  	// NextRequest defines the earliest time that a client should make a new
    28  	// request using the included URL.
    29  	//
    30  	// Under normal circumstances, NextRequest is provided *with* Results. The
    31  	// next request time is sampled from an exponential distribution such that
    32  	// inter-request times are memoryless. Under abnormal circumstances, such as
    33  	// high single-client request rates or target capacity exhaustion, the next
    34  	// request is provided *without* Results.
    35  	//
    36  	// Non-interactive or batch clients SHOULD schedule measurements with this
    37  	// value. All clients SHOULD NOT make additional requests more often than
    38  	// NextRequest. The server MAY reject requests indefinitely when clients do
    39  	// not respect this limit.
    40  	NextRequest *NextRequest `json:"next_request,omitempty"`
    41  
    42  	// Results contains an array of Targets matching the client request.
    43  	Results []Target `json:"results,omitempty"`
    44  }
    45  
    46  // MonitoringResult contains one Target with a single-purpose access-token
    47  // useful only for monitoring services on the target machine.
    48  type MonitoringResult struct {
    49  	// Error contains information about request failures.
    50  	Error *Error `json:"error,omitempty"`
    51  
    52  	// AccessToken is the access token used in Target URLs. Some applications
    53  	// may use this value instead of specific Target.URLs.
    54  	AccessToken string `json:"access_token"`
    55  
    56  	// Target contains service URLs for monitoring the service on the target
    57  	// machine.
    58  	// TODO (kinkade): Remove this field once all monitoring clients are using
    59  	// the Results field below.
    60  	Target *Target `json:"target,omitempty"`
    61  
    62  	// Results is array of Targets matching the client request. In the case of
    63  	// monitoring requests this array will only contain a single element, but we
    64  	// leave it as an array so that the response of monitoring requests matches
    65  	// the response of normal locate requests so that clients can treat data
    66  	// from either request the same.
    67  	Results []Target `json:"results,omitempty"`
    68  }
    69  
    70  // NextRequest contains a URL for scheduling the next request. The URL embeds an
    71  // access token that will be valid after `NotBefore`. The access token will
    72  // remain valid until it `Expires`. If a client uses an expired URL, the request
    73  // will be handled as if no access token were provided, i.e. using a lower
    74  // priority class.
    75  type NextRequest struct {
    76  	// NotBefore defines the time after which the URL will become valid. This
    77  	// value is the same time used in "nbf" field of the underlying JSON Web
    78  	// Token (JWT) claim. To show this equivalence, we use the same name.
    79  	NotBefore time.Time `json:"nbf"`
    80  
    81  	// Expires defines the time after which the URL will be invalid. Expires will
    82  	// always be greater than NotBefore. This value is the same time used in the
    83  	// "exp" field of the underlying JWT claim.
    84  	Expires time.Time `json:"exp"`
    85  
    86  	// URL should be used to make the next request to the location service.
    87  	URL string `json:"url"`
    88  }
    89  
    90  // Location contains metadata about the geographic location of a target machine.
    91  type Location struct {
    92  	City    string `json:"city"`
    93  	Country string `json:"country"`
    94  }
    95  
    96  // Target contains information needed to run a measurement to a measurement
    97  // service on a single M-Lab machine. Measurement services may support multiple
    98  // resources. A Target contains at least one measurement service resource in
    99  // URLs.
   100  type Target struct {
   101  	// Machine is the FQDN of the machine hosting the measurement service.
   102  	Machine string `json:"machine"`
   103  
   104  	// Hostname is the FQDN of the measurement service targeted in URLs.
   105  	Hostname string `json:"hostname"`
   106  
   107  	// Location contains metadata about the geographic location of the target machine.
   108  	Location *Location `json:"location,omitempty"`
   109  
   110  	// URLs contains measurement service resource names and the complete URL for
   111  	// running a measurement.
   112  	//
   113  	// A measurement service may support multiple resources (e.g. upload,
   114  	// download, etc). Each key is a resource name and the value is a complete
   115  	// URL with protocol, service name, port, and parameters fully specified.
   116  	URLs map[string]string `json:"urls"`
   117  }
   118  
   119  // Error describes an error condition that prevents the server from completing a
   120  // NearestResult.
   121  type Error struct {
   122  	// RFC7807 Fields for "Problem Details".
   123  	Type     string `json:"type"`
   124  	Title    string `json:"title"`
   125  	Status   int    `json:"status"`
   126  	Detail   string `json:"detail,omitempty"`
   127  	Instance string `json:"instance,omitempty"`
   128  }
   129  
   130  // NewError creates a new api Error for a NearestResult.
   131  func NewError(typ, title string, status int) *Error {
   132  	return &Error{
   133  		Type:   typ,
   134  		Title:  title,
   135  		Status: status,
   136  	}
   137  }
   138  
   139  // HeartbeatMessage contains pointers to structs of the types
   140  // of messages accepted by the heartbeat service.
   141  type HeartbeatMessage struct {
   142  	Health       *Health
   143  	Registration *Registration
   144  	Prometheus   *Prometheus
   145  }
   146  
   147  // Registration contains a set of identifying fields
   148  // for a server instance.
   149  type Registration struct {
   150  	City          string              // City (e.g., New York).
   151  	CountryCode   string              // Country code (e.g., US).
   152  	ContinentCode string              // Continent code (e.g., NA).
   153  	Experiment    string              // Experiment (e.g., ndt).
   154  	Hostname      string              // Fully qualified service hostname.
   155  	Latitude      float64             // Latitude.
   156  	Longitude     float64             // Longitude.
   157  	Machine       string              // Machine (e.g., mlab1).
   158  	Metro         string              // Metro (e.g., lga).
   159  	Project       string              // Project (e.g., mlab-sandbox).
   160  	Probability   float64             // Probability of picking site (e.g., 0.3).
   161  	Site          string              // Site (e.g.. lga01).
   162  	Type          string              // Machine type (e.g., physical, virtual).
   163  	Uplink        string              // Uplink capacity.
   164  	Services      map[string][]string // Mapping of service names.
   165  }
   166  
   167  // Health is the structure used by the heartbeat service
   168  // to report health updates.
   169  type Health struct {
   170  	Score float64 // Health score.
   171  }
   172  
   173  // Prometheus contains the health data reported by Prometheus.
   174  type Prometheus struct {
   175  	Health bool // Health (e.g., true = healthy).
   176  }