This repository has been archived by the owner on Aug 25, 2024. It is now read-only.
forked from LangStream/langstream
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ca593ee
commit d6108b8
Showing
8 changed files
with
171 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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 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 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
langstream-api-gateway/src/main/java/ai/langstream/apigateway/metrics/ApiGatewayMetrics.java
This file contains 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 @@ | ||
package ai.langstream.apigateway.metrics; | ||
|
||
import ai.langstream.api.model.Gateway; | ||
import io.micrometer.core.instrument.Counter; | ||
import io.micrometer.core.instrument.Metrics; | ||
|
||
public class ApiGatewayMetrics implements AutoCloseable { | ||
|
||
private final static String TAG_TENANT = "tenant"; | ||
private final static String TAG_APPLICATION_ID = "application"; | ||
private final static String TAG_GATEWAY_ID = "gateway"; | ||
|
||
public void addHttpGatewayRequest( | ||
String tenant, | ||
String applicationId, | ||
String gatewayId, | ||
String httpMethod, | ||
int responseStatusCode) { | ||
Counter.builder("langstream.gateways.http.requests") | ||
.description("HTTP requests to gateways") | ||
.tag(TAG_TENANT, tenant) | ||
.tag(TAG_APPLICATION_ID, applicationId) | ||
.tag(TAG_GATEWAY_ID, gatewayId) | ||
.tag("http_method", httpMethod) | ||
.tag("response_status_code", responseStatusCode + "") | ||
.register(Metrics.globalRegistry) | ||
.increment(); | ||
} | ||
|
||
@Override | ||
public void close() throws Exception { | ||
Metrics.globalRegistry.close(); | ||
Metrics.globalRegistry.clear(); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...api-gateway/src/main/java/ai/langstream/apigateway/metrics/ApiGatewayMetricsProvider.java
This file contains 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,25 @@ | ||
package ai.langstream.apigateway.metrics; | ||
|
||
import ai.langstream.api.model.Gateway; | ||
import ai.langstream.api.runner.topics.TopicProducer; | ||
import ai.langstream.apigateway.config.TopicProperties; | ||
import ai.langstream.apigateway.gateways.LRUTopicProducerCache; | ||
import ai.langstream.apigateway.gateways.TopicProducerCache; | ||
import io.micrometer.core.instrument.Counter; | ||
import io.micrometer.core.instrument.Metrics; | ||
import io.micrometer.core.instrument.binder.cache.GuavaCacheMetrics; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import java.util.function.Supplier; | ||
|
||
@Configuration | ||
public class ApiGatewayMetricsProvider { | ||
|
||
|
||
@Bean(destroyMethod = "close") | ||
public ApiGatewayMetrics apiGatewayMetrics() { | ||
return new ApiGatewayMetrics(); | ||
} | ||
|
||
} |
This file contains 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
60 changes: 60 additions & 0 deletions
60
langstream-api-gateway/src/test/java/ai/langstream/apigateway/ApiGatewayTestUtil.java
This file contains 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,60 @@ | ||
package ai.langstream.apigateway; | ||
|
||
import lombok.SneakyThrows; | ||
|
||
import java.net.URI; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class ApiGatewayTestUtil { | ||
|
||
@SneakyThrows | ||
public static String getPrometheusMetrics(final int port) { | ||
final String url = | ||
"http://localhost:%d/management/prometheus" | ||
.formatted(port); | ||
final HttpRequest request = | ||
HttpRequest.newBuilder(URI.create(url)) | ||
.GET() | ||
.build(); | ||
final HttpResponse<String> response = | ||
HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString()); | ||
assertEquals(200, response.statusCode()); | ||
return response.body(); | ||
} | ||
|
||
public record ParsedMetric(String name, String value, Map<String, String> labels) { | ||
} | ||
|
||
|
||
public static List<ParsedMetric> findMetric(String metricName, String metrics) { | ||
return metrics.lines() | ||
.filter(m -> { | ||
if (m.startsWith("#")) { | ||
return false; | ||
} | ||
return m.startsWith(metricName + "{"); | ||
}) | ||
.map(line -> { | ||
final String[] parts = line.split(" "); | ||
String name = parts[0]; | ||
final String value = parts[1]; | ||
final String labels = name.substring(name.indexOf("{") + 1, name.indexOf("}")); | ||
name = name.substring(0, name.indexOf("{")); | ||
final Map<String, String> labelMap = new HashMap<>(); | ||
for (String label : labels.split(",")) { | ||
final String[] labelParts = label.split("="); | ||
labelMap.put(labelParts[0], labelParts[1].substring(1, labelParts[1].length() - 1)); | ||
} | ||
return new ParsedMetric(name, value, labelMap); | ||
}) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
This file contains 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