Track outgoing REST call in Java Function App inside Application Insights - java

From what I understand Azure Application Insights is able to automatically track and log incoming HTTP requests to an application.
However, my usecase is I need to track an outgoing HTTP request (that I make programmatically).
I have a Java Function App (with an HTTP trigger) running in Azure with App Insights attached to it. It logs logs the incoming HTTP request to the function app. Once triggered, my app makes a REST call to a 3rd party REST API. I would like to log this call either as a request or as a dependency under the function app in App Insights
Pointers on how to do this are appreciated.
The documentation seems vague. I looked at a similar question that is answered for C# as well as this, but my question is JAVA and Function App specific and I want the request to be logged and correlated correctly to the above Function APP

If you look at https://learn.microsoft.com/en-us/azure/azure-monitor/app/java-in-process-agent#instrumentation-libraries , it lists a bunch of autocollected dependencies
You just have to use one of the autocollected HTTP libraries (e.g OkHttp), and the outgoing http calls should be automatically tracked.

After some back and forth with Microsoft about this, it turns out Azure functions has a specific set of configuration parameters that needs to be put in. I am posting the windows one as thats what I used, but the linux one is in the link as well.
(Windows):
XDT_MicrosoftApplicationInsights_Java -> 1
ApplicationInsightsAgent_EXTENSION_VERSION -> ~2
https://learn.microsoft.com/en-us/azure/azure-monitor/app/monitor-functions#how-to-enable-distributed-tracing-for-java-function-apps

Related

make a request from java application to a web server url

I made a web-server that runs on an esp32(LAN) and I have made it possible to send information to the esp itself from the servers url, (example : 192.168.1.39/?userInput=123), the number 123 is what I want to send from the application depends on the user's input (I compiled it to a packet of 8bits) so max number is 255, the server has an XML and some basic UI for viewing the information passed back and forth, I wanna be able to send the so called packet to the server and it passing it to the esp32 with almost no delay, I used google firebase before but it has way too much delay for it to be usable, I tried using a WebView and loading the URL with the number from the packet, I ran out of ideas on how to approach this would love some advice :)
I tried searching other questions here on the site, asked friends/teachers, watched a few tutorials and asked chatGPT for help but nothing was helpful.
From reading your question it seems you are lost setting up server and client at the same time. Divide the tasks into chunks you can digest:
First, setup your ESP32 webserver. Follow a tutorial like https://randomnerdtutorials.com/esp32-web-server-arduino-ide/ and test it using a normal web browser. It can be used to run GET requests easily, and the amount of data you need to transfer that should definitely be enough. Alternatively you can use curl to send client requests.
Next, develop your java client to send the appropriate request. You can test the behaviour using any standard webserver and check the logs.
Finally put the ESP32 url into your client and see whether they work together.

Send data to Matlab from Android/Java

