- 
                Notifications
    
You must be signed in to change notification settings  - Fork 375
 
Add endpoint method and path to metrics name. #2850
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
          
     Merged
      
      
            wslulciuc
  merged 4 commits into
  MarquezProject:main
from
JDarDagran:observability/add_endpoint_name_to_metrics_name
  
      
      
   
  Jul 25, 2024 
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            4 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      89ef52f
              
                Add endpoint method and path to metrics name.
              
              
                JDarDagran e83e967
              
                Introduce labels to metrics and add them to v2 metrics endpoint.
              
              
                JDarDagran d141c1f
              
                Rename metric name and labels.
              
              
                JDarDagran ce3835d
              
                Merge branch 'main' into observability/add_endpoint_name_to_metrics_name
              
              
                wslulciuc 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
    
  
  
    
              
        
          
          
            35 changes: 35 additions & 0 deletions
          
          35 
        
  api/src/main/java/marquez/logging/DelegatingSqlLogger.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,35 @@ | ||
| /* | ||
| * Copyright 2018-2023 contributors to the Marquez project | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| 
     | 
||
| package marquez.logging; | ||
| 
     | 
||
| import java.sql.SQLException; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import org.jdbi.v3.core.statement.SqlLogger; | ||
| import org.jdbi.v3.core.statement.StatementContext; | ||
| 
     | 
||
| /** A {@link SqlLogger} implementation that delegates to multiple {@link SqlLogger}s. */ | ||
| public class DelegatingSqlLogger implements SqlLogger { | ||
| private final List<SqlLogger> sqlLoggers; | ||
| 
     | 
||
| public DelegatingSqlLogger(SqlLogger... sqlLoggers) { | ||
| this.sqlLoggers = Arrays.asList(sqlLoggers); | ||
| } | ||
| 
     | 
||
| @Override | ||
| public void logAfterExecution(StatementContext statementContext) { | ||
| for (SqlLogger sqlLogger : sqlLoggers) { | ||
| sqlLogger.logAfterExecution(statementContext); | ||
| } | ||
| } | ||
| 
     | 
||
| @Override | ||
| public void logException(StatementContext statementContext, SQLException ex) { | ||
| for (SqlLogger sqlLogger : sqlLoggers) { | ||
| sqlLogger.logException(statementContext, ex); | ||
| } | ||
| } | ||
| } | 
  
    
      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,46 @@ | ||
| /* | ||
| * Copyright 2018-2023 contributors to the Marquez project | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| 
     | 
||
| package marquez.logging; | ||
| 
     | 
||
| import java.sql.SQLException; | ||
| import java.time.temporal.ChronoUnit; | ||
| import marquez.service.DatabaseMetrics; | ||
| import org.jdbi.v3.core.extension.ExtensionMethod; | ||
| import org.jdbi.v3.core.statement.SqlLogger; | ||
| import org.jdbi.v3.core.statement.StatementContext; | ||
| import org.slf4j.MDC; | ||
| 
     | 
||
| /** | ||
| * A {@link SqlLogger} implementation for JDBI which uses the SQL objects' class names and method | ||
| * names for nanosecond-precision timers. | ||
| */ | ||
| public class LabelledSqlLogger implements SqlLogger { | ||
| 
     | 
||
| @Override | ||
| public void logAfterExecution(StatementContext context) { | ||
| log(context); | ||
| } | ||
| 
     | 
||
| @Override | ||
| public void logException(StatementContext context, SQLException ex) { | ||
| log(context); | ||
| } | ||
| 
     | 
||
| private void log(StatementContext context) { | ||
| ExtensionMethod extensionMethod = context.getExtensionMethod(); | ||
| if (extensionMethod != null) { | ||
| final long elapsed = context.getElapsedTime(ChronoUnit.NANOS); | ||
| if (MDC.get("method") != null && MDC.get("pathWithParams") != null) { | ||
| DatabaseMetrics.recordDbDuration( | ||
| extensionMethod.getType().getName(), | ||
| extensionMethod.getMethod().getName(), | ||
| MDC.get("method"), | ||
| MDC.get("pathWithParams"), | ||
| elapsed / 1e9); | ||
| } | ||
| } | ||
| } | ||
| } | 
  
    
      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,26 @@ | ||
