Skip to content

Commit

Permalink
Add more metrics / observability (#2893)
Browse files Browse the repository at this point in the history
* adds metrics

* adds metrics

* adds metrics

---------

Co-authored-by: sameer saini <[email protected]>
  • Loading branch information
rafaelbey and sameersaini authored Jun 6, 2024
1 parent 669ad1d commit 03bf49d
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.raw.Lambda;
import org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.raw.classInstance.graph.RootGraphFetchTree;
import org.finos.legend.engine.shared.core.api.grammar.GrammarAPI;
import org.finos.legend.engine.shared.core.operational.prometheus.MetricsHandler;
import org.finos.legend.engine.shared.core.operational.prometheus.Prometheus;
import org.pac4j.core.profile.CommonProfile;
import org.pac4j.core.profile.ProfileManager;
import org.pac4j.jax.rs.annotations.Pac4JProfileManager;
Expand All @@ -34,8 +36,10 @@
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import java.util.Map;

import static org.finos.legend.engine.shared.core.operational.http.InflateInterceptor.APPLICATION_ZLIB;
Expand All @@ -49,14 +53,20 @@ public class GrammarToJson extends GrammarAPI
@ApiOperation(value = "Generates Pure protocol JSON from Pure language text")
@Consumes({MediaType.TEXT_PLAIN, APPLICATION_ZLIB})
@Produces(MediaType.APPLICATION_JSON)
@Prometheus(name = "GrammarToJson model", doc = "Grammar to Json duration summary")
public Response model(String text,
@DefaultValue("") @ApiParam("The source ID to be used by the parser") @QueryParam("sourceId") String sourceId,
@DefaultValue("0") @ApiParam("The line number the parser will offset by") @QueryParam("lineOffset") int lineOffset,
@DefaultValue("true") @QueryParam("returnSourceInformation") boolean returnSourceInformation,
@ApiParam(hidden = true) @Pac4JProfileManager ProfileManager<CommonProfile> pm)
@ApiParam(hidden = true) @Pac4JProfileManager ProfileManager<CommonProfile> pm,
@Context UriInfo uriInfo)
{
long start = System.currentTimeMillis();
PureGrammarParserExtensions.logExtensionList();
return grammarToJson(text, (a) -> PureGrammarParser.newInstance().parseModel(a, sourceId, lineOffset, 0, returnSourceInformation), pm, "Grammar to Json : Model");
Response response = grammarToJson(text, (a) -> PureGrammarParser.newInstance().parseModel(a, sourceId, lineOffset, 0, returnSourceInformation), pm, "Grammar to Json : Model");
long end = System.currentTimeMillis();
MetricsHandler.observeRequest(uriInfo != null ? uriInfo.getPath() : null, start, end);
return response;
}

@POST
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.raw.classInstance.graph.RootGraphFetchTree;
import org.finos.legend.engine.shared.core.api.grammar.GrammarAPI;
import org.finos.legend.engine.shared.core.api.grammar.RenderStyle;
import org.finos.legend.engine.shared.core.operational.prometheus.MetricsHandler;
import org.finos.legend.engine.shared.core.operational.prometheus.Prometheus;
import org.pac4j.core.profile.CommonProfile;
import org.pac4j.core.profile.ProfileManager;
import org.pac4j.jax.rs.annotations.Pac4JProfileManager;
Expand All @@ -37,8 +39,10 @@
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import java.util.Map;

import static org.finos.legend.engine.shared.core.operational.http.InflateInterceptor.APPLICATION_ZLIB;
Expand All @@ -53,12 +57,18 @@ public class JsonToGrammar extends GrammarAPI
@ApiOperation(value = "Generates Pure language text from Pure protocol Pure Model Context Data")
@Consumes({MediaType.APPLICATION_JSON, APPLICATION_ZLIB})
@Produces(MediaType.TEXT_PLAIN)
@Prometheus(name = "GrammarToJson model", doc = "Grammar to Json duration summary")
public Response model(PureModelContextData pureModelContext,
@QueryParam("renderStyle") @DefaultValue("PRETTY") RenderStyle renderStyle,
@ApiParam(hidden = true) @Pac4JProfileManager ProfileManager<CommonProfile> pm)
@ApiParam(hidden = true) @Pac4JProfileManager ProfileManager<CommonProfile> pm,
@Context UriInfo uriInfo)
{
long start = System.currentTimeMillis();
PureGrammarComposerExtensionLoader.logExtensionList();
return jsonToGrammar(pureModelContext, renderStyle, (value, renderStyle1) -> PureGrammarComposer.newInstance(PureGrammarComposerContext.Builder.newInstance().withRenderStyle(renderStyle1).build()).renderPureModelContextData(value), pm, "Json to Grammar : Model");
Response response = jsonToGrammar(pureModelContext, renderStyle, (value, renderStyle1) -> PureGrammarComposer.newInstance(PureGrammarComposerContext.Builder.newInstance().withRenderStyle(renderStyle1).build()).renderPureModelContextData(value), pm, "Json to Grammar : Model");
long end = System.currentTimeMillis();
MetricsHandler.observeRequest(uriInfo != null ? uriInfo.getPath() : null, start, end);
return response;
}

@POST
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,4 +389,5 @@ public enum LoggingEventType implements ILoggingEventType
EXPLAIN_INTERACTIVE_EXECUTION_ERROR,

TEST_DATA_GENERATION_ERROR,
RUN_TEST_ERROR,
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class MetricsHandler