I spent some time searching for a way to send data from a android application to Matlab - with no approach. I would prefer to do it with JSON via a Restful webservice.
I probably have the wrong concept in mind how this is going to work.
Matlab should be running/waiting for POST requests from my android device to receive the data, bring it into matlab form from json , progress it and send it back - than wait again for new requests.
The "RESTful web service" like "webread" seems not to wait for incoming data and go active for them.
How to let Matlab listen for incoming data with json ?
or how to let Matlab receive data from Android/java based programms ?
Do i need another frameworks, api's or even a server with Database to get that done ?
Can anyone give me some hints ?
Approach 1:
Matlab also provides Matlab Mobile https://de.mathworks.com/products/matlab-mobile.html, which is capable of executing Matlab code from your device, however, it is not possible to send images to Matlab.
However, you can use WebCam https://play.google.com/store/apps/details?id=com.pas.webcam&hl=en and open up a Server, which is pretty straightforward. You can run the app in the background and then connect to Matlab via Matlab-Mobile, and access it via your IP address and usually Port 8080.
Approach 2:
You can use a WebSocket -Server which is implemented here:
https://de.mathworks.com/matlabcentral/fileexchange/50040-jebej-matlabwebsocket
For more information on how to get it to run you can follow the directions given on the GitHub readme, here: https://github.com/jebej/MatlabWebSocket
A WebSocket Server is on the highest layer of the 7th layer (application layer) of the OSI model https://en.wikipedia.org/wiki/OSI_model and builds op on the 4th layer (TCP). However, you do not need to specify such things as buffer size etc.
The following example code is directly taken from the example code from the GitHub project. To fulfill the desired outcome in the Android application it is the best approach to rebuild the client application on Android.
Echo Server:
classdef EchoServer < WebSocketServer
%ECHOSERVER Summary of this class goes here
% Detailed explanation goes here
properties
end
methods
function obj = EchoServer(varargin)
%Constructor
obj#WebSocketServer(varargin{:});
end
end
methods (Access = protected)
function onOpen(obj,conn,message)
fprintf('%s\n',message)
end
function onTextMessage(obj,conn,message)
% This function sends an echo back to the client
conn.send(message); % Echo
end
function onBinaryMessage(obj,conn,bytearray)
% This function sends an echo back to the client
conn.send(bytearray); % Echo
end
function onError(obj,conn,message)
fprintf('%s\n',message)
end
function onClose(obj,conn,message)
fprintf('%s\n',message)
end
end
end
To run it in MATLAB type:
s = EchoServer(30000);
This will then utilize the port 30000 on your local machine.
On Android simply create a WebSocket-Client and use your URI, which you can find out by using ipconfig (windows) or ifconig (Linux). In Android the uri should like the following:
ws://192.168.1.102:30000
Where the IP address may change according to your IP address
Here are my 2 cents.
Your approach seems right.
Step 1: You need to run a web server using MATLAB on your device. By going through Web Server, it looks like you can use it to run webserver, and execute a .m file when a POST or GET request is made to your server.
Step 2: Lets say that your server is accepting requests on port 8080. From your Android device, if you are on same network then you can make a HTTP POST request to http://your.ip.address:8080 and extract the data and execute your code in .m file.
Note: You can also get a public URL for your local server running on the device using ngrok utility. Then make a POST request to that public URL. You need not be on the same network then in order to make a request. Here is some explanation: Accessing localhost from android over wifi.
Edit:
Additional question says:
Matlab is possible of receiving data via TCP/IP client, but how does the android site need to do the POST/GET algorithm and how can response Matlab responsible to it?
Let me rephrase what I understand. Firstly, you want to know how from Android code, one can do a POST/GET request and secondly how will Matlab respond to the request?
In Android you can make a POST request in background thread either using AsyncTask (Android HttpURLConnection with AsyncTask Tutorial) or if you want to do it properly, you can use Retrofit library to do POST/GET calls (Using Retrofit 2.x as REST client - Tutorial
).
When using WebServer as mentioned in the link previously, when the .m file gets executed on the POST call, you can send the response to the POST request from there. On Android, where you initiated the call, you can receive the callback.
Hope this helps a bit.
Have you tried the Android Support Package for MATLAB?
While it doesn't allow access to the camera, when used with MATLAB Mobile it does provide access to:
Acceleration on 3-axes
Magnetic field on 3-axes
Angular velocity on 3-axes
Azimuth, roll, pitch
Latitude, longitude, altitude, horizontal accuracy, speed, and course
Here's a link with more detailed information on how to get started.

Monitor database with GWT

