Skip to content

Commit d4ead5f

Browse files
log and tooltip fix (#183)
1 parent 538f96a commit d4ead5f

File tree

7 files changed

+35
-105
lines changed

7 files changed

+35
-105
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@
355355
"kbar": "0.1.0-beta.40",
356356
"lodash": "4.17.21",
357357
"logfmt": "^1.3.2",
358+
"loglevel": "^1.9.1",
358359
"lru-cache": "10.0.0",
359360
"lru-memoize": "^1.1.0",
360361
"marked": "5.1.1",

public/app/features/visualization/data-hover/DataHoverView.tsx

+6-2
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ const getStyles = (theme: GrafanaTheme2) => {
138138
wrapper: css`
139139
background: ${bg};
140140
border: 1px solid ${headerBg};
141-
border-radius: ${theme.shape.borderRadius(2)};
142141
`,
143142
header: css`
144143
background: ${headerBg};
@@ -158,14 +157,19 @@ const getStyles = (theme: GrafanaTheme2) => {
158157
padding: 8px;
159158
th {
160159
font-weight: ${theme.typography.fontWeightMedium};
161-
padding: ${theme.spacing(0.25, 2)};
160+
padding: ${theme.spacing(0.25)};
161+
padding-left: ${theme.spacing(1)};
162162
}
163163
tr {
164164
background-color: ${theme.colors.background.primary};
165165
&:nth-child(even) {
166166
background-color: ${tableBgOdd};
167167
}
168168
}
169+
td {
170+
padding: ${theme.spacing(0.25)};
171+
padding-right: ${theme.spacing(1)};
172+
}
169173
`,
170174
highlight: css`
171175
/* !important is required to overwrite default table styles */

public/app/fn-app/create-mfe.ts

+13-28
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,14 @@ type DeepPartial<T> = {
6060

6161
class createMfe {
6262
private static readonly containerSelector = '#grafanaRoot';
63-
64-
private static readonly logPrefix = '[FN Grafana]';
63+
private static logger = FnLoggerService;
6564

6665
mode: FNDashboardProps['mode'];
6766
static Component: ComponentType<Omit<FNDashboardProps, FnPropMappedFromState>>;
6867
constructor(readonly props: FNDashboardProps) {
6968
this.mode = props.mode;
7069
}
7170

72-
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
73-
private static logger = (...args: any[]) => console.log(createMfe.logPrefix, ...args);
74-
7571
static getLifeCycles(component: ComponentType<Omit<FNDashboardProps, FnPropMappedFromState>>) {
7672
const lifeCycles: FrameworkLifeCycles = {
7773
bootstrap: this.boot(),
@@ -168,7 +164,7 @@ class createMfe {
168164
* If isRuntimeOnly then the stylesheets of the turned off theme are not removed
169165
*/
170166
private static loadFnTheme = (mode: FNDashboardProps['mode'] = GrafanaThemeType.Light, isRuntimeOnly = false) => {
171-
createMfe.logger('Trying to load theme.', { mode });
167+
createMfe.logger.info('Trying to load theme.', { mode });
172168

173169
const grafanaTheme2 = createMfe.createGrafanaTheme2(mode);
174170

@@ -179,7 +175,7 @@ class createMfe {
179175
createMfe.publishTheme(bootConfigWithTheme.theme2);
180176

181177
if (isRuntimeOnly) {
182-
createMfe.logger('Successfully loaded theme', { mode });
178+
createMfe.logger.info('Successfully loaded theme', { mode });
183179

184180
return;
185181
}
@@ -190,7 +186,7 @@ class createMfe {
190186
newCssLink.href = config.bootData.themePaths[mode];
191187
document.body.appendChild(newCssLink);
192188

193-
createMfe.logger('Successfully loaded theme.', { mode });
189+
createMfe.logger.info('Successfully loaded theme.', { mode });
194190
};
195191

196192
private static getContainer(props: FNDashboardProps) {
@@ -201,8 +197,6 @@ class createMfe {
201197

202198
static mountFnApp(Component: ComponentType<Omit<FNDashboardProps, FnPropMappedFromState>>) {
203199
const lifeCycleFn: FrameworkLifeCycles['mount'] = (props: FNDashboardProps) => {
204-
createMfe.logger('Trying to mount grafana...');
205-
206200
return new Promise((res, rej) => {
207201
try {
208202
createMfe.loadFnTheme(props.mode);
@@ -214,19 +208,19 @@ class createMfe {
214208
...pick(props, ...fnStateProps),
215209
};
216210

217-
FnLoggerService.log(null, '[FN Grafana] Dispatching initial state.', { initialState });
211+
createMfe.logger.info('[FN Grafana] Dispatching initial state.', { initialState });
218212

219213
dispatch(updateFnState(initialState));
220214

221215
createMfe.renderMfeComponent(props, () => {
222-
createMfe.logger('Mounted grafana.', { props });
216+
createMfe.logger.info('Mounted grafana.', { props });
223217

224218
return res(true);
225219
});
226220
} catch (err) {
227221
const message = `[FN Grafana]: Failed to mount grafana. ${err}`;
228222

229-
FnLoggerService.log(null, message);
223+
FnLoggerService.info(null, message);
230224

231225
const fnError = new Error(message);
232226

@@ -246,13 +240,13 @@ class createMfe {
246240
const container = createMfe.getContainer(props);
247241

248242
if (container) {
249-
createMfe.logger('Trying to unmount grafana...');
243+
createMfe.logger.info('Trying to unmount grafana...');
250244

251245
ReactDOM.unmountComponentAtNode(container);
252246

253-
createMfe.logger('Successfully unmounted grafana.');
247+
createMfe.logger.info('Successfully unmounted grafana.');
254248
} else {
255-
createMfe.logger('Failed to unmount grafana. Container does not exist.');
249+
createMfe.logger.error('Failed to unmount grafana. Container does not exist.');
256250
}
257251

258252
backendSrv.cancelAllInFlightRequests();
@@ -266,26 +260,17 @@ class createMfe {
266260
static updateFnApp() {
267261
const lifeCycleFn: FrameworkLifeCycles['update'] = ({ mode, ...other }: FNDashboardProps) => {
268262
if (mode) {
269-
createMfe.logger('Trying to update grafana with theme.', { mode });
270-
271263
dispatch(
272264
updatePartialFnStates({
273265
mode,
274266
})
275267
);
276-
/**
277-
* NOTE:
278-
* Here happens the theme change.
279-
*
280-
* TODO:
281-
* We could probably made the theme change smoother
282-
* if we use state to update the theme
283-
*/
268+
284269
createMfe.loadFnTheme(mode);
285270
}
286271

287272
if (other.uid) {
288-
createMfe.logger('Trying to update grafana with hidden variables.', { updatedProps: other });
273+
createMfe.logger.info('Trying to render dashboard using update: ', { updatedProps: other });
289274

290275
dispatch(
291276
updatePartialFnStates({
@@ -310,7 +295,7 @@ class createMfe {
310295
const container = createMfe.getContainer(props);
311296

312297
ReactDOM.render(React.createElement(createMfe.Component, props), container, () => {
313-
createMfe.logger('Created mfe component.', { props, container });
298+
createMfe.logger.info('Created mfe component.', { props, container });
314299
onSuccess();
315300
});
316301
}

public/app/fn_logger.ts

+5-75
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,9 @@
1-
import { isBoolean, noop } from 'lodash';
1+
import log from 'loglevel';
22

3-
const SHOULD_LOG = process.env.NODE_ENV !== 'production';
3+
const SHOULD_LOG = process.env.SHOULD_LOG === 'true';
44

5-
type Console = Pick<typeof console, 'info' | 'error' | 'debug' | 'warn' | 'log'>;
5+
const FnLoggerService = log.getLogger('[FN Grafana]');
66

7-
export class FnLoggerService {
8-
private static readonly DEFAULT_SHOULD_LOG = false;
7+
FnLoggerService.setLevel(SHOULD_LOG ? log.levels.DEBUG : log.levels.ERROR);
98

10-
private static logger(shouldLog: boolean | null) {
11-
/* eslint-disable-next-line */
12-
const flag = isBoolean(shouldLog)
13-
? shouldLog
14-
: isBoolean(SHOULD_LOG)
15-
? SHOULD_LOG
16-
: FnLoggerService.DEFAULT_SHOULD_LOG;
17-
18-
if (flag) {
19-
return console as Console;
20-
}
21-
22-
const noopConsole: Console = {
23-
info: noop,
24-
error: noop,
25-
debug: noop,
26-
warn: noop,
27-
log: noop,
28-
};
29-
30-
return noopConsole;
31-
}
32-
33-
/* eslint-disable @typescript-eslint/no-explicit-any */
34-
static debug = (shouldLog: boolean | null, ...args: any[]) => {
35-
FnLoggerService.logger(shouldLog).debug(FnLoggerService.valuesToString(...args));
36-
};
37-
38-
static error = (shouldLog: boolean | null, ...args: any[]) => {
39-
FnLoggerService.logger(shouldLog).error(FnLoggerService.valuesToString(...args));
40-
};
41-
42-
static warn = (shouldLog: boolean | null, ...args: any[]) => {
43-
FnLoggerService.logger(shouldLog).warn(FnLoggerService.valuesToString(...args));
44-
};
45-
46-
static info = (shouldLog: boolean | null, ...args: any[]) => {
47-
FnLoggerService.logger(shouldLog).info(FnLoggerService.valuesToString(...args));
48-
};
49-
50-
static log = (shouldLog: boolean | null, ...args: any[]) => {
51-
FnLoggerService.logger(shouldLog).log(FnLoggerService.valuesToString(...args));
52-
};
53-
/* eslint-enable @typescript-eslint/no-explicit-any */
54-
55-
/* eslint-disable @typescript-eslint/no-explicit-any */
56-
private static readonly valuesToString = (...args: any[]): string =>
57-
args.map(FnLoggerService.valueToString).join(' ');
58-
/* eslint-enable @typescript-eslint/no-explicit-any */
59-
60-
private static readonly valueToString = <V>(value: V): string => {
61-
if (typeof value === 'string') {
62-
return value;
63-
}
64-
65-
if (Array.isArray(value)) {
66-
return value.map(FnLoggerService.valueToString).join(' ');
67-
}
68-
69-
if (value instanceof Error) {
70-
return value.message;
71-
}
72-
73-
try {
74-
return JSON.stringify(value);
75-
} catch {
76-
return String(value);
77-
}
78-
};
79-
}
9+
export { FnLoggerService };

scripts/webpack/webpack.dev.js

+1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ module.exports = (env = {}) => {
125125
new DefinePlugin({
126126
'process.env': {
127127
NODE_ENV: JSON.stringify('development'),
128+
SHOULD_LOG: JSON.stringify('true'),
128129
},
129130
}),
130131
],

scripts/webpack/webpack.prod.js

+1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ module.exports = (env = {}) =>
9696
new DefinePlugin({
9797
'process.env': {
9898
NODE_ENV: JSON.stringify('development'),
99+
SHOULD_LOG: JSON.stringify('false'),
99100
},
100101
}),
101102
new WebpackManifestPlugin({

yarn.lock

+8
Original file line numberDiff line numberDiff line change
@@ -19954,6 +19954,7 @@ __metadata:
1995419954
lint-staged: 13.2.3
1995519955
lodash: 4.17.21
1995619956
logfmt: ^1.3.2
19957+
loglevel: ^1.9.1
1995719958
lru-cache: 10.0.0
1995819959
lru-memoize: ^1.1.0
1995919960
marked: 5.1.1
@@ -23523,6 +23524,13 @@ __metadata:
2352323524
languageName: node
2352423525
linkType: hard
2352523526

23527+
"loglevel@npm:^1.9.1":
23528+
version: 1.9.1
23529+
resolution: "loglevel@npm:1.9.1"
23530+
checksum: e1c8586108c4d566122e91f8a79c8df728920e3a714875affa5120566761a24077ec8ec9e5fc388b022e39fc411ec6e090cde1b5775871241b045139771eeb06
23531+
languageName: node
23532+
linkType: hard
23533+
2352623534
"long@npm:^5.0.0":
2352723535
version: 5.2.3
2352823536
resolution: "long@npm:5.2.3"

0 commit comments

Comments
 (0)