Sunday, 25 December 2022

How to Monitor Java application using prometheus?

 




Most Used Docker Commands:Interview Question

Pre-Requisite: 


What is Prometheus?


Prometheus is a free software application used for
 event monitoring and alerting. It records real-time metrics in a time series database (allowing for high dimensionality) built using a HTTP pull model, with flexible queries and real-time alerting.

Monitor Java Applications with Prometheus

This section will explain how an existing Java application can be updated to publish metrics and monitored by Prometheus.

Prometheus collects metrics from monitored targets by scraping metrics HTTP endpoints on these targets.

As discussed earlier, Prometheus collects metrics from monitored targets by scraping from an HTTP endpoint on these targets. By default, these metrics are expected to be published at /metrics. Any existing Java application can be updated to publish Prometheus-style metrics at this endpoint.

An earlier chapter explained a simple Java EE application that talks to a MySQL database. This application also publishes Prometheus-style metrics for the underlying JVM at /metrics. It also publishes application-specific metrics such as total number of times GET / and GET /{id} is called.

The complete set of JVM metrics are explained at https://github.com/prometheus/client_java. Refer to https://github.com/arun-gupta/docker-javaee/tree/master/employees/src/main/java/org/javaee/samples/employees/metrics for more details on how these metrics are enabled.