Maybe I'm overthinking this but I'd like some advice. Customers can place an order inside my GWT application and on a secondary computer I want to monitor those submittals inside th eGWT application and flash an alarm every time an order is submitted, provided the user has OK'd this. I cant figure out the best way to do this. Orders are submitted to a mysql database if that makes any difference. Does anyone have a suggestion on what to do or try?
There are two options: 1) polling or 2) pushing which would allow your server (in the servlet handling the GWT request) to notify you (after the order is successfully placed).
In 1) polling, the client (meaning the browser you are using to monitor the app) will periodically call the server to see if there is data waiting. It may be more resource intensive as many calls are made for infrequent data. It may also be slower due to the delay between calls. If only your monitoring client is calling though it wouldn't be so resource intensive.
In 2) pushing, the client will make a request and the request will be held open until there is data. It is less resource intensive and can be faster. Once data is returned, the client sends another request (this is long polling). Alternatively, streaming is an option where the server doesn't sent a complete request and just keeps sending data. This streaming option requires a specific client-/browser-specific implementation though. If it's just you monitoring though, you should know the client and could set it up specifically for that.
See the demo project in GWT Event Service
Here is the documentation (user manual) for it.
Also see GWT Server Push FAQ
There are other ways of doing it other than GWT Event Service of course. Just google "GWT server push" and you'll find comet, DWR, etc., and if you are using Google's App Engine the Channel API

Apps updating their website

I have an app [Android] that will basically give me numbers and I need those numbers to be sent to a website hosted via "Apache" (Haven't worked that out yet).
The reason why I am asking it here is because I do not know the first thing about making applications connect to a website in way where it updates a variable each time it is calculated on that app.
Any few tips or advises would be great! [I know how to use HTML, a bit of java scripting, MySQL and lots of java]
What language should I be using. How does the server work and how do we make the app and the website work concurrently, or in parallel?
This is question is too broad, still I am listing few ways
Generate the data on application
Send a custom broadcast whenever the data is generated.
Start an Intent service from the Broadcast Receiver.
Post to server i.e to a REST or SOAP service using Http post and basic name value pairs.
At your server side, you need a service which receives the posted data and saves it to the database or any where you want.

What is the established way to request data from an android device

I currently have an small application that I have been using to learn java/android programming. Right now I have a setup were the app on one phone sends a request (via sms) to another phone running the same app. The remote phone receives the request and sends back some info. Next I would like to try this from the web. Is there an established "best" way to to this?
I was thinking I would have a web server send requests to the device via google cloud messaging and then have the device return the data directly to the web server. (Not that I really know how to do any of that just yet).
I see that there is a google cloud messaging return path (send messages from the device to the google cloud server, but it seems very new, do I need something like that? The main thing I want is to be able to ask the phone to do something when I want, not have it poll to see if there is a request, or just periodically update some status.
UPDATE:
Thanks to the answers below for confirming to me that I was on the right track.
I now have some basic functionality.
I started out using this gcm android demo code
https://code.google.com/p/gcm/source/browse/#git%2Fgcm-client%2Fsrc%2Fcom%2Fgoogle%2Fandroid%2Fgcm%2Fdemo%2Fapp%253Fstate%253Dclosed
and this ruby gem
https://github.com/spacialdb/gcm/blob/master/README.md
between the above two I was able to send a message to my phone pretty easily.
To get the round trip working, I setup a very simple rails app on heroku.
I included a modified version of the sample code in the gcm gem in a controller and then used
HttpPatch (needed for rails 4) to send a post/patch from my phone to my web app, the controller then echoes the message back to my phone.
I guess it would be nice to get the two way gcm stuff to work, but I am not sure there are any gems that handle that, and I am not qualified to handle a task like that :)
I would say it's the right call: Google Cloud Messaging for Android
From the site Android Developer:
This could be a lightweight message telling your app there is new data
to be fetched from the server (for instance, a movie uploaded by a
friend), or it could be a message containing up to 4kb of payload data
(so apps like instant messaging can consume the message directly).
In this case you don't want to fetch data from the server but you want to send them.
You can send them in different ways. I would suggest, since you are learning, to try a RESTful solution using one of the implementation of JAX-RS.
As a short and direct answer for beginner : GCM (Google Cloud Messaging) would solve your issue. However, if your app turned out to be something bigger, other more technical and complicated solutions are present too.
see this link.

Categories