def test_write(monkeypatch): # Given mock_influxdb_client = mock.MagicMock() monkeypatch.setattr(analytics.influxdb_wrapper, "influxdb_client", mock_influxdb_client) mock_write_api = mock.MagicMock() mock_influxdb_client.write_api.return_value = mock_write_api influxdb = InfluxDBWrapper("name", "field_name", "field_value") # When influxdb.write() # Then mock_write_api.write.assert_called()
def track_request_influxdb(request): """ Sends API event data to InfluxDB :param request: (HttpRequest) the request being made """ resource = get_resource_from_uri(request.path) if resource and resource in TRACKED_RESOURCE_ACTIONS: environment = Environment.get_from_cache(request.headers.get('X-Environment-Key')) tags = { "resource": resource, "organisation": environment.project.organisation.get_unique_slug(), "organisation_id": environment.project.organisation_id, "project": environment.project.name, "project_id": environment.project_id } influxdb = InfluxDBWrapper("api_call", "request_count", 1, tags=tags) influxdb.write()
def track_feature_evaluation_influxdb(environment_id, feature_evaluations): """ Sends Feature analytics event data to InfluxDB :param environment_id: (int) the id of the environment the feature is being evaluated within :param feature_evaluations: (dict) A collection of key id / evaluation counts """ influxdb = InfluxDBWrapper("feature_evaluation") for feature_id, evaluation_count in feature_evaluations.items(): tags = {"feature_id": feature_id, "environment_id": environment_id} influxdb.add_data_point("request_count", evaluation_count, tags=tags) influxdb.write()