Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add stats api #3

Merged

Conversation

angorayc
Copy link
Collaborator

@angorayc angorayc commented Dec 7, 2022

Summary

This is a part of https://github.com/elastic/security-team/issues/5578

Request:

GET /internal/data_quality/stats/{indexPattern}

example: GET /internal/data_quality/stats/auditbeat-*

Response:

{
    "auditbeat-7.15.1-2023.01.04-000001": {
        "uuid": "P-edg4RUTgW56FXS0b9U9A",
        "health": "yellow",
        "status": "open",
        "primaries": {
            "docs": {
                "count": 11241,
                "deleted": 0
            },
            "shard_stats": {
                "total_count": 1
            },
            "store": {
                "size_in_bytes": 7979125,
                "total_data_set_size_in_bytes": 7979125,
                "reserved_in_bytes": 0
            },
            "indexing": {
                "index_total": 11241,
                "index_time_in_millis": 22171,
                "index_current": 0,
                "index_failed": 0,
                "delete_total": 0,
                "delete_time_in_millis": 0,
                "delete_current": 0,
                "noop_update_total": 0,
                "is_throttled": false,
                "throttle_time_in_millis": 0,
                "write_load": 0.0003157622208929533
            },
            "get": {
                "total": 0,
                "time_in_millis": 0,
                "exists_total": 0,
                "exists_time_in_millis": 0,
                "missing_total": 0,
                "missing_time_in_millis": 0,
                "current": 0
            },
            "search": {
                "open_contexts": 0,
                "query_total": 8,
                "query_time_in_millis": 16,
                "query_current": 0,
                "fetch_total": 8,
                "fetch_time_in_millis": 0,
                "fetch_current": 0,
                "scroll_total": 0,
                "scroll_time_in_millis": 0,
                "scroll_current": 0,
                "suggest_total": 0,
                "suggest_time_in_millis": 0,
                "suggest_current": 0
            },
            "merges": {
                "current": 0,
                "current_docs": 0,
                "current_size_in_bytes": 0,
                "total": 419,
                "total_time_in_millis": 32943,
                "total_docs": 130086,
                "total_size_in_bytes": 265993970,
                "total_stopped_time_in_millis": 0,
                "total_throttled_time_in_millis": 0,
                "total_auto_throttle_in_bytes": 20971520
            },
            "refresh": {
                "total": 3979,
                "total_time_in_millis": 187342,
                "external_total": 3979,
                "external_total_time_in_millis": 205442,
                "listeners": 0
            },
            "flush": {
                "total": 0,
                "periodic": 0,
                "total_time_in_millis": 0
            },
            "warmer": {
                "current": 0,
                "total": 3978,
                "total_time_in_millis": 3150
            },
            "query_cache": {
                "memory_size_in_bytes": 0,
                "total_count": 0,
                "hit_count": 0,
                "miss_count": 0,
                "cache_size": 0,
                "cache_count": 0,
                "evictions": 0
            },
            "fielddata": {
                "memory_size_in_bytes": 0,
                "evictions": 0
            },
            "completion": {
                "size_in_bytes": 0
            },
            "segments": {
                "count": 3,
                "memory_in_bytes": 0,
                "terms_memory_in_bytes": 0,
                "stored_fields_memory_in_bytes": 0,
                "term_vectors_memory_in_bytes": 0,
                "norms_memory_in_bytes": 0,
                "points_memory_in_bytes": 0,
                "doc_values_memory_in_bytes": 0,
                "index_writer_memory_in_bytes": 0,
                "version_map_memory_in_bytes": 0,
                "fixed_bit_set_memory_in_bytes": 1536,
                "max_unsafe_auto_id_timestamp": -1,
                "file_sizes": {}
            },
            "translog": {
                "operations": 11241,
                "size_in_bytes": 24823633,
                "uncommitted_operations": 11241,
                "uncommitted_size_in_bytes": 24823633,
                "earliest_last_modified_age": 17548
            },
            "request_cache": {
                "memory_size_in_bytes": 0,
                "evictions": 0,
                "hit_count": 0,
                "miss_count": 8
            },
            "recovery": {
                "current_as_source": 0,
                "current_as_target": 0,
                "throttle_time_in_millis": 0
            },
            "bulk": {
                "total_operations": 3598,
                "total_time_in_millis": 22241,
                "total_size_in_bytes": 24081672,
                "avg_time_in_millis": 2,
                "avg_size_in_bytes": 6376
            }
        },
        "total": {
            "docs": {
                "count": 11241,
                "deleted": 0
            },
            "shard_stats": {
                "total_count": 1
            },
            "store": {
                "size_in_bytes": 7979125,
                "total_data_set_size_in_bytes": 7979125,
                "reserved_in_bytes": 0
            },
            "indexing": {
                "index_total": 11241,
                "index_time_in_millis": 22171,
                "index_current": 0,
                "index_failed": 0,
                "delete_total": 0,
                "delete_time_in_millis": 0,
                "delete_current": 0,
                "noop_update_total": 0,
                "is_throttled": false,
                "throttle_time_in_millis": 0,
                "write_load": 0.0003157622208929533
            },
            "get": {
                "total": 0,
                "time_in_millis": 0,
                "exists_total": 0,
                "exists_time_in_millis": 0,
                "missing_total": 0,
                "missing_time_in_millis": 0,
                "current": 0
            },
            "search": {
                "open_contexts": 0,
                "query_total": 8,
                "query_time_in_millis": 16,
                "query_current": 0,
                "fetch_total": 8,
                "fetch_time_in_millis": 0,
                "fetch_current": 0,
                "scroll_total": 0,
                "scroll_time_in_millis": 0,
                "scroll_current": 0,
                "suggest_total": 0,
                "suggest_time_in_millis": 0,
                "suggest_current": 0
            },
            "merges": {
                "current": 0,
                "current_docs": 0,
                "current_size_in_bytes": 0,
                "total": 419,
                "total_time_in_millis": 32943,
                "total_docs": 130086,
                "total_size_in_bytes": 265993970,
                "total_stopped_time_in_millis": 0,
                "total_throttled_time_in_millis": 0,
                "total_auto_throttle_in_bytes": 20971520
            },
            "refresh": {
                "total": 3979,
                "total_time_in_millis": 187342,
                "external_total": 3979,
                "external_total_time_in_millis": 205442,
                "listeners": 0
            },
            "flush": {
                "total": 0,
                "periodic": 0,
                "total_time_in_millis": 0
            },
            "warmer": {
                "current": 0,
                "total": 3978,
                "total_time_in_millis": 3150
            },
            "query_cache": {
                "memory_size_in_bytes": 0,
                "total_count": 0,
                "hit_count": 0,
                "miss_count": 0,
                "cache_size": 0,
                "cache_count": 0,
                "evictions": 0
            },
            "fielddata": {
                "memory_size_in_bytes": 0,
                "evictions": 0
            },
            "completion": {
                "size_in_bytes": 0
            },
            "segments": {
                "count": 3,
                "memory_in_bytes": 0,
                "terms_memory_in_bytes": 0,
                "stored_fields_memory_in_bytes": 0,
                "term_vectors_memory_in_bytes": 0,
                "norms_memory_in_bytes": 0,
                "points_memory_in_bytes": 0,
                "doc_values_memory_in_bytes": 0,
                "index_writer_memory_in_bytes": 0,
                "version_map_memory_in_bytes": 0,
                "fixed_bit_set_memory_in_bytes": 1536,
                "max_unsafe_auto_id_timestamp": -1,
                "file_sizes": {}
            },
            "translog": {
                "operations": 11241,
                "size_in_bytes": 24823633,
                "uncommitted_operations": 11241,
                "uncommitted_size_in_bytes": 24823633,
                "earliest_last_modified_age": 17548
            },
            "request_cache": {
                "memory_size_in_bytes": 0,
                "evictions": 0,
                "hit_count": 0,
                "miss_count": 8
            },
            "recovery": {
                "current_as_source": 0,
                "current_as_target": 0,
                "throttle_time_in_millis": 0
            },
            "bulk": {
                "total_operations": 3598,
                "total_time_in_millis": 22241,
                "total_size_in_bytes": 24081672,
                "avg_time_in_millis": 2,
                "avg_size_in_bytes": 6376
            }
        }
    }
}

