Why did my contour integration for $\int_0^{\pi/2}\frac{1}{1+\sin x}\,\mathrm{d}x$ fail? $\newcommand{\d}{\,\mathrm{d}}$Integrals of the titular form can be successfully contour-integrated, e.g.: $$\int_0^{2\pi}\frac{1}{4\cos x-5}\d x=-\frac{2}{3}\pi$$Follows from a complex substitution very similar to what I've done here.
However:
$$\int_0^{\pi/2}\frac{1}{1+\sin x}\d x=1$$
Is easily evaluated with Weierstrass's substitution, but out of curiosity I attempted to form a contour integration approach, which failed:
First translate by $\pi/4$:
$$\int_{-\pi/4}^{\pi/4}\frac{1}{1+\sin(x+\pi/4)}\d x=\sqrt{2}\int_{-\pi/4}^{\pi/4}\frac{1}{\sqrt{2}+\sin x+\cos x}\d x$$
Let $\Bbb T=\{z\in\Bbb C:|z|=1\}$. Let $\log$ be the principal branch, and put $x=\frac{1}{4i}\log z$, $z=e^{4ix}$ and $[-\pi/4,\pi/4)\mapsto\Bbb T$:
$$\begin{align}\frac{\sqrt{2}}{4i}\oint_{\Bbb T}\frac{1}{\sqrt{2}+\sin\left(\frac{1}{4i}\log z\right)+\cos\left(\frac{1}{4i}\log z\right)}\frac{\d z}{z}&=\frac{\sqrt{2}}{4i}\oint_{\Bbb T}\frac{1}{\sqrt{2}+z^{1/4}}\frac{\d z}{z}\\&=\pi\frac{\sqrt{2}}{2}\sum\mathrm{Res}\end{align}$$
We have a pole at $z=0$ which is contained by $\Bbb T$. The other pole is when $\exp\frac{1}{4}\log z=-2^{1/2}$:
$$\exp\frac{1}{4}\log z=\exp(\pi i+\frac{1}{2}\ln 2),\,\log z=4\pi i+2\ln 2+8\pi i\cdot k$$
But this doesn't really make sense, since the principal $\log z$ should have imaginary part contained in $(-\pi,\pi]$, but this expression clearly does not satisfy this for any $k$. I will infer from this that no principal solutions exist, and then that $z=0$ is the only pole, with residue $\frac{1}{\sqrt{2}}$. We then arrive at the incorrect answer:
$$\int_0^{\pi/2}\frac{1}{1+\sin x}\d x=\frac{\pi}{2}$$
What did I do wrong?
 A: When you go from the real integral to the complex integral you have made a mistake, for the final result (after accounting for the Jacobian) is more like
$$e^{-i\pi/4}\int_{\mathbb T}\frac1{(\sqrt2+(1+i)z^{1/4})^2z^{3/4}}\,dz$$
so contour integration is not suitable for the integrand in that form (branch cuts get introduced with the fractional powers!). The original bounds are another minus, since the method requires a closed loop in the complex plane, usually corresponding to a whole-period $[0,2\pi]$ integral.
A: You could instead integrate the function $\frac{1}{1+\sin z}$ around a rectangular contour with vertices at $z=0$, $z= \frac{\pi}{2}$, $z= \frac{\pi}{2}+ i R$, and $z= iR$.
Since there are no poles inside the contour, we get $$\int_{0}^{\pi/2} \frac{dx}{1+ \sin x} + \int_{0}^{R} \frac{i \, \mathrm dt}{1+ \sin (\pi/2 + it)} + \int_{\pi/2}^{0} \frac{dt}{1+ \sin (t + iR)} + \int_{R}^{0} \frac{i \, \mathrm dt}{1+ \sin(it)}=0. $$
The second integral is purely imaginary since $\sin(\pi/2 + it) = \cosh t.$
And as $R \to \infty$, the third integral vanishes since $\sin (z)$ grows exponentially as $\Im(z) \to +\infty$.
Therefore, if we equate the real parts on both sides of the equation, we get $$ \begin{align} \int_{0}^{\pi/2} \frac{dx}{1+ \sin x} &= \Re \int_{0}^{\infty} \frac{i \, \mathrm dt}{1+ \sin(it)}  \\ &= \Re \int_{0}^{\infty} \frac{i \, \mathrm dt}{1 + i \sinh t} \\ &= \Re \int_{0}^{\infty}\frac{1-i \sinh t}{1+ \sinh^{2}t}   \, i \, \mathrm dt \\ &=\int_{0}^{\infty} \frac{\sinh t}{\cosh^{2} t} \, \mathrm dt \\ &  = \int_{1}^{\infty}\frac{\mathrm du}{u^{2}} \\ &= 1. \end{align}$$