private static final Histogram SUCCESSFUL_REQUEST_LATENCY = Histogram.build().name("legend_engine_successful_request_latency")
.help("Measure legend engine's http request latency")
.buckets(.1, .2, .5, 1, 2, 5, 10, 30, 100, 300)
.buckets(.1, .2, .5, 1, 2, 5, 10, 30, 100, 300, 600, 1200)
.labelNames("uri")
.register(getMetricsRegistry());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,19 @@
import org.finos.legend.engine.shared.core.operational.http.InflateInterceptor;
import org.finos.legend.engine.shared.core.operational.logs.LogInfo;
import org.finos.legend.engine.shared.core.operational.logs.LoggingEventType;
import org.finos.legend.engine.shared.core.operational.prometheus.MetricsHandler;
import org.finos.legend.engine.shared.core.operational.prometheus.Prometheus;
import org.finos.legend.engine.testable.TestableRunner;
import org.finos.legend.engine.testable.model.RunTestsInput;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

import org.finos.legend.engine.testable.model.RunTestsResult;
import org.pac4j.core.profile.CommonProfile;
Expand Down Expand Up @@ -73,20 +77,25 @@ public TestableApi(ModelManager modelManager)
@Path("runTests")
@ApiOperation(value = "Run tests on testables")
@Consumes({MediaType.APPLICATION_JSON, InflateInterceptor.APPLICATION_ZLIB})
public Response doTests(RunTestsInput input, @ApiParam(hidden = true) @Pac4JProfileManager ProfileManager<CommonProfile> profileManager)
@Prometheus(name = "run tests", doc = "Test run duration summary")
public Response doTests(RunTestsInput input, @ApiParam(hidden = true) @Pac4JProfileManager ProfileManager<CommonProfile> profileManager, @Context UriInfo uriInfo)
{
MutableList<CommonProfile> profiles = ProfileManagerHelper.extractProfiles(profileManager);
Identity identity = Identity.makeIdentity(profiles);
long start = System.currentTimeMillis();
try
{
LOGGER.info(new LogInfo(identity.getName(), LoggingEventType.TESTABLE_DO_TESTS_START, "").toString());
Pair<PureModelContextData, PureModel> modelAndData = this.modelManager.loadModelAndData(input.model, input.model instanceof PureModelContextPointer ? ((PureModelContextPointer) input.model).serializer.version : null, identity, null);
RunTestsResult runTestsResult = testableRunner.doTests(input.testables, modelAndData.getTwo(), modelAndData.getOne());
LOGGER.info(new LogInfo(identity.getName(), LoggingEventType.TESTABLE_DO_TESTS_STOP, "").toString());
long end = System.currentTimeMillis();
MetricsHandler.observeRequest(uriInfo != null ? uriInfo.getPath() : null, start, end);
return ManageConstantResult.manageResult(identity.getName(), runTestsResult, objectMapper);
}
catch (Exception e)
{
MetricsHandler.observeError(LoggingEventType.RUN_TEST_ERROR, e, null);
return ExceptionTool.exceptionManager(e, LoggingEventType.TESTABLE_DO_TESTS_ERROR, identity.getName());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import org.finos.legend.engine.shared.core.kerberos.ProfileManagerHelper;
import org.finos.legend.engine.shared.core.operational.errorManagement.ExceptionTool;
import org.finos.legend.engine.shared.core.operational.logs.LoggingEventType;
import org.finos.legend.engine.shared.core.operational.prometheus.MetricsHandler;
import org.finos.legend.engine.shared.core.operational.prometheus.Prometheus;
import org.finos.legend.engine.testData.generation.model.TestDataGenerationInput;
import org.finos.legend.engine.testData.generation.model.TestDataGenerationResult;
import org.finos.legend.engine.testData.generation.service.TestDataGenerationService;
Expand All @@ -41,8 +43,10 @@
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

import static org.finos.legend.engine.shared.core.operational.http.InflateInterceptor.APPLICATION_ZLIB;

Expand All @@ -63,18 +67,23 @@ public TestDataGeneration(ModelManager modelManager)
@Path("DONOTUSE_generateTestData")
@ApiOperation(value = "Studio WIP: will not be backward compatible until we remove the DONOTUSE flag")
@Consumes({MediaType.APPLICATION_JSON, APPLICATION_ZLIB})
public Response generateTestData(TestDataGenerationInput input, @ApiParam(hidden = true) @Pac4JProfileManager ProfileManager<CommonProfile> profileManager)
@Prometheus(name = "generate test data")
public Response generateTestData(TestDataGenerationInput input, @ApiParam(hidden = true) @Pac4JProfileManager ProfileManager<CommonProfile> profileManager, @Context UriInfo uriInfo)
{
MutableList<CommonProfile> profiles = ProfileManagerHelper.extractProfiles(profileManager);
Identity identity = Identity.makeIdentity(profiles);
PureModel pureModel = modelManager.loadModel(input.model, input.clientVersion == null ? PureClientVersions.production : input.clientVersion, identity, null);
long start = System.currentTimeMillis();
try
{
TestDataGenerationResult result = new TestDataGenerationResult(TestDataGenerationService.generateEmbeddedData(input.query, pureModel.getMapping(input.mapping), pureModel));
long end = System.currentTimeMillis();
MetricsHandler.observeRequest(uriInfo != null ? uriInfo.getPath() : null, start, end);
return ManageConstantResult.manageResult(identity.getName(), result, objectMapper);
}
catch (Exception e)
{
MetricsHandler.observeError(LoggingEventType.TEST_DATA_GENERATION_ERROR, e, null);
return ExceptionTool.exceptionManager(e, LoggingEventType.TEST_DATA_GENERATION_ERROR, Response.Status.BAD_REQUEST, identity.getName());
}
}
Expand Down

0 comments on commit 03bf49d

Please sign in to comment.