Skip to content

Commit

Permalink
use client logger
Browse files Browse the repository at this point in the history
  • Loading branch information
Dosant committed Jan 20, 2025
1 parent 4dbad26 commit b7caf82
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import type { Logger } from '@kbn/logging';
import type { HttpStart } from '@kbn/core-http-browser';
import type {
ContentInsightsStats,
Expand All @@ -27,17 +28,21 @@ export interface ContentInsightsClientPublic {
* Client for the Content Management Insights service.
*/
export class ContentInsightsClient implements ContentInsightsClientPublic {
private logger: Logger;
constructor(
private readonly deps: { http: HttpStart },
private readonly deps: { http: HttpStart; logger: Logger },
private readonly config: { domainId: string }
) {}
) {
this.logger = deps.logger.get('content_insights_client');
}

track(id: string, eventType: ContentInsightsEventTypes) {
this.deps.http
.post(`/internal/content_management/insights/${this.config.domainId}/${id}/${eventType}`)
.catch((e) => {
// eslint-disable-next-line no-console
console.warn(`Could not track ${eventType} event for ${id}`, e);
this.logger.warn(`Could not track ${eventType} event for ${id}. Error: ${e?.message}`, {
error: e,
});
});
}

Expand Down
2 changes: 2 additions & 0 deletions src/platform/plugins/shared/dashboard/jest_setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ import {
mockDashboardContentManagementCache,
mockDashboardContentManagementService,
setStubKibanaServices,
setStubLogger,
} from './public/services/mocks';

setStubLogger();
// Start the kibana services with stubs
setStubKibanaServices();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { DashboardCreationOptions, DashboardState } from './types';
import { getDashboardApi } from './get_dashboard_api';
import { startQueryPerformanceTracking } from '../dashboard_container/embeddable/create/performance/query_performance_tracking';
import { coreServices } from '../services/kibana_services';
import { logger } from '../services/logger';
import {
PANELS_CONTROL_GROUP_KEY,
getDashboardBackupService,
Expand Down Expand Up @@ -123,7 +124,7 @@ export async function loadDashboardApi({
// however, there is an edge case that we now count a new view when a user is editing a dashboard and is returning from an editor by canceling
// TODO: this should be revisited by making embeddable transfer support canceling logic /~https://github.com/elastic/kibana/issues/190485
const contentInsightsClient = new ContentInsightsClient(
{ http: coreServices.http },
{ http: coreServices.http, logger },
{ domainId: 'dashboard' }
);
contentInsightsClient.track(savedObjectId, 'viewed');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { getDashboardBackupService } from '../../services/dashboard_backup_servi
import { getDashboardContentManagementService } from '../../services/dashboard_content_management_service';
import { getDashboardRecentlyAccessedService } from '../../services/dashboard_recently_accessed_service';
import { coreServices } from '../../services/kibana_services';
import { logger } from '../../services/logger';
import { getDashboardCapabilities } from '../../utils/get_dashboard_capabilities';
import {
dashboardListingErrorStrings,
Expand Down Expand Up @@ -341,7 +342,7 @@ export const useDashboardListingTable = ({
);

const contentInsightsClient = useMemo(
() => new ContentInsightsClient({ http: coreServices.http }, { domainId: 'dashboard' }),
() => new ContentInsightsClient({ http: coreServices.http, logger }, { domainId: 'dashboard' }),
[]
);

Expand Down
5 changes: 4 additions & 1 deletion src/platform/plugins/shared/dashboard/public/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ import {
} from './dashboard_container/panel_placement';
import type { FindDashboardsService } from './services/dashboard_content_management_service/types';
import { setKibanaServices, untilPluginStartServicesReady } from './services/kibana_services';
import { setLogger } from './services/logger';
import { registerActions } from './dashboard_actions/register_actions';

export interface DashboardFeatureFlagConfig {
Expand Down Expand Up @@ -156,7 +157,9 @@ export class DashboardPlugin
implements
Plugin<DashboardSetup, DashboardStart, DashboardSetupDependencies, DashboardStartDependencies>
{
constructor(private initializerContext: PluginInitializerContext) {}
constructor(private initializerContext: PluginInitializerContext) {
setLogger(initializerContext.logger.get('dashboard'));
}

private appStateUpdater = new BehaviorSubject<AppUpdater>(() => ({}));
private stopUrlTracking: (() => void) | undefined = undefined;
Expand Down
16 changes: 16 additions & 0 deletions src/platform/plugins/shared/dashboard/public/services/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import type { Logger } from '@kbn/logging';

export let logger: Logger;

export const setLogger = (_logger: Logger) => {
logger = _logger;
};
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { urlForwardingPluginMock } from '@kbn/url-forwarding-plugin/public/mocks
import { visualizationsPluginMock } from '@kbn/visualizations-plugin/public/mocks';

import { setKibanaServices } from './kibana_services';
import { setLogger } from './logger';
import { DashboardAttributes } from '../../server/content_management';
import { DashboardCapabilities } from '../../common';
import { LoadDashboardReturn } from './dashboard_content_management_service/types';
Expand Down Expand Up @@ -76,6 +77,10 @@ export const setStubKibanaServices = () => {
});
};

export const setStubLogger = () => {
setLogger(coreMock.createCoreContext().logger);
};

export const mockDashboardContentManagementService = {
loadDashboardState: jest.fn().mockImplementation(() =>
Promise.resolve({
Expand Down

0 comments on commit b7caf82

Please sign in to comment.