Skip to content

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

@kerling

Description

@kerling

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions