Skip to content

Commit

Permalink
Fixes APM trackEvent usage in NP context.
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonrhodes committed Jan 27, 2020
1 parent db3b578 commit d3eec8b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { isRumAgentName } from '../../../../../../common/agent_name';
import { ALL_OPTION_VALUE } from '../../../../../../common/agent_configuration_constants';
import { saveConfig } from './saveConfig';
import { useApmPluginContext } from '../../../../../hooks/useApmPluginContext';
import { useUiTracker } from '../../../../../../../../../plugins/infra/public';

const defaultSettings = {
TRANSACTION_SAMPLE_RATE: '1.0',
Expand All @@ -59,6 +60,9 @@ export function AddEditFlyout({

const callApmApiFromHook = useCallApmApi();

// get a telemetry UI event tracker
const trackApmEvent = useUiTracker({ app: 'apm' });

// config conditions (service)
const [serviceName, setServiceName] = useState<string>(
selectedConfig ? selectedConfig.service.name || ALL_OPTION_VALUE : ''
Expand Down Expand Up @@ -133,7 +137,8 @@ export function AddEditFlyout({
transactionMaxSpans,
configurationId: selectedConfig ? selectedConfig.id : undefined,
agentName,
toasts
toasts,
trackApmEvent
});
setIsSaving(false);
onSaved();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
getOptionLabel,
omitAllOption
} from '../../../../../../common/agent_configuration_constants';
import { UiTracker } from '../../../../../../../../../plugins/infra/public';

interface Settings {
transaction_sample_rate: number;
Expand All @@ -28,7 +29,8 @@ export async function saveConfig({
transactionMaxSpans,
configurationId,
agentName,
toasts
toasts,
trackEvent
}: {
callApmApi: APMClient;
serviceName: string;
Expand All @@ -39,9 +41,9 @@ export async function saveConfig({
configurationId?: string;
agentName?: string;
toasts: NotificationsStart['toasts'];
trackEvent: UiTracker;
}) {
// TODO: Migrate to useTrackMetric
// trackEvent({ app: 'apm', name: 'save_agent_configuration' });
trackEvent({ metric: 'save_agent_configuration' });

try {
const settings: Settings = {
Expand Down
12 changes: 10 additions & 2 deletions x-pack/plugins/infra/public/hooks/use_track_metric.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,24 @@ import { useKibana } from '../../../../../src/plugins/kibana_react/public';
type ObservabilityApp = 'infra_metrics' | 'infra_logs' | 'apm' | 'uptime';

interface TrackOptions {
app: ObservabilityApp;
app?: ObservabilityApp;
metricType?: UiStatsMetricType;
delay?: number; // in ms
}
type EffectDeps = unknown[];

type TrackMetricOptions = TrackOptions & { metric: string };
export type TrackMetricOptions = TrackOptions & { metric: string };
export type UiTracker = (options: TrackMetricOptions) => void;

export { METRIC_TYPE };

export function useUiTracker({ app: defaultApp }: { app?: ObservabilityApp } = {}) {
const { reportUiStats } = useKibana<any>().services?.usageCollection;
return ({ app = defaultApp, metric, metricType = METRIC_TYPE.COUNT }: TrackMetricOptions) => {
reportUiStats(app, metricType, metric);
};
}

export function useTrackMetric(
{ app, metric, metricType = METRIC_TYPE.COUNT, delay = 0 }: TrackMetricOptions,
effectDependencies: EffectDeps = []
Expand Down
8 changes: 7 additions & 1 deletion x-pack/plugins/infra/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ export const plugin: PluginInitializer<
return new Plugin(context);
};

export { useTrackPageview } from './hooks/use_track_metric';
export {
useTrackPageview,
useUiTracker,
UiTracker,
TrackMetricOptions,
METRIC_TYPE,
} from './hooks/use_track_metric';
export { FORMATTERS } from './utils/formatters';
export { InfraFormatterType } from './lib/lib';

0 comments on commit d3eec8b

Please sign in to comment.