how to attach watches for consul using REST API? - consul

I use REST API for accessing consul.
for example here is how I create an entry
curl -X PUT -d #- localhost:8500/v1/kv/example <<< FooValue
I want to add watches to consul that notify my service when a key-value is changed.
The documentation gives example of how http watches look like.
But I am interested in how to attach this watches to consul using REST
I could not find examples of that
Thanks

As per the documentation in consul, currently watches can be added in two ways:
1.As a part of agent configuration
2.Using the watch command.
If you are interested in getting notified when the key value changes, you can do it with the help of 'refresh event' by using event listener. A task scheduler is inherently available to take care of this.
set the property as follows
spring.cloud.consul.config.watch.enabled=true
The watch uses a Spring TaskScheduler to schedule the call to consul.
In the relative path of kv store 'config/application/data' put your configuration,
for eg-> example: FooValue
You will get log info 'keys changed: [example: BarValue]' when you change the kv store data.

After reading source code of the watch command implementation, I found the way to do it in HTTP API. Note that this is undocumented, use it at your own risk.
Suppose to monitor the changes of KV under the path of /xxx.
First emit the following request.
GET http://your-consul.com:8500/v1/kv/xxx?recurse=true&stale=true
Consul would response this request immediately.
Connection: keep-alive
Content-Length: 209
Content-Type: application/json
Date: Sun, 13 Nov 2022 00:27:04 GMT
Vary: Accept-Encoding
Via: 1.1 proxy-server (squid/5.5)
X-Cache: MISS from proxy-server
X-Cache-Lookup: MISS from proxy-server:3128
X-Consul-Default-Acl-Policy: allow
X-Consul-Index: 3386097
X-Consul-Knownleader: true
X-Consul-Lastcontact: 0
X-Consul-Query-Backend: blocking-query
The X-Consul-Index http header is what we are looking for.
Then emits another request with the index.
GET http://your-consul.com:8500/v1/kv/xxx?index=3386097&recurse=true&stale=true
Now this request will stuck till a change occurs.
When this request is responsed, the payload contains the details of all children KV at given path. X-Consul-Index will also be included in response HTTP header, you need update the index query string if you want to watch again.
So generally, you can watch changes via HTTP API as what the consul tool does. Note that you need implement the logic carefully, to handle timeout / network error / etc.

Related

JMeter & socket.io - I can see the message I want, but the socket plugin is not showing what I expect

Here is the socket message I see in the browser debugger console:
More illustrative, perhaps:
I call an API operation that triggers this message over a socket.
What I Tried
To preclude inaccuracies, I started 2 instances of JMeter.
REST API call.
Revised version of the GitHub JMeter example of sockets.io, in which I just call a WebSocket Sampler repeatedly on wss://events.dev.myserver.com:443/socket.io/?EIO=4&transport=websocket.
I kicked off (2).
While that was running, I kicked off (1).
Expected
Eventually, (1) should show me a sampler in the View Results Tree with the message in the screenshot ("42" - GAME_STARTED)
Actual
The only messages I see look like this:
This is really all I want to do: run the appropriate sampler, a sufficient time after making the API call, to get the message.
Update
We succeeded in finding the message using python-socketio:
sio.connect("https://events.dev.server.com", transports='websocket',
headers={'Sec-WebSocket-Extensions: permessage-deflate', 'Sec-Fetch-Dest: websocket',
'Sec-Fetch-Mode: websocket',
'Cookie: ABCSESSIONDEV=NTI3MzkwNWUtMTJmNS00Y2U0LTk1NGUtMjQ2Mzk5OTYxZWE0'})
And here is the output:
Received packet MESSAGE data 2["message","{\"locationId\":110,\"name\":\"GAME_STARTED\",\"payload\":{\"id\":146724,\"boxId\":2002,\"userId\":419,\"createdAt\":\"2022-03-02T14:35:31\",\"lastModifiedAt\":\"2022-03-02T14:35:36.752\",\"completedAt\":\"2022-03-02T14:35:36.621\",\"activationMethod\":\"TAG\",\"nfcTagId\":\"xxxxxx\",\"gameCount\":1,\"app\":false}}"]
I would like to use the websocket plugin to do this in JMeter now.
tried adding Cookie to WebSocket call - only sids, no messages.
tried adding Cookie to an HTTPS request (like the above code) - 400, bad request.
Take a look at other fields of the HTTP Request, in particular HTTP Headers, most probably your JMeter request is missing some essential information.
My expectation is that in order to "start the game" (whatever it means) you need to open the page in the browser, authorize somehow, follow the steps of the protocol upgrade mechanism, etc. to wit exactly mimic what real browser does, all the request sequence which is prior to starting the game.
You might need to correlate dynamic parameters, add HTTP Header Manager, add HTTP Cookie Manager, etc.

