- 
                Notifications
    
You must be signed in to change notification settings  - Fork 1k
 
add file descriptor metrics #11876
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
          
     Closed
      
      
    
  
     Closed
                    add file descriptor metrics #11876
Changes from 2 commits
      Commits
    
    
            Show all changes
          
          
            10 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      a68a8d7
              
                add file descriptor metrics
              
              
                xiangtianyu ee39c6b
              
                fix code style
              
              
                xiangtianyu 5dc42b3
              
                fix some desciption and name and some test
              
              
                xiangtianyu df6fc6c
              
                fix tests
              
              
                xiangtianyu 4f70a6e
              
                Merge branch 'main' into file-metrics
              
              
                xiangtianyu 752143d
              
                add comment relate to issue
              
              
                xiangtianyu 71e5ccd
              
                fix
              
              
                xiangtianyu 6ea98ec
              
                fix
              
              
                xiangtianyu 298eb0c
              
                code style
              
              
                xiangtianyu cc4fb9b
              
                modify metrics name
              
              
                xiangtianyu 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
    
  
  
    
              
        
          
  
    
      
          
            71 changes: 71 additions & 0 deletions
          
          71 
        
  ...entelemetry/instrumentation/runtimemetrics/java8/internal/ExperimentalFileDescriptor.java
  
  
      
      
   
        
      
      
    
  
    
      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,71 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| 
     | 
||
| package io.opentelemetry.instrumentation.runtimemetrics.java8.internal; | ||
| 
     | 
||
| import io.opentelemetry.api.OpenTelemetry; | ||
| import io.opentelemetry.api.metrics.Meter; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.function.Supplier; | ||
| 
     | 
||
| /** | ||
| * Registers measurements that generate experimental metrics about file descriptor. | ||
| * | ||
| * <p>This class is internal and is hence not for public use. Its APIs are unstable and can change | ||
| * at any time. | ||
| */ | ||
| public class ExperimentalFileDescriptor { | ||
| /** Register observers for java runtime experimental file descriptor metrics. */ | ||
| public static List<AutoCloseable> registerObservers(OpenTelemetry openTelemetry) { | ||
| return registerObservers( | ||
| openTelemetry, | ||
| FileDescriptorMethods.openFileDescriptorCount(), | ||
| FileDescriptorMethods.maxFileDescriptorCount()); | ||
| } | ||
| 
     | 
||
| // Visible for testing | ||
| static List<AutoCloseable> registerObservers( | ||
| OpenTelemetry openTelemetry, | ||
| Supplier<Long> openFileDescriptorCount, | ||
| Supplier<Long> maxFileDescriptorCount) { | ||
| Meter meter = JmxRuntimeMetricsUtil.getMeter(openTelemetry); | ||
| List<AutoCloseable> observables = new ArrayList<>(); | ||
| 
     | 
||
| if (openFileDescriptorCount != null) { | ||
| observables.add( | ||
| meter | ||
| .gaugeBuilder("os.file.descriptor.open") | ||
| .setDescription("number of open file descriptors") | ||
                
      
                  laurit marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| .setUnit("{file}") | ||
                
      
                  trask marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| .buildWithCallback( | ||
| observableMeasurement -> { | ||
| Long openCount = openFileDescriptorCount.get(); | ||
| if (openCount != null && openCount >= 0) { | ||
                
      
                  xiangtianyu marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| observableMeasurement.record(openCount); | ||
| } | ||
| })); | ||
| } | ||
| 
     | 
||
| if (maxFileDescriptorCount != null) { | ||
| observables.add( | ||
| meter | ||
| .gaugeBuilder("os.file.descriptor.max") | ||
                
      
                  laurit marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| .setDescription("maximum number of file descriptors") | ||
| .setUnit("{file}") | ||
| .buildWithCallback( | ||
| observableMeasurement -> { | ||
| Long maxCount = maxFileDescriptorCount.get(); | ||
| if (maxCount != null && maxCount >= 0) { | ||
| observableMeasurement.record(maxCount); | ||
| } | ||
| })); | ||
| } | ||
| 
     | 
||
| return observables; | ||
| } | ||
| 
     | 
||
| private ExperimentalFileDescriptor() {} | ||
| } | ||
        
          
          
            82 changes: 82 additions & 0 deletions
          
          82 
        
  ...io/opentelemetry/instrumentation/runtimemetrics/java8/internal/FileDescriptorMethods.java
  
  
      
      
   
        
      
      
    
  
    
      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,82 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| 
     | 
||
| package io.opentelemetry.instrumentation.runtimemetrics.java8.internal; | ||
| 
     | 
||
| import java.lang.management.ManagementFactory; | ||
| import java.lang.management.OperatingSystemMXBean; | ||
| import java.lang.reflect.InvocationTargetException; | ||
| import java.lang.reflect.Method; | ||
| import java.util.function.Supplier; | ||
| import javax.annotation.Nullable; | ||
| 
     | 