Start Java application

  1. Use the Compose file to deploy a simple the Java EE application. This will start WildFly Swarm application and MySQL database.

    docker stack deploy --compose-file=docker-compose.yml webapp

    This will create webapp_default overlay network, and start the webapp_web and webapp_db services.

  2. Verify the network:

    $ docker network ls
    NETWORK ID          NAME                DRIVER              SCOPE
    u6ybdaqx5h5y        webapp_default      overlay             swarm

    Other networks may be shown here as well.

  3. Verify the services:

    $ docker service ls
    ID                  NAME                MODE                REPLICAS            IMAGE                            PORTS
    ucztcpf1vw0a        webapp_db           replicated          1/1                 mysql:8                          *:3306->3306/tcp
    jttfgvr09kre        webapp_web          replicated          1/1                 arungupta/docker-javaee:latest   *:8080->8080/tcp,*:9990->9990/tcp
  4. Verify that the endpoint is accessible:

    $ curl http://localhost:8080/resources/employees
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?><collection><employee><id>1</id><name>Penny</name></employee><employee><id>2</id><name>Sheldon</name></employee><employee><id>3</id><name>Amy</name></employee><employee><id>4</id><name>Leonard</name></employee><employee><id>5</id><name>Bernadette</name></employee><employee><id>6</id><name>Raj</name></employee><employee><id>7</id><name>Howard</name></employee><employee><id>8</id><name>Priya</name></employee></collection>
  5. Access the metrics published by the endpoint using curl http://localhost:8080/metrics to see the output:

    # HELP jvm_info JVM version info
    # TYPE jvm_info gauge
    jvm_info{version="1.8.0_141-8u141-b15-1~deb9u1-b15",vendor="Oracle Corporation",} 1.0
    # HELP jvm_gc_collection_seconds Time spent in a given JVM garbage collector in seconds.
    # TYPE jvm_gc_collection_seconds summary
    jvm_gc_collection_seconds_count{gc="PS Scavenge",} 25.0
    jvm_gc_collection_seconds_sum{gc="PS Scavenge",} 0.386
    jvm_gc_collection_seconds_count{gc="PS MarkSweep",} 6.0
    jvm_gc_collection_seconds_sum{gc="PS MarkSweep",} 0.546
    # HELP process_cpu_seconds_total Total user and system CPU time spent in seconds.
    # TYPE process_cpu_seconds_total counter
    process_cpu_seconds_total 25.5
    # HELP process_start_time_seconds Start time of the process since unix epoch in seconds.
    # TYPE process_start_time_seconds gauge
    process_start_time_seconds 1.508056592419E9
    # HELP process_open_fds Number of open file descriptors.
    # TYPE process_open_fds gauge
    process_open_fds 499.0
    # HELP process_max_fds Maximum number of open file descriptors.
    # TYPE process_max_fds gauge
    process_max_fds 1048576.0
    # HELP process_virtual_memory_bytes Virtual memory size in bytes.
    # TYPE process_virtual_memory_bytes gauge
    process_virtual_memory_bytes 4.244393984E9
    # HELP process_resident_memory_bytes Resident memory size in bytes.
    # TYPE process_resident_memory_bytes gauge
    process_resident_memory_bytes 5.06601472E8
    # HELP jvm_classes_loaded The number of classes that are currently loaded in the JVM
    # TYPE jvm_classes_loaded gauge
    jvm_classes_loaded 13096.0
    # HELP jvm_classes_loaded_total The total number of classes that have been loaded since the JVM has started execution
    # TYPE jvm_classes_loaded_total counter
    jvm_classes_loaded_total 13096.0
    # HELP jvm_classes_unloaded_total The total number of classes that have been unloaded since the JVM has started execution
    # TYPE jvm_classes_unloaded_total counter
    jvm_classes_unloaded_total 0.0
    # HELP jvm_threads_current Current thread count of a JVM
    # TYPE jvm_threads_current gauge
    jvm_threads_current 60.0
    # HELP jvm_threads_daemon Daemon thread count of a JVM
    # TYPE jvm_threads_daemon gauge
    jvm_threads_daemon 12.0
    # HELP jvm_threads_peak Peak thread count of a JVM
    # TYPE jvm_threads_peak gauge
    jvm_threads_peak 67.0
    # HELP jvm_threads_started_total Started thread count of a JVM
    # TYPE jvm_threads_started_total counter
    jvm_threads_started_total 93.0
    # HELP jvm_threads_deadlocked Cycles of JVM-threads that are in deadlock waiting to acquire object monitors or ownable synchronizers
    # TYPE jvm_threads_deadlocked gauge
    jvm_threads_deadlocked 0.0
    # HELP jvm_threads_deadlocked_monitor Cycles of JVM-threads that are in deadlock waiting to acquire object monitors
    # TYPE jvm_threads_deadlocked_monitor gauge
    jvm_threads_deadlocked_monitor 0.0
    # HELP jvm_memory_bytes_used Used bytes of a given JVM memory area.
    # TYPE jvm_memory_bytes_used gauge
    jvm_memory_bytes_used{area="heap",} 1.2072508E8
    jvm_memory_bytes_used{area="nonheap",} 9.3550048E7
    # HELP jvm_memory_bytes_committed Committed (bytes) of a given JVM memory area.
    # TYPE jvm_memory_bytes_committed gauge
    jvm_memory_bytes_committed{area="heap",} 2.69484032E8
    jvm_memory_bytes_committed{area="nonheap",} 1.0133504E8
    # HELP jvm_memory_bytes_max Max (bytes) of a given JVM memory area.
    # TYPE jvm_memory_bytes_max gauge
    jvm_memory_bytes_max{area="heap",} 4.66092032E8
    jvm_memory_bytes_max{area="nonheap",} -1.0
    # HELP jvm_memory_pool_bytes_used Used bytes of a given JVM memory pool.
    # TYPE jvm_memory_pool_bytes_used gauge
    jvm_memory_pool_bytes_used{pool="Code Cache",} 1.4589888E7
    jvm_memory_pool_bytes_used{pool="Metaspace",} 6.9998048E7
    jvm_memory_pool_bytes_used{pool="Compressed Class Space",} 8962112.0
    jvm_memory_pool_bytes_used{pool="PS Eden Space",} 2.3732032E7
    jvm_memory_pool_bytes_used{pool="PS Survivor Space",} 6073592.0
    jvm_memory_pool_bytes_used{pool="PS Old Gen",} 9.0919456E7
    # HELP jvm_memory_pool_bytes_committed Committed bytes of a given JVM memory pool.
    # TYPE jvm_memory_pool_bytes_committed gauge
    jvm_memory_pool_bytes_committed{pool="Code Cache",} 1.47456E7
    jvm_memory_pool_bytes_committed{pool="Metaspace",} 7.5800576E7
    jvm_memory_pool_bytes_committed{pool="Compressed Class Space",} 1.0788864E7
    jvm_memory_pool_bytes_committed{pool="PS Eden Space",} 9.2274688E7
    jvm_memory_pool_bytes_committed{pool="PS Survivor Space",} 3.8797312E7
    jvm_memory_pool_bytes_committed{pool="PS Old Gen",} 1.38412032E8
    # HELP jvm_memory_pool_bytes_max Max bytes of a given JVM memory pool.
    # TYPE jvm_memory_pool_bytes_max gauge
    jvm_memory_pool_bytes_max{pool="Code Cache",} 2.5165824E8
    jvm_memory_pool_bytes_max{pool="Metaspace",} -1.0
    jvm_memory_pool_bytes_max{pool="Compressed Class Space",} 1.073741824E9
    jvm_memory_pool_bytes_max{pool="PS Eden Space",} 9.699328E7
    jvm_memory_pool_bytes_max{pool="PS Survivor Space",} 3.8797312E7
    jvm_memory_pool_bytes_max{pool="PS Old Gen",} 3.49700096E8

    It shows all the JVM metrics that are published by the Prometheus JVM Client. The metrics generated by the application are not shown yet. It requires for the application to be accessed first.