Hot to define a fallback mapping temmplate in AWS Api Gateway?

I have a POST method exposed though an API in AWS API Gateway. It would process a certain JSON. But there is a weird requirement: I am supposed to accept anything and return always a 200 status code (as long as the service is available and working of course).
So if I receive a call with Content-Type: image/jpeg, application/xml or what/ever, I must be able to map the request to my Lambda function.
I have achieved to map any request to a custom type using the VTL (Velocity Template Language), the issue is that I must specify every single Content/Type I want to support in the Integration Request.
I would like to define a fallback mapping template for every Content-Type that is not among those that I have already defined. It is not allowed to specify "*/*" as Content-Type.
I can not use When no template matches the request Content-Type header option as passthrough because b y default the lambda function tries to deserialize the body from a JSON format.
Any ideas?
I think you should enable the When no template matches the request Content-Type header option. As documented your Lambda will get an API Gateway event. This event has a headers field that, on a POST should contain a content-type field. If your code understands the content-type then handle it. If not, return a response that has the correct status code and whatever is appropriate. This is not on the API gateway side but rather on the Lambda side.
See this blog for some additional ideas.

How to POST data in in REST API using J meter

by HTTP request
enter image description herebase url is: http://124.124.21.165:8093/FieldApp/Service.svc/
Your configuration looks good, I would suggest just adding a HTTP Header Manager to send Content-Type header with the value of application/json
Also it would be good to add View Results Tree listener to inspect request and response details (don't forget to remove or disable it when it comes to running the load test assuming large amount of users)
Check out Testing SOAP/REST Web Services Using JMeter test plan for step-by-step instructions

Windows phone Web service call using http request

Some time before I faced an issue while making network request. When I call a same webservice multiple times.I get same set of data. It seems that the Network library caches the http requests-responses. Initially I added NO-CACHE in the header, but it did not work.Hence I added a timestamp field as a workaround which works. But I want to know if there is any better solution to this.
Could you add a timestamp to the end of the URL you are requesting? e.g. http://somewhere.com/Request?2012-04-17-10-09-31 or http://somewhere.com/Request?param=val&ts=2012-04-17-10-09-31

Access log replay for load testing? Jmeter Pitfalls and Competitors

Context
We wish to use "replay" web server access logs to generate load tests. JMeter had come to mind as I'd recently read blog posts about using jmeter in the cloud (e.g. firing up a number of Amazon EC2 instances to generate the load)
For years I had heard of JMeter's ability to replay access logs, but in reviewing this feature I found the following.
Access Log Sampler
DOES:
recreate sessions, i.e. handle the jsessionId token (thought it tries to approximate sessions by IP address);
DOES NOT:
handle POST data (even if you could configure apache/tomcat to write out post data to the access log, jmeter access log sampler only handles 'common' log format).
Post data would go a long way toward recreating actual load.
Additionally, the documentation describes the Access Log Sampler as "alpha code" even though it's 8 years old. It doesn't seem actively maintained. (That's longer than Gmail's beta.)
HttpPerf
Another blog post pointed me to the httpperf tool. I've started to read up on it:
blog: http://www.igvita.com/2008/09/30/load-testing-with-log-replay/
httpperf: http://code.google.com/p/httperf/
Summary
What's the best way to generate load testing 'scripts' from real user data?
What has worked best for you?
Pros and cons of various tools?
JMeter + HTTP Raw Request + Raw Data Source for me works well
I will describe how do we solve this problem using our own LT tool called Yandex Tank
It can handle simple access.log but only 'GET' requests, too. When there's a need to make other types of requests, we use other ammo formats (ammo is a file containing all the requests that we gonna send to our server). Example:
342
POST / HTTP/1.1^M
Host: xxx.xxx.xxx.xxx:8080^M
Connection: keep-alive^M
Keep-Alive: 300^M
Content-Type: multipart/form-data; boundary=AGHTUNG^M
Content-Length: 1400^M
Connection: Close^M
^M
--AGHTUNG^M
Content-Disposition: form-data; name="fp"; filename="fp_tank"^M
Content-Type: application/octet-stream^M
Content-Transfer-Encoding: binary^M
...
--AGHTUNG--^M
A number ('342') on the first line is the size of a following request. Request is in it's raw format. You could write a simple script in your favourite language that generates such ammo files from your access.log and then use it for load testing.
Such ammo format makes it really flexible. For example, this code generates ammo from FCGI logs (POST bodies are encoded in Base64). But on the other hand you will need to handle sessions manually.
You can easily replay access logs with POST data using ZebraTester. It has many plugins similar to JMeter and also ability to add inline scripts using which you can easily target POST payload, URLs, timestamps, etc. from the access logs. You can run load tests directly from the tool locally or copy the recorded script to the SaaS portal to run massive million virtual user load tests

Resources