samber / samber/chartjs-plugin-datasource-prometheus
Cannot read properties of null (reading 'addEventListener')
Open
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 123
- Forks
- 23
- PR merge metrics
- No merged PRs in 30d
Description
Hi, i have got the error in the console
import { useEffect, useRef } from "react";
import Chart from "chart.js/auto";
import "chartjs-adapter-date-fns";
import ChartDatasourcePrometheusPlugin from "chartjs-plugin-datasource-prometheus";
import "./styles.css";
export default function App() {
const canvasRef = useRef<HTMLCanvasElement>(null);
const chartRef = useRef<Chart | null>(null);
useEffect(() => {
if (canvasRef.current) {
chartRef.current = new Chart(canvasRef.current, {
type: "line",
data: {
datasets: [],
},
plugins: [ChartDatasourcePrometheusPlugin],
options: {
plugins: {
"datasource-prometheus": {
prometheus: {
endpoint: "https://prometheus.demo.do.prometheus.io",
baseURL: "/api/v1", // default value
},
query: "sum by (job) (go_gc_duration_seconds)",
timeRange: {
type: "relative",
// from 1 hours ago to now
start: -1 * 60 * 60 * 1000,
end: 0,
},
},
},
},
});
}
return () => {
chartRef.current?.destroy();
};
}, []);
return <canvas ref={canvasRef}></canvas>;
}
Playground:
https://codesandbox.io/p/sandbox/chartjs-prometheus-bug-4jq97s
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the error using the linked CodeSandbox and the React example in the issue body. Trace the plugin initialization around the reported addEventListener failure, then verify that the example creates the chart without the console error and can read the Prometheus data.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- chart.js, prometheus, react, typescript
- Domain
- frontend, observability-sre
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 42/100