Let’s access the JVM metrics in Prometheus dashboard first, and then we’ll access the app to show app-specific metrics.

Start Prometheus service

  1. Make sure to terminate any previously running Prometheus endpoints:

    docker service rm metrics
  2. Create a directory prometheus and change into that directory.

  3. Create a text file prometheus.yml and add the following content:

    global:
      scrape_interval: 10s
    scrape_configs:
      - job_name: 'webapp'
        dns_sd_configs:
          - names: ['tasks.webapp_web']
            type: 'A'
            port: 8080

    This defines the configuration for the HTTP endpoint that publishes Prometheus-style metrics from the Java application.

  4. Start Prometheus service:

    docker service create \
      --replicas 1 \
      --network webapp_default \
      --name metrics \
      --mount type=bind,source=`pwd`/prometheus.yml,destination=/etc/prometheus/prometheus.yml \
      --publish 9090:9090 \
      prom/prometheus

    Note, this service is using the webapp_default overlay network that is created when the application stack was deployed.

  5. Access Prometheus dashboard at http://localhost:9090

  6. Check the configured targets at http://localhost:9090/targets:

    prometheus metrics target4

    It shows that the application metrics HTTP endpoint is configured as a Prometheus target.

View application metrics

  1. On Prometheus dashboard, click on -insert metric at cursor- to see the list of metrics available:

    prometheus metrics12

    JVM metrics shown earlier are displayed here as well.

  2. Select jvm_memory_pool_bytes_used metric and click on Execute to view the metric.

    prometheus metrics13
  3. Select Graph to view the graphical representation

    prometheus metrics14
  4. Now access the application using curl http://localhost:8080/resources/employees a few times.

  5. Refresh Prometheus dashboard and see the updated list of metrics:

    prometheus metrics15

    Note, app* and requests* that are generated by the application.

  6. Select requests_get_all metric and view the graph:

    prometheus metrics16
  7. Access the application a few times using curl http://localhost:8080/resources/employees/5 and then watch the requests_get_one metric.

****************************************

TOP 15 BDD - CUCUMBER Interview Q&A


************************************************

✍️AUTHORLinkedIn Profile

************************************************

Learn (API-Microservice)Testing+ Selenium UI Automation-SDET with Self Paced Videos prepared by FAANG employees and LIVE Doubt Session 

SDET TRANING VIDEOS AVAILABLE with Live Doubt Session(course-1 below,API TRaining Videos With Class Notes and Coding Set) and (API+UI, both course-1 & 2 below) Check Training Page for Course Content or reach out @whatsapp +91-9619094122. 
This includes classnotes, 300+ interview questions, 3 projects, Java Coding question set for product companies along with career guidance from FAANG employees for Automation and SDET.

For more details whatsapp : https://lnkd.in/dnBWDM33

*************************************************

