Skip to content

Commit

Permalink
DEV-23041 - log to logzio on any error (#17)
Browse files Browse the repository at this point in the history
* DEV-23041 - log to logzio on any error
DEV-26768 - Use new label resolving api for m3

* DEV-28224 disable exemplars
Disable sending frontend metrics
Email validation
Adding types icons
Handle email endpoint
Disable endpoints editing in grafana
(ALERTS) Add emails input -->
Disable AM Selector
DEV-33551

* Fix compile errors
  • Loading branch information
copyhold authored Apr 7, 2024
1 parent e5f6ea5 commit 8b443f4
Show file tree
Hide file tree
Showing 20 changed files with 343 additions and 161 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { css, cx } from '@emotion/css';
import { isEqual } from 'lodash';
import React, { memo, useCallback } from 'react';
import { usePrevious } from 'react-use';
// LOGZIO GRAFANA CHANGE :: DEV-28224 disable exemplars
// import { isEqual } from 'lodash';
import React, { memo } from 'react';
// import { usePrevious } from 'react-use';
// LOGZIO GRAFANA CHANGE :: end

import { InlineFormLabel, RadioButtonGroup } from '@grafana/ui';

import { PrometheusDatasource } from '../datasource';
import { PromQuery } from '../types';

import { PromExemplarField } from './PromExemplarField';
// import { PromExemplarField } from './PromExemplarField'// LOGZIO GRAFANA CHANGE :: DEV-28224 disable exemplars

export interface PromExploreExtraFieldProps {
query: PromQuery;
Expand All @@ -19,16 +21,17 @@ export interface PromExploreExtraFieldProps {

export const PromExploreExtraField = memo(({ query, datasource, onChange, onRunQuery }: PromExploreExtraFieldProps) => {
const rangeOptions = getQueryTypeOptions(true);
const prevQuery = usePrevious(query);

const onExemplarChange = useCallback(
(exemplar: boolean) => {
if (!isEqual(query, prevQuery) || exemplar !== query.exemplar) {
onChange({ ...query, exemplar });
}
},
[prevQuery, query, onChange]
);
// LOGZIO GRAFANA CHANGE :: DEV-28224 disable exemplars
//const prevQuery = usePrevious(query);

//const onExemplarChange = useCallback(
// (exemplar: boolean) => {
// if (!isEqual(query, prevQuery) || exemplar !== query.exemplar) {
// onChange({ ...query, exemplar });
// }
// },
// [prevQuery, query, onChange]
//);

function onChangeQueryStep(interval: string) {
onChange({ ...query, interval });
Expand Down Expand Up @@ -102,7 +105,7 @@ export const PromExploreExtraField = memo(({ query, datasource, onChange, onRunQ
/>
</div>

<PromExemplarField onChange={onExemplarChange} datasource={datasource} query={query} />
{/* <PromExemplarField onChange={onExemplarChange} datasource={datasource} query={query} /> // LOGZ.IO GRAFANA CHANGE :: Disable exemplars*/}
</div>
);
});
Expand Down
69 changes: 36 additions & 33 deletions packages/grafana-prometheus/src/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export class PrometheusDatasource
this.defaultEditor = instanceSettings.jsonData.defaultEditor;
this.disableRecordingRules = instanceSettings.jsonData.disableRecordingRules ?? false;
this.variables = new PrometheusVariableSupport(this, this.templateSrv);
this.exemplarsAvailable = true;
this.exemplarsAvailable = false; // LOGZ.IO GRAFANA CHANGE :: Disable exemplars
this.cacheLevel = instanceSettings.jsonData.cacheLevel ?? PrometheusCacheLevel.Low;

this.cache = new QueryCache({
Expand Down Expand Up @@ -339,18 +339,19 @@ export class PrometheusDatasource
}

shouldRunExemplarQuery(target: PromQuery, request: DataQueryRequest<PromQuery>): boolean {
if (target.exemplar) {
// We check all already processed targets and only create exemplar target for not used metric names
const metricName = this.languageProvider.histogramMetrics.find((m) => target.expr.includes(m));
// Remove targets that weren't processed yet (in targets array they are after current target)
const currentTargetIdx = request.targets.findIndex((t) => t.refId === target.refId);
const targets = request.targets.slice(0, currentTargetIdx).filter((t) => !t.hide);

if (!metricName || (metricName && !targets.some((t) => t.expr.includes(metricName)))) {
return true;
}
return false;
}
// LOGZ.IO GRAFANA CHANGE :: Disable exemplars
// if (target.exemplar) {
// // We check all already processed targets and only create exemplar target for not used metric names
// const metricName = this.languageProvider.histogramMetrics.find((m) => target.expr.includes(m));
// // Remove targets that weren't processed yet (in targets array they are after current target)
// const currentTargetIdx = request.targets.findIndex((t) => t.refId === target.refId);
// const targets = request.targets.slice(0, currentTargetIdx).filter((t) => !t.hide);

// if (!metricName || (metricName && !targets.some((t) => t.expr.includes(metricName)))) {
// return true;
// }
// return false;
// }
return false;
}

Expand Down Expand Up @@ -758,27 +759,29 @@ export class PrometheusDatasource
}
}

// LOGZ.IO GRAFANA CHANGE :: Disable exemplars
async areExemplarsAvailable() {
try {
const res = await this.metadataRequest(
'/api/v1/query_exemplars',
{
query: 'test',
start: dateTime().subtract(30, 'minutes').valueOf().toString(),
end: dateTime().valueOf().toString(),
},
{
// Avoid alerting the user if this test fails
showErrorAlert: false,
}
);
if (res.data.status === 'success') {
return true;
}
return false;
} catch (err) {
return false;
}
return false;
// try {
// const res = await this.metadataRequest(
// '/api/v1/query_exemplars',
// {
// query: 'test',
// start: dateTime().subtract(30, 'minutes').valueOf().toString(),
// end: dateTime().valueOf().toString(),
// },
// {
// // Avoid alerting the user if this test fails
// showErrorAlert: false,
// }
// );
// if (res.data.status === 'success') {
// return true;
// }
// return false;
// } catch (err) {
// return false;
// }
}

modifyQuery(query: PromQuery, action: QueryFixAction): PromQuery {
Expand Down
13 changes: 13 additions & 0 deletions public/app/core/copy/appNotification.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useMemo } from 'react';
import { v4 as uuidv4 } from 'uuid';

import { logzioServices } from '@grafana/data'; // LOGZ.IO GRAFANA CHANGE :: DEV-23041 - log to logzio on any error
import { getMessageFromError } from 'app/core/utils/errors';
import { AppNotification, AppNotificationSeverity, useDispatch } from 'app/types';

Expand Down Expand Up @@ -42,6 +43,18 @@ export const createErrorNotification = (
traceId?: string,
component?: React.ReactElement
): AppNotification => {
// LOGZ.IO GRAFANA CHANGE :: DEV-23041 - log to logzio on any error
const logzLogger = logzioServices.LoggerService;
logzLogger.logError({
origin: logzLogger.Origin.GRAFANA,
message: getMessageFromError(text),
error: null,
uxType: logzLogger.UxType.TOAST,
extra: {
title,
},
});

return {
...defaultErrorNotification,
text: getMessageFromError(text),
Expand Down
9 changes: 5 additions & 4 deletions public/app/core/services/echo/backends/PerformanceBackend.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EchoBackend, EchoEvent, EchoEventType } from '@grafana/runtime';

import { backendSrv } from '../../backend_srv';
// import { backendSrv } from '../../backend_srv'; // LOGZ.IO GRAFANA CHANGE :: Disable sending frontend metrics

export interface PerformanceEventPayload {
name: string;
Expand Down Expand Up @@ -32,9 +32,10 @@ export class PerformanceBackend implements EchoBackend<PerformanceEvent, Perform
return;
}

backendSrv.post('/api/frontend-metrics', {
events: this.buffer,
});
// LOGZ.IO GRAFANA CHANGE :: Disable sending frontend metrics
// backendSrv.post('/api/frontend-metrics', {
// events: this.buffer,
// });

this.buffer = [];
};
Expand Down
77 changes: 75 additions & 2 deletions public/app/features/alerting/AlertTabCtrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ export class AlertTabCtrl {
alertingMinIntervalSecs: number;
alertingMinInterval: string;
frequencyWarning: any;
// LOGZ.IO GRAFANA CHANGE :: (ALERTS) Email validation
isEmailValid: boolean;
alertEmailNotifications: any;
emailInEdit: string;
// LOGZ.IO GRAFANA CHANGE :: end

static $inject = ['$scope', 'dashboardSrv', 'uiSegmentSrv', 'datasourceSrv'];

Expand All @@ -61,6 +66,11 @@ export class AlertTabCtrl {
this.panelCtrl._enableAlert = this.enable;
this.alertingMinIntervalSecs = config.alertingMinInterval;
this.alertingMinInterval = rangeUtil.secondsToHms(config.alertingMinInterval);
// LOGZ.IO GRAFANA CHANGE :: (ALERTS) Email validation
this.isEmailValid = true;
this.alertEmailNotifications = [];
this.emailInEdit = '';
// LOGZ.IO GRAFANA CHANGE :: end
}

$onInit() {
Expand Down Expand Up @@ -131,22 +141,33 @@ export class AlertTabCtrl {
return 'arrow-random';
case 'teams':
return 'fa fa-windows';
// LOGZ.IO GRAFANA CHANGE :: (ALERTS) Adding types icons
case 'custom':
return 'fa fa-wrench';
case 'datadog':
return 'fa fa-paw';
}
return 'bell';
}

getNotifications() {
return Promise.resolve(
this.notifications.map((item: any) => {
return this.uiSegmentSrv.newSegment(item.name);
// LOGZ.IO GRAFANA CHANGE :: (ALERTS) Alert {type.name}
return this.uiSegmentSrv.newSegment(`${item.type}.${item.name}`);
})
);
}

notificationAdded() {
// LOGZ.IO GRAFANA CHANGE :: (ALERTS) Get name from 'type.name'
const endpointName = this.addNotificationSegment.value.substr(this.addNotificationSegment.value.indexOf('.') + 1);

const model: any = find(this.notifications, {
name: this.addNotificationSegment.value,
name: endpointName,
});
// LOGZ.IO GRAFANA CHANGE :: end

if (!model) {
return;
}
Expand All @@ -169,6 +190,55 @@ export class AlertTabCtrl {
this.addNotificationSegment.fake = true;
}

// LOGZ.IO GRAFANA CHANGE :: (ALERTS) Handle email endpoint
emailInputChanged(e: any) {
if (e.keyCode === 13) {
this.addEmail();
return;
}

this.isEmailValid = !this.emailInEdit || (window as any).logzio.services.logzPatterns.email.test(this.emailInEdit);
}

addEmail() {
if (!this.emailInEdit || !this.isEmailValid) {
this.isEmailValid = false;

return;
}

const address = this.emailInEdit;
this.emailInEdit = '';

this.alert.emailNotifications.push({
address,
});

this.alertEmailNotifications.push({
name: address,
iconClass: this.getNotificationIcon('email'),
isDefault: false,
});
}

initEmails() {
this.alert.emailNotifications = this.alert.emailNotifications || [];

this.alert.emailNotifications.forEach((email: any) => {
this.alertEmailNotifications.push({
name: email.address,
iconClass: this.getNotificationIcon('email'),
isDefault: false,
});
});
}

removeEmailNotification(an: any) {
remove(this.alertEmailNotifications, (n: any) => n.name === an.name);
remove(this.alert.emailNotifications, (n: any) => n.address === an.name);
}
// LOGZ.IO GRAFANA CHANGE :: (ALERTS) Handle email endpoint : End

removeNotification(an: any) {
// remove notifiers referred to by id and uid to support notifiers added
// before and after we added support for uid
Expand Down Expand Up @@ -263,6 +333,9 @@ export class AlertTabCtrl {
}
}

// LOGZ.IO GRAFANA CHANGE :: (ALERTS) Init emails
this.initEmails();

this.panelCtrl.editingThresholds = true;
this.panelCtrl.render();
}
Expand Down
Loading

0 comments on commit 8b443f4

Please sign in to comment.