| /* | ||
| * Copyright 2018-2023 contributors to the Marquez project | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| 
     | 
||
| package marquez.service; | ||
| 
     | 
||
| import io.prometheus.client.CollectorRegistry; | ||
| import io.prometheus.client.Histogram; | ||
| 
     | 
||
| public class DatabaseMetrics { | ||
| public static final CollectorRegistry registry = new io.prometheus.client.CollectorRegistry(); | ||
| 
     | 
||
| public static final Histogram dbDurationSeconds = | ||
| Histogram.build() | ||
| .namespace("marquez") | ||
| .labelNames("sql_class", "sql_method", "http_method", "http_path") | ||
| .name("db_duration_seconds_by_http_call") | ||
| .help("The time to make the DB call for a given HTTP endpoint.") | ||
| .register(registry); | ||
| 
     | 
||
| public static void recordDbDuration( | ||
| String sqlClass, String sqlMethod, String httpMethod, String httpPath, double duration) { | ||
| dbDurationSeconds.labels(sqlClass, sqlMethod, httpMethod, httpPath).observe(duration); | ||
| } | ||
| } | 
  
    
      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,54 @@ | ||
| /* | ||
| * Copyright 2018-2023 contributors to the Marquez project | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| 
     | 
||
| package marquez; | ||
| 
     | 
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| 
     | 
||
| import java.io.IOException; | ||
| import java.net.http.HttpResponse; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.Test; | ||
| 
     | 
||
| @org.junit.jupiter.api.Tag("IntegrationTests") | ||
| @Slf4j | ||
| public class MetricsIntegrationTest extends BaseIntegrationTest { | ||
| @Test | ||
| public void testCheckMetricV1Name() throws IOException { | ||
| client.listNamespaces(); | ||
| CompletableFuture<String> response = | ||
| this.getMetrics() | ||
| .thenApply(HttpResponse::body) | ||
| .whenComplete( | ||
| (val, error) -> { | ||
| if (error != null) { | ||
| Assertions.fail("Could not complete request"); | ||
| } | ||
| }); | ||
| assertThat(response.join()).contains("marquez_db_NamespaceDao_findAll_count"); | ||
| } | ||
| 
     | 
||
| @Test | ||
| public void testCheckMetricV2Name() throws IOException { | ||
| client.listNamespaces(); | ||
| CompletableFuture<String> response = | ||
| this.getMetricsV2() | ||
| .thenApply(HttpResponse::body) | ||
| .whenComplete( | ||
| (val, error) -> { | ||
| if (error != null) { | ||
| Assertions.fail("Could not complete request"); | ||
| } | ||
| }); | ||
| assertThat(response.join()) | ||
| .contains( | ||
| "marquez_db_duration_seconds_by_http_call_sum{sql_class=\"marquez.db.NamespaceDao\"," | ||
| + "sql_method=\"findAll\"," | ||
| + "http_method=\"GET\"," | ||
| + "http_path=\"/api/v1/namespaces\",}"); | ||
| } | ||
| } | 
        
          
          
            53 changes: 53 additions & 0 deletions
          
          53 
        
  api/src/test/java/marquez/logging/DelegatingSqlLoggerTest.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,53 @@ | ||
| /* | ||
| * Copyright 2018-2023 contributors to the Marquez project | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| 
     | 
||
| package marquez.logging; | ||
| 
     | 
||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.times; | ||
| import static org.mockito.Mockito.verify; | ||
| 
     | 
||
| import java.sql.SQLException; | ||
| import org.jdbi.v3.core.statement.SqlLogger; | ||
| import org.jdbi.v3.core.statement.StatementContext; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| 
     | 
||
| public class DelegatingSqlLoggerTest { | ||
| private SqlLogger logger1; | ||
| private SqlLogger logger2; | ||
| private DelegatingSqlLogger delegatingSqlLogger; | ||
| private StatementContext statementContext; | ||
| private SQLException sqlException; | ||
| 
     | 
||
| @BeforeEach | ||
| public void setUp() { | ||
| logger1 = mock(SqlLogger.class); | ||
| logger2 = mock(SqlLogger.class); | ||
| delegatingSqlLogger = new DelegatingSqlLogger(logger1, logger2); | ||
| statementContext = mock(StatementContext.class); | ||
| sqlException = new SQLException("Test SQL Exception"); | ||
| } | ||
| 
     | 
||
| @Test | ||
| public void testLogAfterExecution() { | ||
| // Act | ||
| delegatingSqlLogger.logAfterExecution(statementContext); | ||
| 
     | 
||
| // Assert | ||
| verify(logger1, times(1)).logAfterExecution(statementContext); | ||
| verify(logger2, times(1)).logAfterExecution(statementContext); | ||
| } | ||
| 
     | 
||
| @Test | ||
| public void testLogException() { | ||
| // Act | ||
| delegatingSqlLogger.logException(statementContext, sqlException); | ||
| 
     | 
||
| // Assert | ||
| verify(logger1, times(1)).logException(statementContext, sqlException); | ||
| verify(logger2, times(1)).logException(statementContext, sqlException); | ||
| } | ||
| } | 
      
      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.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.