SeleniumWebdriver Automation Testing Interview Questions:

https://automationreinvented.blogspot.com/search/label/SeleniumWebdriver

API Testing Interview Question Set:

https://automationreinvented.blogspot.com/2022/03/top-80-api-testing-interview-questions.html

DevOps Interview Q&A:

https://automationreinvented.blogspot.com/2021/11/top-11-devops-interview-questions-and.html 

Kubernetes Interview Question Set

https://automationreinvented.blogspot.com/search/label/Kubernetes

Docker Interview Question Set

https://automationreinvented.blogspot.com/Docker

Linux Interview question Set

https://automationreinvented.blogspot.com/search/label/Linux

Automation Testing/SDET Framework Design

https://automationreinvented.blogspot.com/search/label/FrameworkDesign

Java Related Interview Question Set

https://automationreinvented.blogspot.com/search/label/Java

GIT Interview Question Set:

https://automationreinvented.blogspot.com/2021/09/top-40-git-interview-questions-and.html

Coding Interview Question Set:

https://automationreinvented.blogspot.com/search/label/Coding%20Questions

Mobile Testing Interview Question Set:

https://automationreinvented.blogspot.com/search/label/Mobile%20Testing

Python Interview Question Set for QAE - SDET - SDE:

https://automationreinvented.blogspot.com/search/label/Python


Thursday, 22 December 2022

Most Asked Selenium Python Interview Questions and Answers?

 



If you are preparing for Selenium-Python Interview then also check below Q&A:
Python Interview Question & Answers Set - 01

Python Interview Q&A Set - 02

Python Interview Q&A Set - 03


Introduction


Python supports the Object-Oriented Programming approach to establish the applications. It is simple and easy to learn and provides lots of high-level data structures. It is an open-source language.

Selenium Python bindings provides a simple API to write functional/acceptance tests using Selenium WebDriver. Through Selenium Python API you can access all functionalities of Selenium WebDriver in an intuitive way.

Selenium Python bindings provide a convenient API to access Selenium WebDrivers like Firefox, Ie, Chrome, Remote etc. The current supported Python versions are 3.5 and above.

If you are new to PYTHON then first check the post on How to Install Python on windows and mac?, once installation is complete then only checkout the below Q&A

How to Configure Selenium using Python

There are following steps to configure Selenium using Python:

  • Download and install Python
  • Install Selenium libraries in Python: use "pip install selenium" command
  • Download and install PyCharm
  • Create a new project and write the Selenium test script
  • Run and validate the test scripts.

Let's start going through the Q&A:


How to start the session ?

driver = webdriver.Chrome(service=ChromeService(executable_path
=ChromeDriverManager().install()))

How to write the first Python Selenium script with Basic Commands?

Ans: Click the link HERE for the entire code 


How to use ChromeDriver ?

Download the latest chromedriver from the download page. Unzip the file:

unzip chromedriver_linux32_x.x.x.x.zip

You should see an chromedriver executable. Now you can create an instance of Chrome WebDriver like this:

driver = webdriver.Chrome(executable_path="/path/to/chromedriver")

The rest of the examples should work as given in the other documentation.

Does Selenium 2 support XPath 2.0 ?

Ref: http://seleniumhq.org/docs/03_webdriver.html#how-xpath-works-in-webdriver

Selenium delegates XPath queries down to the browser’s own XPath engine, so Selenium support XPath supports whatever the browser supports. In browsers which don’t have native XPath engines (IE 6,7,8), Selenium supports XPath 1.0 only.

How do I scroll down to the bottom of a page ?

You can use the execute_script method to execute javascript on the loaded page. You can call the JavaScript API to scroll to the bottom or any other position of a page.

Here is an example to scroll to the bottom of a page:

driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

The window object in DOM has a scrollTo method is to scroll to any position of an open window. scrollHeight is a common property for all elements. document.body.scrollHeight will give the height of the entire body of the page.

How to auto save files using a custom Firefox profile?

The first step is to identify the type of file you want to auto save.

Lets understand the process with demo code and detailed steps check the Post HERE

How to upload files into file inputs ?

