Data query: Allow logging panel plugin id when executing queries (#81164)

* Data query: Allo logging panel plugin id when executing queries

* Update tracing header middleware

* Test fix

* Add panelPluginType to query analytics

* Cleanup
This commit is contained in:
Dominik Prokop 2024-01-30 00:06:31 -08:00 committed by GitHub
parent 5ab75410e9
commit f77c831e3f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 38 additions and 9 deletions

View File

@ -555,6 +555,7 @@ export interface DataQueryRequest<TQuery extends DataQuery = DataQuery> {
rangeRaw?: RawTimeRange;
timeInfo?: string; // The query time description (blue text in the upper right)
panelId?: number;
panelPluginType?: string;
dashboardUID?: string;
/** Filters to dynamically apply to all queries */

View File

@ -29,6 +29,7 @@ export interface DataRequestInfo extends Partial<DashboardInfo> {
datasourceUid: string;
datasourceType: string;
panelId?: number;
panelPluginType?: string;
panelName?: string;
duration: number;
error?: string;

View File

@ -80,6 +80,7 @@ enum PluginRequestHeaders {
DatasourceUID = 'X-Datasource-Uid', // can be used for routing/ load balancing
DashboardUID = 'X-Dashboard-Uid', // mainly useful for debugging slow queries
PanelID = 'X-Panel-Id', // mainly useful for debugging slow queries
PanelPluginType = 'X-Panel-Plugin-Type',
QueryGroupID = 'X-Query-Group-Id', // mainly useful to find related queries with query splitting
FromExpression = 'X-Grafana-From-Expr', // used by datasources to identify expression queries
SkipQueryCache = 'X-Cache-Skip', // used by datasources to skip the query cache
@ -226,6 +227,9 @@ class DataSourceWithBackend<
if (request.panelId) {
headers[PluginRequestHeaders.PanelID] = `${request.panelId}`;
}
if (request.panelPluginType) {
headers[PluginRequestHeaders.PanelPluginType] = `${request.panelPluginType}`;
}
if (request.queryGroupId) {
headers[PluginRequestHeaders.QueryGroupID] = `${request.queryGroupId}`;
}

View File

@ -343,6 +343,8 @@ func (proxy *DataSourceProxy) logRequest() {
}
}
panelPluginType := proxy.ctx.Req.Header.Get("X-Panel-Plugin-Type")
ctxLogger := logger.FromContext(proxy.ctx.Req.Context())
ctxLogger.Info("Proxying incoming request",
"userid", proxy.ctx.UserID,
@ -351,6 +353,7 @@ func (proxy *DataSourceProxy) logRequest() {
"datasource", proxy.ds.Type,
"uri", proxy.ctx.Req.RequestURI,
"method", proxy.ctx.Req.Method,
"panelPluginType", panelPluginType,
"body", body)
}

View File

@ -34,7 +34,7 @@ func (m *TracingHeaderMiddleware) applyHeaders(ctx context.Context, req backend.
return
}
var headersList = []string{query.HeaderQueryGroupID, query.HeaderPanelID, query.HeaderDashboardUID, query.HeaderDatasourceUID, query.HeaderFromExpression, `X-Grafana-Org-Id`}
var headersList = []string{query.HeaderQueryGroupID, query.HeaderPanelID, query.HeaderDashboardUID, query.HeaderDatasourceUID, query.HeaderFromExpression, `X-Grafana-Org-Id`, query.HeaderPanelPluginType}
for _, headerName := range headersList {
gotVal := reqCtx.Req.Header.Get(headerName)

View File

@ -28,12 +28,13 @@ import (
)
const (
HeaderPluginID = "X-Plugin-Id" // can be used for routing
HeaderDatasourceUID = "X-Datasource-Uid" // can be used for routing/ load balancing
HeaderDashboardUID = "X-Dashboard-Uid" // mainly useful for debugging slow queries
HeaderPanelID = "X-Panel-Id" // mainly useful for debugging slow queries
HeaderQueryGroupID = "X-Query-Group-Id" // mainly useful for finding related queries with query chunking
HeaderFromExpression = "X-Grafana-From-Expr" // used by datasources to identify expression queries
HeaderPluginID = "X-Plugin-Id" // can be used for routing
HeaderDatasourceUID = "X-Datasource-Uid" // can be used for routing/ load balancing
HeaderDashboardUID = "X-Dashboard-Uid" // mainly useful for debugging slow queries
HeaderPanelID = "X-Panel-Id" // mainly useful for debugging slow queries
HeaderPanelPluginType = "X-Panel-Plugin-Type"
HeaderQueryGroupID = "X-Query-Group-Id" // mainly useful for finding related queries with query chunking
HeaderFromExpression = "X-Grafana-From-Expr" // used by datasources to identify expression queries
)
func ProvideService(

View File

@ -125,12 +125,13 @@ describe('DashboardScene', () => {
scene.onEnterEditMode();
});
it('Should add app, uid, and panelId', () => {
it('Should add app, uid, panelId and panelPluginType', () => {
const queryRunner = sceneGraph.findObject(scene, (o) => o.state.key === 'data-query-runner')!;
expect(scene.enrichDataRequest(queryRunner)).toEqual({
app: CoreApp.Dashboard,
dashboardUID: 'dash-1',
panelId: 1,
panelPluginType: 'table',
});
});

View File

@ -411,6 +411,7 @@ export class DashboardScene extends SceneObjectBase<DashboardSceneState> {
app: CoreApp.Dashboard,
dashboardUID: this.state.uid,
panelId,
panelPluginType: panel?.state.pluginId,
};
}

View File

@ -366,6 +366,7 @@ export class PanelModel implements DataConfigSource, IPanelModel {
datasource: this.datasource,
queries: this.targets,
panelId: this.id,
panelPluginType: this.type,
dashboardUID: dashboardUID,
timezone: dashboardTimezone,
timeRange: timeData.timeRange,

View File

@ -50,6 +50,7 @@ export interface QueryRunnerOptions<
datasource: DataSourceRef | DataSourceApi<TQuery, TOptions> | null;
queries: TQuery[];
panelId?: number;
panelPluginType?: string;
dashboardUID?: string;
timezone: TimeZone;
timeRange: TimeRange;
@ -257,6 +258,7 @@ export class PanelQueryRunner {
timezone,
datasource,
panelId,
panelPluginType,
dashboardUID,
timeRange,
timeInfo,
@ -278,6 +280,7 @@ export class PanelQueryRunner {
requestId: getNextRequestId(),
timezone,
panelId,
panelPluginType,
dashboardUID,
range: timeRange,
timeInfo,

View File

@ -103,6 +103,7 @@ function getTestData(
scopedVars: {},
targets: [],
timezone: 'utc',
panelPluginType: 'timeseries',
...overrides,
},
series: series || [],
@ -134,6 +135,7 @@ describe('emitDataRequestEvent', () => {
duration: 1,
totalQueries: 0,
cachedQueries: 0,
panelPluginType: 'timeseries',
})
);
});
@ -161,6 +163,7 @@ describe('emitDataRequestEvent', () => {
duration: 1,
totalQueries: 2,
cachedQueries: 1,
panelPluginType: 'timeseries',
})
);
});
@ -188,6 +191,7 @@ describe('emitDataRequestEvent', () => {
duration: 1,
totalQueries: 1,
cachedQueries: 1,
panelPluginType: 'timeseries',
})
);
});
@ -234,6 +238,7 @@ describe('emitDataRequestEvent', () => {
dataSize: 0,
duration: 1,
totalQueries: 0,
panelPluginType: 'timeseries',
})
);
});
@ -270,6 +275,7 @@ describe('emitDataRequestEvent', () => {
dataSize: 0,
duration: 1,
totalQueries: 0,
panelPluginType: 'timeseries',
})
);
});

View File

@ -29,6 +29,7 @@ export function emitDataRequestEvent(datasource: DataSourceApi) {
datasourceType: datasource.type,
dataSize: 0,
panelId: 0,
panelPluginType: data.request?.panelPluginType,
duration: data.request.endTime! - data.request.startTime,
};

View File

@ -246,7 +246,10 @@ export class GraphiteDatasource
return this.doGraphiteRequest(httpOptions).pipe(map(this.convertResponseToDataFrames));
}
addTracingHeaders(httpOptions: { headers: any }, options: { dashboardId?: number; panelId?: number }) {
addTracingHeaders(
httpOptions: { headers: any },
options: { dashboardId?: number; panelId?: number; panelPluginType?: string }
) {
const proxyMode = !this.url.match(/^http/);
if (proxyMode) {
if (options.dashboardId) {
@ -255,6 +258,9 @@ export class GraphiteDatasource
if (options.panelId) {
httpOptions.headers['X-Panel-Id'] = options.panelId;
}
if (options.panelPluginType) {
httpOptions.headers['X-Panel-Plugin-Id'] = options.panelPluginType;
}
}
}