# Luna Streams v.1.1.15 documentation

## Monitoring

## Data for monitoring

Now we monitor two types of events for monitoring: request and error. First type is all requests, second is failed requests only. Every event is a point in the time series. The point is represented as union of the following data:

> - series name (now requests and errors)

> - start request time

> - tags, indexed data in storage, dictionary: keys - string tag names, values - string, integer, float

> - fields, non indexed data in storage, dictionary: keys - string tag names, values - string, integer, float

‘Requests’ series. Triggered on every request. Each point contains a data about corresponding request (execution time and etc).

> - tags

>   > tag name | description
>   > --- | ---
>   > service | always “luna-streams”
>   > route | concatenation of a request method and a request resource (POST:/streams)
>   > status_code | http status code of response

> - fields

>   > fields | description
>   > --- | ---
>   > request_id | request id
>   > execution_time | request execution time

‘Errors’ series. Triggered on failed request. Each point contains error_code of luna error.

> - tags

>   > tag name | description
>   > --- | ---
>   > service | always “luna-streams”
>   > route | concatenation of a request method and a request resource (POST:/streams)
>   > status_code | http status code of response
>   > error_code | luna error code

> - fields

>   > fields | description
>   > --- | ---
>   > request_id | request id

‘Licensing’ series. Triggered when the service starts and every 60 seconds. Each point contains license check data.

> - tags

>   > tag name | description
>   > service | always “luna-streams”
>   > license_status | license status (“ok”, “warning”, “error”, “exception”)

> - fields

>   > fields | description
>   > license_streams_limit_rate | the percentage of used streams
>   > warnings | license warning messages
>   > errors | license error messages

## Database

Monitoring is implemented as data sending to an influx database. You can setup your database credentials in configuration file in section “monitoring”.

## Classes

Module contains points for monitoring.

class luna_streams.crutches_on_wheels.cow.monitoring.points.BaseMonitoringPoint(eventTime)

  Abstract class for points

  eventTime

    event time as timestamp

    Type:

      float

  abstract property fields: Dict[str, int | float | str]

    Get fields from point. We supposed that fields are not indexing data

    Returns:

      dict with fields.

  abstract property tags: Dict[str, int | float | str]

    Get tags from point. We supposed that tags are indexing data

    Returns:

      dict with tags.

class luna_streams.crutches_on_wheels.cow.monitoring.points.BaseRequestMonitoringPoint(requestId, resource, method, requestTime, service, statusCode)

  Base class for point which is associated with requests.

  requestId

    request id

    Type:

      str

  route

    concatenation of a request method and a request resource

    Type:

      str

  service

    service name

    Type:

      str

  statusCode

    status code of a request response.

    Type:

      int

  property fields: Dict[str, int | float | str]

    Get fields

    Returns:

      “request_id”

    Return type:

      dict with following keys

  property tags: Dict[str, int | float | str]

    Get tags

    Returns:

      “route”, “service”, “status_code”

    Return type:

      dict with following keys

class luna_streams.crutches_on_wheels.cow.monitoring.points.DataForMonitoring(tags=<factory>, fields=<factory>)

  Class fo storing an additional data for monitoring.

class luna_streams.crutches_on_wheels.cow.monitoring.points.InfluxFormatter

  Format any point filed into inline format

class luna_streams.crutches_on_wheels.cow.monitoring.points.MonitoringPointInfluxFormatBuilder(name, bases, namespace, /, **kwargs)

  Complement point class with explicit fields formatting function for the sake of better performance

  To perform type format building target class must have ‘fields’ property return value annotated with TypedDict.

  Target class might be configured via ‘Config’ class. Available options:

  > extraFields: whether class should handle additional fields or not

      >>> from typing import TypedDict
      >>>
      >>> class MonitoringFields(TypedDict):
      ...     field1: str
      ...     field2: int
      ...     field3: float
      ...     field4: bool
      >>>
      >>> class BasePoint(BaseMonitoringPoint, metaclass=MonitoringPointInfluxFormatBuilder):
      ...
      ...     def __init__(self, fields: dict):
      ...         self._fields = fields
      ...
      ...     @property
      ...     def tags(self):
      ...         return {}
      ...
      ...     @property
      ...     def fields(self) -> MonitoringFields:
      ...         return self._fields
      ...
      >>> class TestPointNoExtra(BasePoint):
      ...
      ...     class Config:
      ...         extraFields = False
      ...
      >>> class TestPointWithExtra(BasePoint):
      ...     class Config:
      ...         extraFields = True
      >>>
      >>>
      >>> point1 = TestPointNoExtra({"field1": "data", "field2": 1, "field3": 1.0, "field4": False})
      >>> point2 = TestPointWithExtra({"field1": "data", "field2": 1, "field3": 1.0, "field4": False, "extra": True})
      >>> print(point1.convertFieldsToInfluxLineProtocol())
      field1="data",field2=1i,field3=1.000000,field4=False
      >>> print(point2.convertFieldsToInfluxLineProtocol())
      field1="data",field2=1i,field3=1.000000,field4=False,extra=True

  classmethod buildInfluxFormats(annotations, extraFields)

    Build map with influx formats for corresponding fields

    Parameters:

      - annotations (dict) – point fields type annotations

      - extraFields (bool) – whether point uses extra fields or not

    Return type:

      `dict`

    Returns:

      dict of fields with their format

  static convertFieldsToInfluxLineProtocolNoExtra(point)

    Convert point fields into influx line protocol format without extra fields

  static convertFieldsToInfluxLineProtocolWithExtra(point)

    Convert point fields into influx line protocol format with extra fields

  static getTypeFormat(_type, _field, extraFields)

    Get field type format

    Parameters:

      - _type (type) – field type

      - _field (str) – field name

      - extraFields (bool) – whether point uses extra fields or not

    Return type:

      `str`

    Returns:

      string format of the field

