- 
                Notifications
    You must be signed in to change notification settings 
- Fork 247
feat: Add JVM memory metrics support for accurate Java application resource recommendations #440
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Open
      
      
            myutex-developers
  wants to merge
  2
  commits into
  robusta-dev:main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
myutex-developers:feature/support-jvm-metric
  
      
      
   
  
    
  
  
  
 
  
      
    base: main
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            2 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| import pytest | ||
| from datetime import datetime, timedelta | ||
| import numpy as np | ||
|  | ||
| from robusta_krr.core.integrations.prometheus.metrics.memory import ( | ||
| JVMMemoryLoader, | ||
| MaxJVMMemoryLoader, | ||
| JVMMemoryAmountLoader, | ||
| JVMDetector, | ||
| ) | ||
| from robusta_krr.core.models.objects import K8sObjectData, PodData | ||
|  | ||
|  | ||
| @pytest.fixture | ||
| def mock_pod_data(): | ||
| return K8sObjectData( | ||
| name="test-app", | ||
| namespace="default", | ||
| kind="Deployment", | ||
| container="app", | ||
| pods=[ | ||
| PodData(name="test-app-pod-1", namespace="default"), | ||
| PodData(name="test-app-pod-2", namespace="default"), | ||
| ], | ||
| ) | ||
|  | ||
|  | ||
| @pytest.fixture | ||
| def mock_prometheus_response(): | ||
| return { | ||
| "status": "success", | ||
| "data": { | ||
| "resultType": "matrix", | ||
| "result": [ | ||
| { | ||
| "metric": { | ||
| "container": "app", | ||
| "pod": "test-app-pod-1", | ||
| "job": "kubernetes-pods", | ||
| }, | ||
| "values": [ | ||
| [1625097600, "1000000"], # 1MB | ||
| [1625097900, "2000000"], # 2MB | ||
| [1625098200, "1500000"], # 1.5MB | ||
| ], | ||
| }, | ||
| { | ||
| "metric": { | ||
| "container": "app", | ||
| "pod": "test-app-pod-2", | ||
| "job": "kubernetes-pods", | ||
| }, | ||
| "values": [ | ||
| [1625097600, "1200000"], # 1.2MB | ||
| [1625097900, "1800000"], # 1.8MB | ||
| [1625098200, "1600000"], # 1.6MB | ||
| ], | ||
| }, | ||
| ], | ||
| }, | ||
| } | ||
|  | ||
|  | ||
| def test_jvm_memory_loader_query(mock_pod_data): | ||
| loader = JVMMemoryLoader() | ||
| query = loader.get_query(mock_pod_data, "1h", "1m") | ||
|  | ||
| assert "jvm_memory_bytes_used" in query | ||
| assert "area=\"heap\"" in query | ||
| assert "test-app-pod-1|test-app-pod-2" in query | ||
| assert "namespace=\"default\"" in query | ||
| assert "container=\"app\"" in query | ||
|  | ||
|  | ||
| def test_max_jvm_memory_loader_query(mock_pod_data): | ||
| loader = MaxJVMMemoryLoader() | ||
| query = loader.get_query(mock_pod_data, "1h", "1m") | ||
|  | ||
| assert "jvm_memory_bytes_used" in query | ||
| assert "area=\"heap\"" in query | ||
| assert "max_over_time" in query | ||
| assert "test-app-pod-1|test-app-pod-2" in query | ||
|  | ||
|  | ||
| def test_jvm_memory_amount_loader_query(mock_pod_data): | ||
| loader = JVMMemoryAmountLoader() | ||
| query = loader.get_query(mock_pod_data, "1h", "1m") | ||
|  | ||
| assert "jvm_memory_bytes_used" in query | ||
| assert "area=\"heap\"" in query | ||
| assert "count_over_time" in query | ||
| assert "test-app-pod-1|test-app-pod-2" in query | ||
|  | ||
|  | ||
| def test_jvm_detector_query(mock_pod_data): | ||
| loader = JVMDetector() | ||
| query = loader.get_query(mock_pod_data, "1h", "1m") | ||
|  | ||
| assert "jvm_memory_bytes_used" in query | ||
| assert "test-app-pod-1|test-app-pod-2" in query | ||
|  | ||
|  | ||
| def test_jvm_memory_loader_parse_response(mock_prometheus_response): | ||
| loader = JVMMemoryLoader() | ||
| result = loader.parse_response(mock_prometheus_response) | ||
|  | ||
| assert len(result) == 2 | ||
| assert "test-app-pod-1" in result | ||
| assert "test-app-pod-2" in result | ||
|  | ||
| # Check if values are properly converted to numpy arrays | ||
| pod1_values = result["test-app-pod-1"] | ||
| assert isinstance(pod1_values, np.ndarray) | ||
| assert pod1_values.shape == (3, 2) # 3 timestamps, 2 values each | ||
| assert np.max(pod1_values[:, 1]) == 2000000 # Max value should be 2MB | ||
|  | ||
|  | ||
| def test_max_jvm_memory_loader_parse_response(mock_prometheus_response): | ||
| loader = MaxJVMMemoryLoader() | ||
| result = loader.parse_response(mock_prometheus_response) | ||
|  | ||
| assert len(result) == 2 | ||
| assert "test-app-pod-1" in result | ||
| assert "test-app-pod-2" in result | ||
|  | ||
| # Check if values are properly converted to numpy arrays | ||
| pod1_values = result["test-app-pod-1"] | ||
| assert isinstance(pod1_values, np.ndarray) | ||
| assert pod1_values.shape == (3, 2) | ||
| assert np.max(pod1_values[:, 1]) == 2000000 # Max value should be 2MB | ||
|  | ||
|  | ||
| def test_jvm_memory_amount_loader_parse_response(mock_prometheus_response): | ||
| loader = JVMMemoryAmountLoader() | ||
| result = loader.parse_response(mock_prometheus_response) | ||
|  | ||
| assert len(result) == 2 | ||
| assert "test-app-pod-1" in result | ||
| assert "test-app-pod-2" in result | ||
|  | ||
| # Check if values are properly converted to numpy arrays | ||
| pod1_values = result["test-app-pod-1"] | ||
| assert isinstance(pod1_values, np.ndarray) | ||
| assert pod1_values.shape == (3, 2) | ||
| assert np.sum(pod1_values[:, 1]) == 3 # Should count 3 data points | ||
|  | ||
|  | ||
| def test_jvm_detector_parse_response(mock_prometheus_response): | ||
| loader = JVMDetector() | ||
| result = loader.parse_response(mock_prometheus_response) | ||
|  | ||
| assert len(result) == 2 | ||
| assert "test-app-pod-1" in result | ||
| assert "test-app-pod-2" in result | ||
|  | ||
| # Check if values are properly converted to numpy arrays | ||
| pod1_values = result["test-app-pod-1"] | ||
| assert isinstance(pod1_values, np.ndarray) | ||
| assert pod1_values.shape == (3, 2) | ||
| assert np.max(pod1_values[:, 1]) == 2000000 # Max value should be 2MB | 
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused import triggers Ruff F401 – drop or actually use
JVMMemoryLoader.JVMMemoryLoaderis imported but never referenced. Ruff flags this asF401, which will fail lint-only CI steps.Either add the loader to the
metricslist or remove the import:- JVMMemoryLoader, MaxJVMMemoryLoader,If a plain (non-max) JVM timeseries is not needed here, simply deleting the import keeps the strategy lean and CI green.
📝 Committable suggestion
🧰 Tools
🪛 Ruff (0.11.9)
24-24:
robusta_krr.core.integrations.prometheus.metrics.JVMMemoryLoaderimported but unusedRemove unused import:
robusta_krr.core.integrations.prometheus.metrics.JVMMemoryLoader(F401)
🤖 Prompt for AI Agents