TITLE: Why do keyhole contours work?
QUESTION [6 upvotes]: When evaluating integrals on $(0,\infty)$, we often extend the integral into the complex plane by using a "keyhole contour" with inner radius $\epsilon$ and outer radius $R$:

Credit for the image goes to Linda J. Cummings.
My question is - why do we do this? When we take $R\to\infty$ and $\epsilon\to 0$, the contribution from the circular arcs typically vanish, but what we have remaining is two integrals, i.e $\int\limits_0^\infty$ and $\int\limits_\infty^0$. But these will just cancel out right? When we integrate over the same line in the opposite direction, the result is negated, is it not?
I have not seen anyone rigorously justify this. More worrying is a section on Wikipedia where they conclude
$$\int\limits_R^\epsilon \frac{\sqrt{z}}{z^2+6z+8}\mathrm{d}z=\int\limits_R^\epsilon \frac{-\sqrt{z}}{z^2+6z+8}\mathrm{d}z=\int\limits_\epsilon^R \frac{\sqrt{z}}{z^2+6z+8}\mathrm{d}z$$
But from this surely the integral is trivially zero?!
Why is this not a completely pointless endeavor? Does it have to do with the fact we are taking a limit as the upper bound of the integral goes $\to\infty$? Or does domain reversal work differently in the complex plane? I'd appreciate if someone could explain this.

REPLY [4 votes]: Imagine that in the wild we found a holomorphic function $f$ defined in an open neighborhood of the black contour in picture $A$, but not defined at either of the red points, where there are poles of $f$, and we would like to compute the integral of $f$ around the black contour in picture $A$.

To evaluate the integral of $f$ around the black contour, draw neat centered circles around the red points, following the keyhole contour. (Why centered circles? Because when we began studying complex analysis, line integrals around neat centered circles were amongst the first of the contours we learned how to compute directly.)
In the limit as the "corridors" connecting the circles to the surrounding contour narrow, the integrals over the segments of the keyhole contour that connect the black contour to the blue circles in picture $A$ will cancel each other because of their opposite orientations, as you noted, leaving us with the statement that
$$
\int_{\text{black contour}}f(z)\,dz + \int_{\color{blue}{\text{blue circles}}} f(z)\,dz = 0,
$$
summarized in picture $B$. Moving the integral over the blue circles to the other side of the equation and using the fact that the orientation change switches a minus sign to plus sign, we find that
$$
\int_{\text{black contour}}f(z)\,dz = \int_{\text{black circles}} f(z)\,dz,
$$
which is depicted in picture $C$. Because we know how to compute the right-hand side, we have proven the
Residue Theorem. Suppose that $f$ is holomorphic in an open set containing a nice enough contour $\Gamma$ and its interior, except for a pole at $z_0$ in the interior of $\Gamma$. Then
$$
\int_\Gamma f(z)\,dz = 2\pi i\,\mathrm{res}_{z_0}f.
$$
To summarize, we can turn hopeless integrals over rather arbitrary and hard-to-understand contours $\Gamma$ into hopefully more tractable integrals over neat circles with the keyhole family of contours.