Ans: Select the <input type="file"> element and call the send_keys() method passing the file path, either the path relative to the test script, or an absolute path. Keep in mind the differences in path names between Windows and Unix systems.

How to use firebug with Firefox ?

First download the Firebug XPI file, later you call the add_extension method available for the firefox profile:

from selenium import webdriver

fp = webdriver.FirefoxProfile()

fp.add_extension(extension='firebug-1.8.4.xpi')
fp.set_preference("extensions.firebug.currentVersion", "1.8.4") #Avoid startup screen
browser = webdriver.Firefox(firefox_profile=fp)

Using Selenium with remote WebDriver


Ans: Click HERE to understand the steps with the demo code

How to use Implicit wait in Selenium with python?

An implicit wait tells WebDriver to poll the DOM for a certain amount of time when trying to find any element (or elements) not immediately available. The default setting is 0 (zero). Once set, an implicit wait is set for the life of the WebDriver object.

from selenium import webdriver

driver = webdriver.Firefox()
driver.implicitly_wait(10) # seconds
driver.get("https://automationreinvented.blogspot.com/")
myDynamicElement = driver.find_element_by_id("myDynamicElement")

How do I perform click and hold with Selenium-Python?


This method combines moving the mouse to the center of an element by pressing the left mouse button. This is useful for focusing a specific element:

clickable = driver.find_element(By.ID, "clickable")
    ActionChains(driver)\
        .click_and_hold(clickable)\
        .perform()

How to take screenshot of the current window ?

Use the save_screenshot method provided by the webdriver:

Click the link HERE with all the steps and demo code


*******************************************************************

For any doubts or career guidance, reach out to me 


********************************************************************

****************************************

TOP 15 BDD - CUCUMBER Interview Q&A


************************************************

✍️AUTHORLinkedIn Profile

************************************************

Learn (API-Microservice)Testing+ Selenium UI Automation-SDET with Self Paced Videos prepared by FAANG employees and LIVE Doubt Session 

SDET TRANING VIDEOS AVAILABLE with Live Doubt Session(course-1 below,API TRaining Videos With Class Notes and Coding Set) and (API+UI, both course-1 & 2 below) Check Training Page for Course Content or reach out @whatsapp +91-9619094122. 
This includes classnotes, 300+ interview questions, 3 projects, Java Coding question set for product companies along with career guidance from FAANG employees for Automation and SDET.

For more details whatsapp : https://lnkd.in/dnBWDM33

*************************************************

SeleniumWebdriver Automation Testing Interview Questions:

https://automationreinvented.blogspot.com/search/label/SeleniumWebdriver

API Testing Interview Question Set:

https://automationreinvented.blogspot.com/2022/03/top-80-api-testing-interview-questions.html

DevOps Interview Q&A:

https://automationreinvented.blogspot.com/2021/11/top-11-devops-interview-questions-and.html 

Kubernetes Interview Question Set

https://automationreinvented.blogspot.com/search/label/Kubernetes

Docker Interview Question Set

https://automationreinvented.blogspot.com/Docker

Linux Interview question Set

https://automationreinvented.blogspot.com/search/label/Linux

Automation Testing/SDET Framework Design

https://automationreinvented.blogspot.com/search/label/FrameworkDesign

Java Related Interview Question Set

https://automationreinvented.blogspot.com/search/label/Java

GIT Interview Question Set:

https://automationreinvented.blogspot.com/2021/09/top-40-git-interview-questions-and.html

Coding Interview Question Set:

https://automationreinvented.blogspot.com/search/label/Coding%20Questions

Mobile Testing Interview Question Set:

https://automationreinvented.blogspot.com/search/label/Mobile%20Testing

Python Interview Question Set for QAE - SDET - SDE:

https://automationreinvented.blogspot.com/search/label/Python


All Time Popular Posts

Most Featured Post

Introduction to Web Testing with Playwright

  Introduction to Web Testing with Playwright 📌  Telegram Group:  https://t.me/+FTf_NPb--GQ2ODhl 📌  YouTube channel:  https://lnkd.in/gGUG...