The integral is$$\int_0^{2\pi}\frac{\mathrm dθ}{2-\cosθ}.$$Just to skip time, the answer of the indefinite integral is $\dfrac2{\sqrt{3}}\tan^{-1}\left(\sqrt3\tan\left(\dfracθ2\right)\right)$.
Evaluating it from $0$ to $ 2 \pi$ yields$$\frac2{\sqrt3}\tan^{-1}(\sqrt3 \tanπ)-\frac2{\sqrt3}\tan^{-1}(\sqrt3 \tan0)=0-0=0.$$But using complex analysis, the integral is transformed into$$2i\int_C\frac{\mathrm dz}{z^2-4z+1}=2i\int_C\frac{\mathrm dz}{(z-2+\sqrt3)(z-2-\sqrt3)},$$ where $C$ is the boundary of the circle $|z|=1$. Then by Cauchy's integral formula, since $z=2-\sqrt3$ is inside the domain of the region bounded by $C$, then: $$2i\int_C\frac{\mathrm dz}{(z-2+\sqrt3)(z-2-\sqrt3)}=2πi\frac{2i}{2-\sqrt3-2-\sqrt3}=2πi\frac{2i}{-2\sqrt3}=\frac{2π}{\sqrt3}.$$
Using real analysis I get $0$, using complex analysis I get $\dfrac{2π}{\sqrt3}$. What is wrong?
The problem with the real approach is that you make the change of variable $t=\tan\left(\dfrac{\theta}{2}\right)$ for $0 < \theta < 2 \pi$.
This is problematic since your substitution need to be defined and continuous for all $\theta$, but you have a problem when $\theta=\pi$.
Edit: Note that if you split the integral into $\int_0^\pi+\int_\pi^{2 \pi}$, you are going to get the right answer, as for one integral you are going to get $\arctan(- \infty)$ and for the other $\arctan(+\infty)$:
$$\int_0^{2 \pi} \frac{\mathrm{d}θ}{2-\cos \theta}=\int_0^\pi \frac{\mathrm{d}θ}{2-\cos \theta}+\int_\pi ^{2 \pi} \frac{\mathrm{d}θ}{2-\cos \theta}\\ = \lim_{r \to \pi_-} \int_0^r \frac{\mathrm{d}θ}{2-\cos \theta}+ \lim_{w \to \pi_+} \int_w^{2 \pi} \frac{\mathrm{d}θ}{2-\cos \theta}\\= \lim_{r \to \pi_-} \left(\frac{2\tan^-1( \sqrt{3} \tan( \frac{ r}{2}))}{ \sqrt{3}}-0\right)+ \lim_{w \to \pi_+}\left(0- \frac{2\tan^-1( \sqrt{3} \tan( \frac{ r}{2}))}{ \sqrt{3}}\right).$$