From efdf80498926a83ef3da8ec8e5b37343d20f9000 Mon Sep 17 00:00:00 2001 From: "opensearch-trigger-bot[bot]" <98922864+opensearch-trigger-bot[bot]@users.noreply.github.com> Date: Tue, 22 Oct 2024 13:19:19 -0700 Subject: [PATCH] Fixes span to logs redireciton, updates mds label when undefined (#2225) (#2226) * fixes span to logs redireciton, updates mds label when undefined * add time stamp note * fix endTime in service redirection * changed default starttime to 3 years ago --------- (cherry picked from commit f9ac8d0aa263cafb34599dc15496ea38c8f4d749) Signed-off-by: Shenoy Pratik Signed-off-by: github-actions[bot] Co-authored-by: github-actions[bot] Co-authored-by: Adam Tackett <105462877+TackAdam@users.noreply.github.com> --- .../components/services/service_view.tsx | 30 +++++++++----- .../components/traces/span_detail_flyout.tsx | 40 ++++++++++++++----- public/components/trace_analytics/home.tsx | 19 ++++++++- 3 files changed, 69 insertions(+), 20 deletions(-) diff --git a/public/components/trace_analytics/components/services/service_view.tsx b/public/components/trace_analytics/components/services/service_view.tsx index fbac328a5..a7e377ab4 100644 --- a/public/components/trace_analytics/components/services/service_view.tsx +++ b/public/components/trace_analytics/components/services/service_view.tsx @@ -4,6 +4,7 @@ */ /* eslint-disable react-hooks/exhaustive-deps */ +import dateMath from '@elastic/datemath'; import { EuiBadge, EuiContextMenu, @@ -28,6 +29,12 @@ import _ from 'lodash'; import React, { useEffect, useMemo, useState } from 'react'; import { DataSourceManagementPluginSetup } from '../../../../../../../src/plugins/data_source_management/public'; import { DataSourceOption } from '../../../../../../../src/plugins/data_source_management/public/components/data_source_menu/types'; +import { + DEFAULT_DATA_SOURCE_NAME, + DEFAULT_DATA_SOURCE_TYPE, +} from '../../../../../common/constants/data_sources'; +import { observabilityLogsID } from '../../../../../common/constants/shared'; +import { TRACE_ANALYTICS_DATE_FORMAT } from '../../../../../common/constants/trace_analytics'; import { setNavBreadCrumbs } from '../../../../../common/utils/set_nav_bread_crumbs'; import { coreRefs } from '../../../../framework/core_refs'; import { HeaderControlledComponentsWrapper } from '../../../../plugin_helpers/plugin_headerControl'; @@ -48,11 +55,6 @@ import { SearchBarProps, renderDatePicker } from '../common/search_bar'; import { SpanDetailFlyout } from '../traces/span_detail_flyout'; import { SpanDetailTable } from '../traces/span_detail_table'; import { ServiceMetrics } from './service_metrics'; -import { - DEFAULT_DATA_SOURCE_NAME, - DEFAULT_DATA_SOURCE_TYPE, -} from '../../../../../common/constants/data_sources'; -import { observabilityLogsID } from '../../../../../common/constants/shared'; interface ServiceViewProps extends TraceAnalyticsComponentDeps { serviceName: string; @@ -74,7 +76,6 @@ export function ServiceView(props: ServiceViewProps) { const [redirect, setRedirect] = useState(false); const [actionsMenuPopover, setActionsMenuPopover] = useState(false); - const isNewNavEnabled = coreRefs?.chrome?.navGroup?.getNavGroupEnabled(); const refresh = () => { const DSL = filtersToDsl( mode, @@ -178,11 +179,20 @@ export function ServiceView(props: ServiceViewProps) { name: 'View logs', 'data-test-subj': 'viewLogsButton', onClick: () => { - if (isNewNavEnabled) { + // NOTE: Discover has issue with PPL Time filter, hence adding +3/-3 days to actual timestamp + const startTime = + dateMath + .parse(props.startTime)! + .subtract(3, 'days') + .format(TRACE_ANALYTICS_DATE_FORMAT) ?? 'now-3y'; + const endTime = + dateMath + .parse(props.endTime, { roundUp: true })! + .add(3, 'days') + .format(TRACE_ANALYTICS_DATE_FORMAT) ?? 'now'; + if (coreRefs?.dataSource?.dataSourceEnabled) { coreRefs?.application!.navigateToApp('data-explorer', { - path: `discover#?_a=(discover:(columns:!(_source),isDirty:!f,sort:!()),metadata:(view:discover))&_g=(filters:!(),refreshInterval:(pause:!t,value:0),time:(from:${ - props.startTime - },to:${props.endTime}))&_q=(filters:!(),query:(dataset:(dataSource:(id:'${ + path: `discover#?_a=(discover:(columns:!(_source),isDirty:!f,sort:!()),metadata:(view:discover))&_g=(filters:!(),refreshInterval:(pause:!t,value:0),time:(from:'${startTime}',to:'${endTime}'))&_q=(filters:!(),query:(dataset:(dataSource:(id:'${ props.dataSourceMDSId[0].id ?? '' }',title:'${props.dataSourceMDSId[0].label}',type:DATA_SOURCE),id:'${ props.dataSourceMDSId[0].id ?? '' diff --git a/public/components/trace_analytics/components/traces/span_detail_flyout.tsx b/public/components/trace_analytics/components/traces/span_detail_flyout.tsx index 88da27d11..0c8e219d6 100644 --- a/public/components/trace_analytics/components/traces/span_detail_flyout.tsx +++ b/public/components/trace_analytics/components/traces/span_detail_flyout.tsx @@ -22,6 +22,11 @@ import round from 'lodash/round'; import moment from 'moment'; import React, { useEffect, useState } from 'react'; import { HttpSetup } from '../../../../../../../src/core/public'; +import { + DEFAULT_DATA_SOURCE_NAME, + DEFAULT_DATA_SOURCE_TYPE, +} from '../../../../../common/constants/data_sources'; +import { observabilityLogsID } from '../../../../../common/constants/shared'; import { TRACE_ANALYTICS_DATE_FORMAT } from '../../../../../common/constants/trace_analytics'; import { SpanField, TraceAnalyticsMode } from '../../../../../common/types/trace_analytics'; import { coreRefs } from '../../../../framework/core_refs'; @@ -300,17 +305,34 @@ export function SpanDetailFlyout(props: { }; const redirectToExplorer = () => { + // NOTE: Discover has issue with PPL Time filter, hence adding +3/-3 days to actual timestamp + const startTime = + moment(span.startTime).subtract(3, 'days').format(TRACE_ANALYTICS_DATE_FORMAT) ?? 'now-3y'; + const endTime = + moment(span.endTime).add(3, 'days').format(TRACE_ANALYTICS_DATE_FORMAT) ?? 'now'; const spanId = getSpanValue(span, mode, 'SPAN_ID'); const spanField = getSpanFieldKey(mode, 'SPAN_ID'); - coreRefs?.application!.navigateToApp('data-explorer', { - path: `discover#?_a=(discover:(columns:!(_source),isDirty:!f,sort:!()),metadata:(view:discover))&_g=(filters:!(),refreshInterval:(pause:!t,value:0),time:(from:${ - props.startTime - },to:${props.endTime}))&_q=(filters:!(),query:(dataset:(dataSource:(id:'${ - props.dataSourceMDSId ?? '' - }',title:${props.dataSourceMDSLabel},type:DATA_SOURCE),id:'${ - props.dataSourceMDSId - }::ss4o_logs-*',timeFieldName:time,title:'ss4o_logs-*',type:INDEXES),language:PPL,query:'source%20%3D%20ss4o_logs-*%20%7C%20where%20${spanField}%20%3D%20!'${spanId}!''))`, - }); + + if (coreRefs?.dataSource?.dataSourceEnabled) { + coreRefs?.application!.navigateToApp('data-explorer', { + path: `discover#?_a=(discover:(columns:!(_source),isDirty:!f,sort:!()),metadata:(view:discover))&_g=(filters:!(),refreshInterval:(pause:!t,value:0),time:(from:'${startTime}',to:'${endTime}'))&_q=(filters:!(),query:(dataset:(dataSource:(id:'${ + props.dataSourceMDSId ?? '' + }',title:${props.dataSourceMDSLabel},type:DATA_SOURCE),id:'${ + props.dataSourceMDSId ?? '' + }::ss4o_logs-*',timeFieldName:'time',title:'ss4o_logs-*',type:INDEXES),language:PPL,query:'source%20%3D%20ss4o_logs-*%20%7C%20where%20${spanField}%20%3D%20!'${spanId}!''))`, + }); + } else { + coreRefs?.application!.navigateToApp(observabilityLogsID, { + path: `#/explorer`, + state: { + DEFAULT_DATA_SOURCE_NAME, + DEFAULT_DATA_SOURCE_TYPE, + queryToRun: `source = ss4o_logs-* | where ${spanField}='${spanId}'`, + startTimeRange: startTime, + endTimeRange: endTime, + }, + }); + } }; return ( diff --git a/public/components/trace_analytics/home.tsx b/public/components/trace_analytics/home.tsx index ab3eae139..bb07128ad 100644 --- a/public/components/trace_analytics/home.tsx +++ b/public/components/trace_analytics/home.tsx @@ -17,9 +17,11 @@ import { } from '../../../../../src/core/public'; import { DataSourceManagementPluginSetup, + DataSourceOption, DataSourceSelectableConfig, DataSourceViewConfig, } from '../../../../../src/plugins/data_source_management/public'; +import { DataSourceAttributes } from '../../../../../src/plugins/data_source_management/public/types'; import { TRACE_TABLE_TYPE_KEY } from '../../../common/constants/trace_analytics'; import { TraceAnalyticsMode, TraceQueryMode } from '../../../common/types/trace_analytics'; import { coreRefs } from '../../framework/core_refs'; @@ -122,7 +124,7 @@ export const Home = (props: HomeProps) => { const queryParamsOnLoad = new URLSearchParams(window.location.href.split('?')[1]); const dsFromURL = queryParamsOnLoad.get('datasourceId'); - const [dataSourceMDSId, setDataSourceMDSId] = useState([ + const [dataSourceMDSId, setDataSourceMDSId] = useState([ { id: dsFromURL ?? undefined, label: undefined }, ]); const [currentSelectedService, setCurrentSelectedService] = useState(''); @@ -194,6 +196,17 @@ export const Home = (props: HomeProps) => { props.notifications, ]); + // This function sets mds label given the id is set in state:dataSourceMDSId + const getDatasourceAttributes = async () => { + const dataSourceAttributes = await coreRefs?.savedObjectsClient?.get( + 'data-source', + dataSourceMDSId[0].id + ); + setDataSourceMDSId([ + { id: dataSourceMDSId[0].id, label: dataSourceAttributes?.attributes.title }, + ]); + }; + useEffect(() => { handleDataPrepperIndicesExistRequest( props.http, @@ -201,6 +214,10 @@ export const Home = (props: HomeProps) => { dataSourceMDSId[0].id ); handleJaegerIndicesExistRequest(props.http, setJaegerIndicesExist, dataSourceMDSId[0].id); + // When datasource is loaded form the URL, the label is set to undefined + if (dataSourceMDSId[0].id && dataSourceMDSId[0].label === undefined) { + getDatasourceAttributes(); + } }, [dataSourceMDSId]); const modes = [