awslabs / awslabs/aws-embedded-metrics-java

Can a single MetricsLogger be used in different threads which will use different dimensions? (E.g. in a request handler)

Open
#131 1 comment 1 reaction 0 assignees View on GitHub
question
Dominant language
Java
Stars
48
Forks
39
PR merge metrics
No merged PRs in 30d

Description

Hey all,

Thanks for the library! This has sped up telemetry work quite a bit on a new service I'm working on.

Can I use a single MetricsLogger in different threads of a service, with each thread using different dimensions?

E.g. can I use a single MetricsLogger bean in an ECS service, which is expected to handle multiple requests concurrently? I would use it to log metrics for downstream service calls and setting specific downstream APIs as the dimension for each call, like:
- **Namespace**: OtherTeamService, **Endpoint** (dimension): GetSomeEntity, **Latency** (metric): 100ms
- **Namespace**: OtherTeamService, **Endpoint** (dimension): PutSomeEntity, **Latency** (metric): 500ms
- **Namespace**: OtherTeamService, **Endpoint** (dimension): PostSomeOperation, **Latency** (metric): 1000ms

Can I use an implementation like...
```
@AllArgsConstructor
@RestController
public class MyApiHandler {
private final MetricsLogger metricsLogger; // This is a singleton bean which is passed in through the constructor
private final OtherTeamServiceClient otherTeamServiceClient;

@GET
@Path("myApi")
public MyApiHandlerResponse handleRequest(final MyApiHandlerRequest request) {
// [...]
metricsLogger.setDimensions(DimensionSet.of("Endpoint", "GetSomeEntity");
metricsLogger.putMetric("Latency", Duration.between(getSomeEntityStart, getSomeEntityEnd).toMillis(), Unit.MILLISECONDS);
metricsLogger.flush()
// [...]
metricsLogger.setDimensions(DimensionSet.of("Endpoint", "PutSomeEntity");
metricsLogger.putMetric("Latency", Duration.between(putSomeEntityStart, putSomeEntityEnd).toMillis(), Unit.MILLISECONDS);
metricsLogger.flush()
// [...]
metricsLogger.setDimensions(DimensionSet.of("Endpoint", "PostSomeOperation");
metricsLogger.putMetric("Latency", Duration.between(postSomeOperationStart, postSomeOperationEnd).toMillis(), Unit.MILLISECONDS);
metricsLogger.flush()
}
}
```
...knowing that there will be potentially hundreds of concurrent requests to the same handler on the same ECS instance? Or would the multiple concurrent calls to `.setDimension` across the threads step on each other's toes?

Contributor guide

Open the contributing guide

Research direction

Start with the MetricsLogger API and its setDimensions, putMetric, and flush behavior. Determine whether a singleton MetricsLogger can safely be shared across concurrent request threads with different dimensions, and document the supported usage or required alternative. The issue provides no file or test location, so repository-wide API and documentation searches are needed.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
observability-sre
Issue type
Documentation
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.