I'm currently taking a class on complex analysis, and I'm not sure why the professor uses certain contours to integrate. For example, when trying to integrate $$ \int_{-\infty}^\infty \frac{\sin x}{x(x^2+4)} dx $$ they integrate over the following contour 1
Then, when integrating $$ \int_{0}^\infty \frac{x^a}{x^2+3x+2} dx, 0 < a< 1 $$ they use the keyhole contour. I understand the two functions are different, but I don't understand how or why we integrate over the different contours and not just the usual upper-half circle. Futhermore, what is stopping us from integrating over the upper-half circle alone?
For the integral we typically first rewrite $$\int_{-\infty}^\infty \frac{\sin x\,dx}{x(x^2 + 4)} = \Im \int_{-\infty}^\infty \frac{e^{iz} \,dz}{z (z^2 + 4)}$$ (what happens if we try to evaluate $\int_{-\infty}^\infty \frac{\sin z\,dz}{z(z^2 + 4)}$ using a semicircular contour?). The integrand has a pole at $z = 0$, hence to apply the Residue Theorem to evaluate a contour integral $\oint_\Gamma \frac{e^{iz} \,dz}{z (z^2 + 1)}$, the contour $\Gamma$ must not pass through $0$. Thus, we take a semicircular detour of radius $\epsilon$ about $0$ and compute the limit as $\epsilon \to 0$.
In the second case, $$\int_{0}^\infty \frac{x^a}{x^2+3x+2} dx$$ integrating over a semicircular path poses three problems: