One way to do this would be to install a configuration profile with the proper APN settings which will route all the network traffic through a proxy server. For WiFi, you can set the proxy in the network settings. This way you can easily get data usage statistics using a proxy server such as Squid. Also, it is possible to get per-app data usage by inspecting the HTTP headers of the request, since all apps using CFNetworking pass the app name in the header. I am almost sure this is how Onavo works.
The other way would be to use a VPN connection. The principle is the same, only the technique is different.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…