constructor(private response: KibanaResponseFactory) {}

error<T>({ statusCode, body, headers }: CustomHttpResponseOptions<T>) {
// KibanaResponse is not exported so we cannot use a return type here and that is why the linter is turned off above
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider removing this comment if it's no longer applicable

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when running the following command:

node scripts/type_check.js --project x-pack/plugins/data_quality/tsconfig.json

the following type error is reported:

[bazel] packages/kbn-securitysolution-es-utils/src/build_response/index.ts:9:71 - error TS2307: Cannot find module '@kbn/core/server' or its corresponding type declarations.
[bazel]
[bazel] 9 import type { CustomHttpResponseOptions, KibanaResponseFactory } from '@kbn/core/server';
[bazel]                                                                         ~~~~~~~~~~~~~~~~~~
[bazel]
[bazel] Found 1 error in packages/kbn-securitysolution-es-utils/src/build_response/index.ts:9

path: GET_INDEX_MAPPINGS,
validate: {
params: schema.object({
index_name: schema.string(),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our current plan (in the Data Quality dashboard) is to only request a specific index name, e.g. .ds-logs-endpoint.alerts-default-2022.11.17-000001 from this route, but given that the underlying ES mappings API also accepts a pattern, e.g. .ds-logs-*, consider renaming the request.params.index_name parameter to request.params.pattern, to align the parameter name with the full capabilities of the API.

path: GET_INDEX_STATS,
validate: {
params: schema.object({
index_name: schema.string(),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider renaming the request.params.index_name parameter to request.params.pattern, because the Data Quality Dashboard will provide (as input to this route) a pattern like logs-*, to enumerate all the indexes that match the specified pattern.

const { client } = (await context.core).elasticsearch;
const decodedIndexName = decodeURIComponent(request.params.index_name);

const mappings = await fetchStats(client, decodedIndexName);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider renaming mappings to stats

const mappings = await fetchStats(client, decodedIndexName);

return response.ok({
body: mappings.indices,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The body returned here is of type Record<string, IndicesStatsIndicesStats>. The IndicesStatsIndicesStats type may be imported via:

import type { IndicesStatsIndicesStats } from '@elastic/elasticsearch/lib/api/types';

Would it be reasonable to use io-ts here to verify the shape of the API response matches the type above?

If yes, would it be reasonable to create a common io-ts validation code that could be used both here (in the server), and on the client?

const mappings = await fetchMappings(client, decodedIndexName);

return response.ok({
body: mappings,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The body returned here is of type Record<string, IndicesGetMappingIndexMappingRecord>. The IndicesGetMappingIndexMappingRecord type may be imported via:

import type { IndicesGetMappingIndexMappingRecord } from '@elastic/elasticsearch/lib/api/types';

Would it be reasonable to use io-ts here to verify the shape of the API response matches the type above?

If yes, would it be reasonable to create a common io-ts validation code that could be used both here (in the server), and on the client?

Copy link
Owner

@andrew-goldstein andrew-goldstein left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @angorayc for implementing these APIs 🙏

Running a local copy of this branch, I integrated with both the stats and mappings APIs, and everything worked perfectly!

LGTM 🚀

@angorayc
Copy link
Collaborator Author

Thanks for the feedback Andrew @andrew-goldstein , I've done the renaming and added some basic input validation with io-ts. I haven't done the io-ts validation for api responses, is it ok if I do that in a following pr?

The other issue I have is the type issue, somehow the type check just doesn't like this import, but this is what other places doing. I'm still trying to figure this out.
import type { CustomHttpResponseOptions, KibanaResponseFactory } from '@kbn/core-http-server';

@angorayc
Copy link
Collaborator Author

Update: After adding "//packages/core/http/core-http-server:npm_module_types" to TYPES_DEPS in packages/kbn-securitysolution-es-utils/BUILD.bazel, the type issues should be fixed.

@angorayc angorayc merged commit d97694f into andrew-goldstein:data-quality-dashboard Dec 12, 2022
andrew-goldstein pushed a commit that referenced this pull request Jan 3, 2023
## Summary

Related to elastic#144161

Found that on a bulk update tags task failure, the task didn't stop
after 3 retries (should be over in less then a minute), the retries kept
happening for 2 hours.
This change removes the retry task if 3 retries are reached.

Also testing in cloud deployment to see if the tags error can be
reproduced with this fix.
I could reproduce the reported error locally, and seeing it goes away
with this fix.

To verify:
- Add at least 50k agents with the `create_agents` script in kibana repo
- open Kibana, select the 50k agents, and open Actions / Add tags
- Try this in a few seconds: add 2 new tags, and remove one of them
- Wait about 30s, the agents should reflect the changes
- Check the logs to see that the tasks are removed after 3rd retry is
reached or successful.
- Check that there are no more running tasks. Any running task can be
found in Kibana Console by running this query: `GET
.kibana_task_manager/_search?q=task.taskType:"fleet:update_agent_tags:retry"`

Locally simulated an error to test that the retry (and check) task is
removed:

```
[2022-12-07T15:52:16.415+01:00][ERROR][plugins.fleet] Retry #3 of task fleet:update_agent_tags:retry:848984ab-c11d-4ebe-8d1f-606143dd656b failed: failing task
[2022-12-07T15:52:16.416+01:00][WARN ][plugins.fleet] Stopping after 3rd retry. Error: failing task
[2022-12-07T15:52:16.416+01:00][INFO ][plugins.fleet] Removing task fleet:update_agent_tags:retry:check:848984ab-c11d-4ebe-8d1f-606143dd656b
[2022-12-07T15:52:16.416+01:00][INFO ][plugins.fleet] Removing task fleet:update_agent_tags:retry:848984ab-c11d-4ebe-8d1f-606143dd656b
```
andrew-goldstein pushed a commit that referenced this pull request Aug 13, 2024
## Summary

Resolves elastic#143905. This PR adds support for integration-level outputs.
This means that different integrations within the same agent policy can
now be configured to send data to different locations. This feature is
gated behind `enterprise` level subscription.

For each input, the agent policy will configure sending data to the
following outputs in decreasing order of priority:
1. Output set specifically on the integration policy
2. Output set specifically on the integration's parent agent policy
(including the case where an integration policy belongs to multiple
agent policies)
3. Global default data output set via Fleet Settings

Integration-level outputs will respect the same rules as agent
policy-level outputs:
- Certain integrations are disallowed from using certain output types,
attempting to add them to each other via creation, updating, or
"defaulting", will fail
- `fleet-server`, `synthetics`, and `apm` can only use same-cluster
Elasticsearch output
- When an output is deleted, any integrations that were specifically
using it will "clear" their output configuration and revert back to
either `#2` or `#3` in the above list
- When an output is edited, all agent policies across all spaces that
use it will be bumped to a new revision, this includes:
- Agent policies that have that output specifically set in their
settings (existing behavior)
- Agent policies that contain integrations which specifically has that
output set (new behavior)
- When a proxy is edited, the same new revision bump above will apply
for any outputs using that proxy

The final agent policy YAML that is generated will have:
- `outputs` block that includes:
- Data and monitoring outputs set at the agent policy level (existing
behavior)
- Any additional outputs set at the integration level, if they differ
from the above
- `outputs_permissions` block that includes permissions for each
Elasticsearch output depending on which integrations and/or agent
monitoring are assigned to it

Integration policies table now includes `Output` column. If the output
is defaulting to agent policy-level output, or global setting output, a
tooltip is shown:

<img width="1392" alt="image"
src="https://github.com/user-attachments/assets/5534716b-49b5-402a-aa4a-4ba6533e0ca8">

Configuring an integration-level output is done under Advanced options
in the policy editor. Setting to the blank value will "clear" the output
configuration. The list of available outputs is filtered by what outputs
are available for that integration (see above):

<img width="799" alt="image"
src="https://github.com/user-attachments/assets/617af6f4-e8f8-40b1-b476-848f8ac96e76">

An example of failure: ES output cannot be changed to Kafka while there
is an integration
<img width="1289" alt="image"
src="https://github.com/user-attachments/assets/11847eb5-fd5d-4271-8464-983d7ab39218">


## TODO
- [x] Adjust side effects of editing/deleting output when policies use
it across different spaces
- [x] Add API integration tests
- [x] Update OpenAPI spec
- [x] Create doc issue

### Checklist

Delete any items that are not applicable to this PR.

- [x] Any text added follows [EUI's writing
guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses
sentence case text and includes [i18n
support](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)
- [ ]
[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)
was added for features that require explanation or tutorials
- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios

---------

Co-authored-by: kibanamachine <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants