[Usage collection] Replace agent.type with hard coded value to categorize monitoring type correctly. (#216391)

## Summary
Collection type was set with `agent.type` which conflicts on stack and
metricbeat monitorings (same `metricbeat` value). For the stack
monitoring, elastic-agent internally utilizes filebat or metricbeat.
This change hard codes the `collection_types` corresponding on each
monitoring type to categorize monitoring type correctly.



### Checklist

Check the PR satisfies following conditions. 

Reviewers should verify this PR satisfies this list as well.

- ~~[ ] 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/src/platform/packages/shared/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
- ~~[ ] If a plugin configuration key changed, check if it needs to be
allowlisted in the cloud and added to the [docker
list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)~~
- ~~[ ] This was checked for breaking HTTP API changes, and any breaking
changes have been approved by the breaking-change committee. The
`release_note:breaking` label should be applied in these situations.~~
- ~~[ ] [Flaky Test
Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was
used on any tests changed~~
- [x] The PR description includes the appropriate Release Notes section,
and the correct `release_note:*` label is applied per the
[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)

### Identify risks

- [ ] 


### Example data
- when stack monitoring
```
{
    "clusterUuid": "WKhHGcv8Rt6nHwKRmKDdAw",
    "stats": {
      "collectionSource": "monitoring",
      "cacheDetails": {
        "updatedAt": "2025-03-31T23:07:32.154Z",
        "fetchedAt": "2025-03-31T23:07:33.349Z"
      },
      "cluster_name": "logstash",
      "timestamp": "2025-03-31T23:07:32Z",
      "version": "9.1.0",
      "cluster_uuid": "WKhHGcv8Rt6nHwKRmKDdAw",
      "stack_stats": {
        "logstash": {
          "versions": [
            {
              "version": "9.1.0",
              "count": 1
            }
          ],
          "count": 1,
          "cluster_stats": {
            "pipelines": {
              "count": 1,
              "batch_size_total": 125,
              "batch_size_max": 125,
              "batch_size_min": 125,
              "batch_size_avg": 125,
              "workers_total": 10,
              "workers_max": 10,
              "workers_min": 10,
              "workers_avg": 10,
              "sources": {
                "file": true
              }
            },
            "plugins": [
              {
                "name": "logstash-input-heartbeat",
                "count": 1
              },
              {
                "name": "logstash-filter-ruby",
                "count": 1
              },
              {
                "name": "logstash-filter-mutate",
                "count": 1
              },
              {
                "name": "logstash-output-stdout",
                "count": 1
              }
            ],
            "monitoringClusterUuid": "WKhHGcv8Rt6nHwKRmKDdAw",
            "collection_types": {
              "agent": 1
            },
            "queues": {
              "memory": 1
            }
          }
        }
      },
      "cluster_stats": {}
    }
  }
```

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Mashhur 2025-04-03 00:06:38 -07:00 committed by GitHub
parent 3c87833a0a
commit c4ad8cbe8c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 20 additions and 9 deletions

View file

@ -204,7 +204,7 @@ describe('LogstashAgentMonitoring', () => {
count: 1,
cluster_stats: {
collection_types: {
filebeat: 1,
agent: 1,
},
monitoringClusterUuid: 'FlV4ckTxQ0a78hmBkzzc9A',
pipelines: {
@ -288,7 +288,7 @@ describe('LogstashAgentMonitoring', () => {
count: 1,
cluster_stats: {
collection_types: {
filebeat: 1,
agent: 1,
},
monitoringClusterUuid: 'FlV4ckTxQ0a78hmBkzzc9A',
pipelines: {
@ -491,7 +491,7 @@ describe('LogstashAgentMonitoring', () => {
'1n1p': {
cluster_stats: {
collection_types: {
filebeat: 1,
agent: 1,
},
monitoringClusterUuid: 'monitoringClusterUuid',
pipelines: {
@ -523,7 +523,7 @@ describe('LogstashAgentMonitoring', () => {
'1nmp': {
cluster_stats: {
collection_types: {
filebeat: 2,
agent: 2,
},
monitoringClusterUuid: 'monitoringClusterUuid',
pipelines: {
@ -547,7 +547,7 @@ describe('LogstashAgentMonitoring', () => {
mnmp: {
cluster_stats: {
collection_types: {
filebeat: 3,
agent: 3,
},
monitoringClusterUuid: 'monitoringClusterUuid',
pipelines: {

View file

@ -120,7 +120,7 @@ export class LogstashAgentMonitoring implements LogstashMonitoring {
}
}
const thisCollectionType = hit._source?.agent?.type || 'agent';
const thisCollectionType = 'agent';
if (!Object.hasOwn(clusterStats, 'collection_types')) {
clusterStats.collection_types = {};
}

View file

@ -53,15 +53,17 @@ describe('LogstashMetricbeatMonitoring', () => {
});
test('should set and get indexPattern correctly', () => {
metricbeatMonitoring.setIndexPattern('legacy');
metricbeatMonitoring.setIndexPattern('metricbeat');
const indexPatternForLegacy = metricbeatMonitoring.getIndexPattern();
expect(indexPatternForLegacy.stats).toBe(INDEX_PATTERN_LOGSTASH_MONITORING);
expect(indexPatternForLegacy.state).toBe(INDEX_PATTERN_LOGSTASH_MONITORING);
expect(metricbeatMonitoring.getMonitoringType()).toBe('metricbeat');
metricbeatMonitoring.setIndexPattern('stack');
const indexPatternForStack = metricbeatMonitoring.getIndexPattern();
expect(indexPatternForStack.stats).toBe(INDEX_PATTERN_LOGSTASH_STACK_MONITORING_STATS);
expect(indexPatternForStack.state).toBe(INDEX_PATTERN_LOGSTASH_STACK_MONITORING_STATE);
expect(metricbeatMonitoring.getMonitoringType()).toBe('stack');
});
describe('Logstash metricbeat monitoring query test', () => {

View file

@ -33,6 +33,8 @@ export class LogstashMetricbeatMonitoring implements LogstashMonitoring {
stats: INDEX_PATTERN_LOGSTASH_STACK_MONITORING_STATS,
};
private monitoringType: string = 'metricbeat';
/*
* Call the function for fetching and summarizing Logstash metrics for Metricbeat monitoring
* @param {Object} callCluster - ES client
@ -67,6 +69,7 @@ export class LogstashMetricbeatMonitoring implements LogstashMonitoring {
* @param monitoringType - the monitoring type where metricbeat monitoring is intended.
*/
setIndexPattern(monitoringType: string) {
this.monitoringType = monitoringType;
if (monitoringType === 'stack') {
this.indexPattern.state = INDEX_PATTERN_LOGSTASH_STACK_MONITORING_STATE;
this.indexPattern.stats = INDEX_PATTERN_LOGSTASH_STACK_MONITORING_STATS;
@ -79,6 +82,11 @@ export class LogstashMetricbeatMonitoring implements LogstashMonitoring {
getIndexPattern(): { [key: string]: string } {
return this.indexPattern;
}
getMonitoringType(): string {
return this.monitoringType;
}
/*
* Update a clusters object with processed Logstash stats for metricbeat monitoring
* @param {Array} results - array of LogstashStats docs from ES
@ -117,7 +125,8 @@ export class LogstashMetricbeatMonitoring implements LogstashMonitoring {
incrementByKey(a, thisVersion);
clusters[clusterUuid].versions = mapToList(a, 'version');
const thisCollectionType = hit._source?.agent?.type || 'metricbeat';
// for stack_monitoring, agent internally uses metricbeat or filebeat
const thisCollectionType = this.getMonitoringType() === 'stack' ? 'agent' : 'metricbeat';
if (!Object.hasOwn(clusterStats, 'collection_types')) {
clusterStats.collection_types = {};
}

View file

@ -103,7 +103,7 @@ export class LogstashSelfMonitoring implements LogstashMonitoring {
clusters[clusterUuid].versions = mapToList(a, 'version');
// Internal Collection has no agent field, so default to 'internal_collection'
const thisCollectionType = hit._source?.agent?.type || 'internal_collection';
const thisCollectionType = 'internal_collection';
if (!Object.hasOwn(clusterStats, 'collection_types')) {
clusterStats.collection_types = {};
}