diff --git a/src/plugins/telemetry_collection_manager/server/plugin.ts b/src/plugins/telemetry_collection_manager/server/plugin.ts index c3db0ca39e6acf..6223d2c916bb59 100644 --- a/src/plugins/telemetry_collection_manager/server/plugin.ts +++ b/src/plugins/telemetry_collection_manager/server/plugin.ts @@ -37,7 +37,7 @@ import { UsageStatsPayload, StatsCollectionContext, } from './types'; - +import { isClusterOptedIn } from './util'; import { encryptTelemetry } from './encryption'; interface TelemetryCollectionPluginsDepsSetup { @@ -240,7 +240,7 @@ export class TelemetryCollectionManagerPlugin collection.licenseGetter(clustersDetails, statsCollectionConfig, context), ]); - return stats.map((stat) => { + const clustersUsage = stats.map((stat) => { const license = licenses[stat.cluster_uuid]; return { ...(license ? { license } : {}), @@ -248,5 +248,7 @@ export class TelemetryCollectionManagerPlugin collectionSource: collection.title, }; }); + + return clustersUsage.filter(isClusterOptedIn); } } diff --git a/src/plugins/telemetry_collection_manager/server/util.test.ts b/src/plugins/telemetry_collection_manager/server/util.test.ts new file mode 100644 index 00000000000000..ba5d999c3bf9a6 --- /dev/null +++ b/src/plugins/telemetry_collection_manager/server/util.test.ts @@ -0,0 +1,51 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { isClusterOptedIn } from './util'; + +const createMockClusterUsage = (plugins: any) => { + return { + stack_stats: { + kibana: { plugins }, + }, + }; +}; + +describe('isClusterOptedIn', () => { + it('returns true if cluster has opt_in_status: true', () => { + const mockClusterUsage = createMockClusterUsage({ telemetry: { opt_in_status: true } }); + const result = isClusterOptedIn(mockClusterUsage); + expect(result).toBe(true); + }); + it('returns false if cluster has opt_in_status: false', () => { + const mockClusterUsage = createMockClusterUsage({ telemetry: { opt_in_status: false } }); + const result = isClusterOptedIn(mockClusterUsage); + expect(result).toBe(false); + }); + it('returns false if cluster has opt_in_status: undefined', () => { + const mockClusterUsage = createMockClusterUsage({ telemetry: {} }); + const result = isClusterOptedIn(mockClusterUsage); + expect(result).toBe(false); + }); + it('returns false if cluster stats is malformed', () => { + expect(isClusterOptedIn(createMockClusterUsage({}))).toBe(false); + expect(isClusterOptedIn({})).toBe(false); + expect(isClusterOptedIn(undefined)).toBe(false); + }); +}); diff --git a/src/plugins/telemetry_collection_manager/server/util.ts b/src/plugins/telemetry_collection_manager/server/util.ts new file mode 100644 index 00000000000000..d6e1b516636885 --- /dev/null +++ b/src/plugins/telemetry_collection_manager/server/util.ts @@ -0,0 +1,22 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +export const isClusterOptedIn = (clusterUsage: any): boolean => { + return clusterUsage?.stack_stats?.kibana?.plugins?.telemetry?.opt_in_status === true; +};