4. Given the function
$$
f(x)=\frac{x^{4}+\left(k^{2}+2 k-4\right) x^{2}+4}{x^{4}+2 x^{2}+4}
$$

has a minimum value of 0. Then the value of the non-zero real number $k$ is ( ).
(A) -4
(B) -2
(C) 2
(D) 4
4. B.

Notice that
$$
f(x)=1+\left(k^{2}+2 k-6\right) \frac{x^{2}}{x^{4}+2 x^{2}+4} \text {. }
$$

Since $x^{4}+4 \geqslant 4 x^{2}$, we have
$$
0 \leqslant \frac{x^{2}}{x^{4}+2 x^{2}+4} \leqslant \frac{1}{6} \text {. }
$$

When $k^{2}+2 k-6 \geqslant 0$, $f_{\text {min }}=1>0$;
When $k^{2}+2 k-6<0$,
$$
f_{\min }=1+\frac{1}{6}\left(k^{2}+2 k-6\right)=0 \text {, }
$$

Solving this gives $k=-2$ or 0 (discard 0).