class luna_streams.crutches_on_wheels.cow.monitoring.points.RequestErrorMonitoringPoint(requestId, resource, method, errorCode, service, requestTime, statusCode, additionalTags=None, additionalFields=None)

  Request monitoring point is suspended for monitoring requests errors (error codes)

  errorCode

    error code

    Type:

      int

  additionalTags

    additional tags which was specified for the request

    Type:

      dict

  additionalFields

    additional fields which was specified for the request

    Type:

      dict

  property fields: Dict[str, int | float | str]

    Get fields.

    Returns:

      dict with base fields and additional tags

  series: `str` = 'errors'

    series “errors”

  property tags: Dict[str, int | float | str]

    Get tags.

    Returns:

      dict with base tags, “error_code” and additional tags

class luna_streams.crutches_on_wheels.cow.monitoring.points.RequestMonitoringPoint(requestId, resource, method, executionTime, requestTime, service, statusCode, additionalTags=None, additionalFields=None)

  Request monitoring point is suspended for monitoring all requests and measure a request time etc.

  executionTime

    execution time

    Type:

      float

  additionalTags

    additional tags which was specified for the request

    Type:

      dict

  additionalFields

    additional fields which was specified for the request

    Type:

      dict

  property fields: Dict[str, int | float | str]

    Get fields.

    Returns:

      dict with base fields, “execution_time” and additional tags

  series: `str` = 'requests'

    series “request”

  property tags: Dict[str, int | float | str]

    Get tags.

    Returns:

      dict with base tags and additional tags

luna_streams.crutches_on_wheels.cow.monitoring.points.getRoute(resource, method)

  Get a request route, concatenation of a request method and a request resource :type resource: `str` :param resource: resource :type method: `str` :param method: method

  Returns:

    {resource}”

  Return type:

    “{method}

luna_streams.crutches_on_wheels.cow.monitoring.points.monitorTime(monitoringData, fieldName)

  Context manager for timing execution time.

  Parameters:

    - monitoringData (`DataForMonitoring`) – container for saving result

    - fieldName (`str`) – field name

Module contains classes for sending a data to an influx monitoring.

class luna_streams.crutches_on_wheels.cow.monitoring.influx_adapter.InfluxMonitoringAdapter(settings, flushingPeriod)

  Influx 2.x adaptor. Suspended to send points to an influxdb

  bucket

    influx bucket name

    Type:

      str

  addPointsToBuffer(points)

    Add points to buffer.

    Parameters:

      points (`Iterable`[`BaseMonitoringPoint`]) – points

    Return type:

      `None`

  clearBuffer()

    Clear monitoring buffer

    Return type:

      `list`[`str`]

  static convertFieldsToInfluxLineProtocol(fields)

    Convert field value to influx line protocol format

    Parameters:

      fields (`dict`) – dict with values to convert

    Return type:

      `str`

    Returns:

      line protocol string

  generatePointStr(point)

    Generate string from point

    Parameters:

      point (`BaseMonitoringPoint`) – point

    Return type:

      `str`

    Returns:

      influx line protocol string

  async stopMonitoring()

    Stop monitoring (cancel all request and stop getting new).

    Return type:

      `None`

class luna_streams.crutches_on_wheels.cow.monitoring.influx_adapter.InfluxSettings(url, bucket, organization, token)

  Container for influx 2.x settings