||
| /** | ||
| * This class is internal and is hence not for public use. Its APIs are unstable and can change at | ||
| * any time. | ||
| */ | ||
| public class FileDescriptorMethods { | ||
                
      
                  laurit marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| 
     | 
||
| private static final String OS_BEAN_J9 = "com.ibm.lang.management.UnixOperatingSystemMXBean"; | ||
| private static final String OS_BEAN_HOTSPOT = "com.sun.management.UnixOperatingSystemMXBean"; | ||
| private static final String METHOD_OPEN_FILE_DESCRIPTOR_COUNT = "getOpenFileDescriptorCount"; | ||
| private static final String METHOD_MAX_FILE_DESCRIPTOR_COUNT = "getMaxFileDescriptorCount"; | ||
| 
     | 
||
| @Nullable private static final Supplier<Long> openFileDescriptorCount; | ||
| @Nullable private static final Supplier<Long> maxFileDescriptorCount; | ||
| 
     | 
||
| static { | ||
| OperatingSystemMXBean osBean = ManagementFactory.getOperatingSystemMXBean(); | ||
| 
     | 
||
| Supplier<Long> openFileDescriptorCountSupplier = | ||
| methodInvoker(osBean, OS_BEAN_HOTSPOT, METHOD_OPEN_FILE_DESCRIPTOR_COUNT); | ||
| if (openFileDescriptorCountSupplier == null) { | ||
| // More users will be on hotspot than j9, so check for j9 second | ||
| openFileDescriptorCountSupplier = | ||
| methodInvoker(osBean, OS_BEAN_J9, METHOD_OPEN_FILE_DESCRIPTOR_COUNT); | ||
| } | ||
| 
     | 
||
| openFileDescriptorCount = openFileDescriptorCountSupplier; | ||
| 
     | 
||
| Supplier<Long> maxFileDescriptorCountSupplier = | ||
| methodInvoker(osBean, OS_BEAN_HOTSPOT, METHOD_MAX_FILE_DESCRIPTOR_COUNT); | ||
| if (maxFileDescriptorCountSupplier == null) { | ||
| // More users will be on hotspot than j9, so check for j9 second | ||
| maxFileDescriptorCountSupplier = | ||
| methodInvoker(osBean, OS_BEAN_J9, METHOD_MAX_FILE_DESCRIPTOR_COUNT); | ||
| } | ||
| 
     | 
||
| maxFileDescriptorCount = maxFileDescriptorCountSupplier; | ||
| } | ||
| 
     | 
||
| @Nullable | ||
| @SuppressWarnings("ReturnValueIgnored") | ||
| private static Supplier<Long> methodInvoker( | ||
| OperatingSystemMXBean osBean, String osBeanClassName, String methodName) { | ||
| try { | ||
| Class<?> osBeanClass = Class.forName(osBeanClassName); | ||
| osBeanClass.cast(osBean); | ||
| Method method = osBeanClass.getDeclaredMethod(methodName); | ||
| return () -> { | ||
| try { | ||
| return (Long) method.invoke(osBean); | ||
| } catch (IllegalAccessException | InvocationTargetException e) { | ||
| return null; | ||
| } | ||
| }; | ||
| } catch (ClassNotFoundException | ClassCastException | NoSuchMethodException e) { | ||
| return null; | ||
| } | ||
| } | ||
| 
     | 
||
| public static Supplier<Long> openFileDescriptorCount() { | ||
| return openFileDescriptorCount; | ||
| } | ||
| 
     | 
||
| public static Supplier<Long> maxFileDescriptorCount() { | ||
| return maxFileDescriptorCount; | ||
| } | ||
| 
     | 
||
| private FileDescriptorMethods() {} | ||
| } | ||
        
          
  
    
      
          
            57 changes: 57 additions & 0 deletions
          
          57 
        
  ...lemetry/instrumentation/runtimemetrics/java8/internal/ExperimentalFileDescriptorTest.java
  
  
      
      
   
        
      
      
    
  
    
      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,57 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| 
     | 
||
| package io.opentelemetry.instrumentation.runtimemetrics.java8.internal; | ||
| 
     | 
||
| import static io.opentelemetry.instrumentation.runtimemetrics.java8.ScopeUtil.EXPECTED_SCOPE; | ||
| import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; | ||
| 
     | 
||
| import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; | ||
| import io.opentelemetry.instrumentation.testing.junit.LibraryInstrumentationExtension; | ||
| import java.util.function.Supplier; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
| import org.junit.jupiter.api.extension.RegisterExtension; | ||
| import org.mockito.junit.jupiter.MockitoExtension; | ||
| 
     | 
||
| @ExtendWith(MockitoExtension.class) | ||
                
      
                  xiangtianyu marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| public class ExperimentalFileDescriptorTest { | ||
                
      
                  xiangtianyu marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| @RegisterExtension | ||
| static final InstrumentationExtension testing = LibraryInstrumentationExtension.create(); | ||
| 
     | 
||
| @Test | ||
| void registerObservers() { | ||
| Supplier<Long> openFileDescriptor = () -> 10L; | ||
| Supplier<Long> maxFileDescriptor = () -> 10000L; | ||
| 
     | 
||
| ExperimentalFileDescriptor.registerObservers( | ||
| testing.getOpenTelemetry(), openFileDescriptor, maxFileDescriptor); | ||
| 
     | 
||
| testing.waitAndAssertMetrics( | ||
| "io.opentelemetry.runtime-telemetry-java8", | ||
| "os.file.descriptor.open", | ||
| metrics -> | ||
| metrics.anySatisfy( | ||
| metricData -> | ||
| assertThat(metricData) | ||
| .hasInstrumentationScope(EXPECTED_SCOPE) | ||
| .hasDescription("number of open file descriptors") | ||
| .hasUnit("{file}") | ||
| .hasDoubleGaugeSatisfying( | ||
| gauge -> gauge.hasPointsSatisfying(point -> point.hasValue(10L))))); | ||
| testing.waitAndAssertMetrics( | ||
| "io.opentelemetry.runtime-telemetry-java8", | ||
| "os.file.descriptor.max", | ||
| metrics -> | ||
| metrics.anySatisfy( | ||
| metricData -> | ||
| assertThat(metricData) | ||
| .hasInstrumentationScope(EXPECTED_SCOPE) | ||
| .hasDescription("maximum number of file descriptors") | ||
| .hasUnit("{file}") | ||
| .hasDoubleGaugeSatisfying( | ||
| gauge -> gauge.hasPointsSatisfying(point -> point.hasValue(10000L))))); | ||
| } | ||
| } | ||
  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.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.