Class AbstractCollector<REQ extends com.google.protobuf.AbstractMessage,RESP extends com.google.protobuf.AbstractMessage,RECORD>
java.lang.Object
java.util.concurrent.SubmissionPublisher<RECORD>
io.mishmash.opentelemetry.server.collector.AbstractCollector<REQ,RESP,RECORD>
- Type Parameters:
REQ- the type of OTLP request being handled by this collectorRESP- the type of OTLP responseRECORD- the type of data items a request is broken into
- All Implemented Interfaces:
AutoCloseable,Flow.Publisher<RECORD>
- Direct Known Subclasses:
LogsCollector,MetricsCollector,TracesCollector
public abstract class AbstractCollector<REQ extends com.google.protobuf.AbstractMessage,RESP extends com.google.protobuf.AbstractMessage,RECORD>
extends SubmissionPublisher<RECORD>
This class handles the OTLP protocol for gRPC and HTTP transports for a collector of a given signal type (like logs, metrics or traces).
As each collector attaches to own gRPC methods and own HTTP endpoints, the common functionality of receiving, parsing, 'queuing' signals for subscribing classes to process and responding back to clients is kept here.
This class provides methods to bind to Vert.x gRPC and HTTP servers
and to process the OTLP requests and responses, leaving only the
task of breaking up the data into individual items to be submitted
to the subscribers. See LogsCollector, MetricsCollector
and TracesCollector.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intDefault timeout (in sec) to complete a call to close() this collector.static final intDefault maximum HTTP request body size in bytes.static final intDefault size of a subscriber queue.static final intDefault timeout (in sec) to process a single OTLP packet.protected static final intHTTP status code 502.protected static final intHTTP status code 400.protected static final intHTTP status code 417.protected static final intHTTP status code 504.protected static final intHTTP status code 500.static final intHTTP status code 200.protected static final intHTTP status code 413.protected static final intHTTP status code 503.protected static final intHTTP status code 429. -
Constructor Summary
ConstructorsConstructorDescriptionAbstractCollector(String httpUrlPath, io.grpc.MethodDescriptor<REQ, RESP> exportMethod, Instrumentation otelHelper, Supplier<com.google.protobuf.Parser<REQ>> requestParser, Supplier<com.google.protobuf.Message.Builder> requestBuilder, Supplier<com.google.protobuf.Parser<RESP>> responseParser, Supplier<com.google.protobuf.Message.Builder> responseBuilder, ForkJoinPool threadPool) Create a new collector. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidaddDroppedRequestItems(long count, String transport, String encoding) Update the counter of how many OTLP items (logs, spans, metrics) have been dropped so far.protected voidaddFailedRequests(long count, String transport, String encoding, boolean retryable) Updates the number of failed requests counter.protected voidaddGrpcFailedRequests(long count, io.vertx.grpc.common.GrpcStatus status) Update the number of failed requests counter for a given gRPC error code.protected voidaddHttpFailedRequests(long count, int httpResponseStatusCode, String encoding) Update the number of failed requests counter for a given HTTP error code.protected voidaddPartiallySucceededRequests(long count, String transport, String encoding) Updates the number of partially successful requests counter.protected voidaddRequestItems(long count, String transport, String encoding) Update the counter of how many OTLP items (logs, spans, metrics) have been processed so far.protected voidaddRequestsInProcess(long count, String transport, String encoding) Update the counter of number of requests currently being processed.protected voidaddSucceededRequests(long count, String transport, String encoding) Updates the number of successful requests counter.voidbind(io.vertx.ext.web.Router router) Bind this collector to the given HTTPRouterand start accepting requests.voidbind(io.vertx.grpc.server.GrpcServer server) Binds this collector to the gRPC server.protected voidcheckBody(io.vertx.ext.web.RoutingContext context) Checks the body of an incoming HTTP request for validity.protected voidcheckContentType(io.vertx.ext.web.RoutingContext ctx) Checks the Content-Type header of an incoming HTTP request for validity.protected voidcheckSubscribers(io.vertx.ext.web.RoutingContext ctx) Fail an incoming request if we have no subscribers.voidclose()voidcloseExceptionally(Throwable error) protected abstract RESPCalled to compute the correct response to the client after an entire batch (or packet) of OpenTelemetry logs, metrics or spans has been processed.protected intgetErrorCode(io.vertx.ext.web.RoutingContext ctx) Returns the previously set error code for the given HTTP request.protected InstrumentationGet the OpenTelemetry instrumentation helper.protected io.opentelemetry.context.ContextgetOtelContext(io.vertx.ext.web.RoutingContext ctx) Get the telemetry context previously associated with this HTTP routing context.protected REQgetOtelRequest(io.vertx.ext.web.RoutingContext ctx) Get the OTLP request object from a HTTP request.protected RESPgetOtelResponse(io.vertx.ext.web.RoutingContext ctx) Get the OTLP response object from the routing context.protected voidHandles an incoming gRPC request.protected StringhttpEncoding(io.vertx.ext.web.RoutingContext ctx) Gets the encoding of a HTTP OTLP request.protected voidCreate own instrumentation metrics.protected booleanisError(io.vertx.ext.web.RoutingContext ctx) Checks if the processing of a HTTP request encountered an error.protected booleanisJsonContent(io.vertx.core.http.HttpServerRequest request) Check if the HTTP request is JSON formatted.protected booleanisJsonRequest(io.vertx.ext.web.RoutingContext ctx) Check if the HTTP routing context was previously marked as for a JSON-encoded OTLP request.protected booleanisProtobufContent(io.vertx.core.http.HttpServerRequest request) Check if the HTTP request is protobuf formatted.protected booleanisRetryable(int httpResponseStatusCode) Check if a given HTTP error can be retried by the client.protected booleanisRetryable(io.vertx.grpc.common.GrpcStatus status) Check if a given gRPC error can be retried by the client.loadBatch(REQ request, String transport, String encoding, io.opentelemetry.context.Context otelContext) Creates a new batch from a client's request.protected longparseContentLengthHeader(io.vertx.core.http.HttpServerRequest request) Parses the Content-Length header of an incoming HTTP request.protected REQParses an incoming HTTP json-encoded request.protected REQParses an incoming HTTP protobuf-encoded request.protected voidparseHttpRequest(io.vertx.ext.web.RoutingContext ctx) Parse an incoming HTTP request.protected voidPublish an incoming HTTP request to subscribers.publish(REQ request, String transport, String encoding, io.opentelemetry.context.Context otelContext) Publishes an OTLP packet (request) to all current subscribers.protected voidrespond(io.vertx.ext.web.RoutingContext ctx) Send the HTTP response back to the client.protected voidrespond(io.vertx.grpc.server.GrpcServerRequest<REQ, RESP> grpcRequest, REQ request, Batch<RECORD> batch, Throwable err) Responds to a processed gRPC OTLP request.protected StringresponseToJson(RESP response) Converts a response object to JSON before sending back to client.protected byte[]responseToProtobuf(RESP response) Converts a response object to protobuf before sending back to client.protected voidsetErrorCode(io.vertx.ext.web.RoutingContext ctx, int errorCode) Sets an error in a HTTP routing context so that a proper response can be returned to the client.protected voidsetJsonRequest(io.vertx.ext.web.RoutingContext ctx, boolean isJson) Mark the HTTP routing context as handling a JSON-encoded request.protected voidsetOtelContext(io.vertx.ext.web.RoutingContext ctx, io.opentelemetry.context.Context otelContext) Configure the telemetry context to be used when processing this HTTP request.protected voidsetOtelRequest(io.vertx.ext.web.RoutingContext ctx, REQ request) Sets the OTLP request object within the routing context for later use.protected voidsetOtelResponse(io.vertx.ext.web.RoutingContext ctx, RESP response) Sets the OTLP response object within the routing context for later use.protected abstract StringGet the type of telemetry signals processed by this collector.Methods inherited from class java.util.concurrent.SubmissionPublisher
consume, estimateMaximumLag, estimateMinimumDemand, getClosedException, getExecutor, getMaxBufferCapacity, getNumberOfSubscribers, getSubscribers, hasSubscribers, isClosed, isSubscribed, offer, offer, submit, subscribe
-
Field Details
-
DEFAULT_TIMEOUT_SEC
public static final int DEFAULT_TIMEOUT_SECDefault timeout (in sec) to process a single OTLP packet.- See Also:
-
DEFAULT_CLOSE_TIMETOUT_SEC
public static final int DEFAULT_CLOSE_TIMETOUT_SECDefault timeout (in sec) to complete a call to close() this collector.- See Also:
-
DEFAULT_MAX_BODY_LEN
public static final int DEFAULT_MAX_BODY_LENDefault maximum HTTP request body size in bytes.- See Also:
-
DEFAULT_SUBSCRIBER_QUEUE_SIZE
public static final int DEFAULT_SUBSCRIBER_QUEUE_SIZEDefault size of a subscriber queue.- See Also:
-
HTTP_OK
public static final int HTTP_OKHTTP status code 200.- See Also:
-
HTTP_BAD_REQUEST
protected static final int HTTP_BAD_REQUESTHTTP status code 400.- See Also:
-
HTTP_PAYLOAD_TOO_LARGE
protected static final int HTTP_PAYLOAD_TOO_LARGEHTTP status code 413.- See Also:
-
HTTP_EXPECTATION_FAILED
protected static final int HTTP_EXPECTATION_FAILEDHTTP status code 417.- See Also:
-
HTTP_TOO_MANY_REQUESTS
protected static final int HTTP_TOO_MANY_REQUESTSHTTP status code 429.- See Also:
-
HTTP_INTERNAL_ERROR
protected static final int HTTP_INTERNAL_ERRORHTTP status code 500.- See Also:
-
HTTP_BAD_GATEWAY
protected static final int HTTP_BAD_GATEWAYHTTP status code 502.- See Also:
-
HTTP_SERVICE_UNAVAILABLE
protected static final int HTTP_SERVICE_UNAVAILABLEHTTP status code 503.- See Also:
-
HTTP_GATEWAY_TIMEOUT
protected static final int HTTP_GATEWAY_TIMEOUTHTTP status code 504.- See Also:
-
-
Constructor Details
-
AbstractCollector
public AbstractCollector(String httpUrlPath, io.grpc.MethodDescriptor<REQ, RESP> exportMethod, Instrumentation otelHelper, Supplier<com.google.protobuf.Parser<REQ>> requestParser, Supplier<com.google.protobuf.Message.Builder> requestBuilder, Supplier<com.google.protobuf.Parser<RESP>> responseParser, Supplier<com.google.protobuf.Message.Builder> responseBuilder, ForkJoinPool threadPool) Create a new collector.- Parameters:
httpUrlPath- the URL path for HTTP requestsexportMethod- the gRPC method to bind tootelHelper- a helper for own instrumentationrequestParser- to get a parser for the request typerequestBuilder- to get a builder for the request typeresponseParser- to get a parser for the response typeresponseBuilder- to get a builder for the response typethreadPool- the thread pool to use for processing
-
-
Method Details
-
loadBatch
protected abstract Batch<RECORD> loadBatch(REQ request, String transport, String encoding, io.opentelemetry.context.Context otelContext) Creates a new batch from a client's request.- Parameters:
request- the client's OTLP requesttransport- the OTLP transport usedencoding- the encoding of the OTLP packetotelContext- context to use for processing- Returns:
- a new
Batchwith all items of the request
-
getBatchResponse
protected abstract RESP getBatchResponse(REQ request, Batch<RECORD> completedBatch, String transport, String encoding) Called to compute the correct response to the client after an entire batch (or packet) of OpenTelemetry logs, metrics or spans has been processed.- Parameters:
request- the original request from the clientcompletedBatch- the completed batch of recordstransport- the OTLP transport used - "grpc" or "http"encoding- - the OTLP encoding - "protobuf" or "json"- Returns:
- the correct OTLP reposne to be sent back to the client
-
telemetrySignalType
Get the type of telemetry signals processed by this collector. Used to format some strings like metric names, logs, etc.- Returns:
- the signal type
-
initInstrumentation
protected void initInstrumentation()Create own instrumentation metrics. -
getInstrumentation
Get the OpenTelemetry instrumentation helper.- Returns:
- the instrumentation helper
-
parseHttpProtobuf
Parses an incoming HTTP protobuf-encoded request.- Parameters:
is- an input stream of the data- Returns:
- the parsed request object
- Throws:
Exception- if an error is encountered
-
parseHttpJson
Parses an incoming HTTP json-encoded request.- Parameters:
is- an input stream of the data- Returns:
- the parsed request object
- Throws:
Exception- if an error is encountered
-
responseToJson
Converts a response object to JSON before sending back to client.- Parameters:
response- the response to format- Returns:
- a JSON string
- Throws:
Exception- if an error is encountered
-
responseToProtobuf
Converts a response object to protobuf before sending back to client.- Parameters:
response- the response to format- Returns:
- a JSON string
- Throws:
Exception- if an error is encountered
-
publish
protected Batch<RECORD> publish(REQ request, String transport, String encoding, io.opentelemetry.context.Context otelContext) Publishes an OTLP packet (request) to all current subscribers.- Parameters:
request- the client's requesttransport- the OTLP transport usedencoding- the encoding of the OTLP transportotelContext- a context to be used for own telemetry- Returns:
- a
Batchof all items
-
respond
protected void respond(io.vertx.grpc.server.GrpcServerRequest<REQ, RESP> grpcRequest, REQ request, Batch<RECORD> batch, Throwable err) Responds to a processed gRPC OTLP request.- Parameters:
grpcRequest- the gRPC requestrequest- the OTLP requestbatch- the processingBatcherr- set if an error occurred during processing
-
handle
Handles an incoming gRPC request.- Parameters:
request- the gRPC request
-
bind
public void bind(io.vertx.grpc.server.GrpcServer server) Binds this collector to the gRPC server.- Parameters:
server- the gRPC server to bind to
-
checkContentType
protected void checkContentType(io.vertx.ext.web.RoutingContext ctx) Checks the Content-Type header of an incoming HTTP request for validity.- Parameters:
ctx- the routing context of the request
-
checkBody
protected void checkBody(io.vertx.ext.web.RoutingContext context) Checks the body of an incoming HTTP request for validity.- Parameters:
context- the routing context of the request
-
checkSubscribers
protected void checkSubscribers(io.vertx.ext.web.RoutingContext ctx) Fail an incoming request if we have no subscribers.- Parameters:
ctx- the request's context
-
parseHttpRequest
protected void parseHttpRequest(io.vertx.ext.web.RoutingContext ctx) Parse an incoming HTTP request.- Parameters:
ctx- the request context
-
publish
Publish an incoming HTTP request to subscribers.- Parameters:
ctx- the request's contextrequest- the request to publish
-
respond
protected void respond(io.vertx.ext.web.RoutingContext ctx) Send the HTTP response back to the client.- Parameters:
ctx- the routing context of this HTTP request
-
bind
public void bind(io.vertx.ext.web.Router router) Bind this collector to the given HTTPRouterand start accepting requests.- Parameters:
router- the router
-
isJsonContent
protected boolean isJsonContent(io.vertx.core.http.HttpServerRequest request) Check if the HTTP request is JSON formatted.- Parameters:
request- the request- Returns:
- true if JSON was used by the client
-
isProtobufContent
protected boolean isProtobufContent(io.vertx.core.http.HttpServerRequest request) Check if the HTTP request is protobuf formatted.- Parameters:
request- the request- Returns:
- true if protobuf was used by the client
-
getOtelRequest
Get the OTLP request object from a HTTP request.- Parameters:
ctx- the request context- Returns:
- the OTLP request object
-
setOtelRequest
Sets the OTLP request object within the routing context for later use.- Parameters:
ctx- the routing contextrequest- the OTLP request object
-
getOtelResponse
Get the OTLP response object from the routing context.- Parameters:
ctx- the context- Returns:
- the response object
-
setOtelResponse
Sets the OTLP response object within the routing context for later use.- Parameters:
ctx- the routing contextresponse- the OTLP response object
-
isJsonRequest
protected boolean isJsonRequest(io.vertx.ext.web.RoutingContext ctx) Check if the HTTP routing context was previously marked as for a JSON-encoded OTLP request.- Parameters:
ctx- the routing context- Returns:
- true if OTLP request was JSON-encoded
-
setJsonRequest
protected void setJsonRequest(io.vertx.ext.web.RoutingContext ctx, boolean isJson) Mark the HTTP routing context as handling a JSON-encoded request.- Parameters:
ctx- the routing contextisJson- set to true to mark this request as JSON-encoded
-
httpEncoding
Gets the encoding of a HTTP OTLP request.- Parameters:
ctx- the routing context- Returns:
- "json" or "protobuf"
-
isError
protected boolean isError(io.vertx.ext.web.RoutingContext ctx) Checks if the processing of a HTTP request encountered an error.- Parameters:
ctx- the routing context- Returns:
- true if an error was encountered
-
setErrorCode
protected void setErrorCode(io.vertx.ext.web.RoutingContext ctx, int errorCode) Sets an error in a HTTP routing context so that a proper response can be returned to the client.- Parameters:
ctx- the routing contexterrorCode- the error code
-
getErrorCode
protected int getErrorCode(io.vertx.ext.web.RoutingContext ctx) Returns the previously set error code for the given HTTP request.- Parameters:
ctx- the routing context- Returns:
- the error code previously set
-
setOtelContext
protected void setOtelContext(io.vertx.ext.web.RoutingContext ctx, io.opentelemetry.context.Context otelContext) Configure the telemetry context to be used when processing this HTTP request.- Parameters:
ctx- the HTTP routing contextotelContext- the telemetry context to be set
-
getOtelContext
protected io.opentelemetry.context.Context getOtelContext(io.vertx.ext.web.RoutingContext ctx) Get the telemetry context previously associated with this HTTP routing context.- Parameters:
ctx- the routing context- Returns:
- the associated telemetry context
-
parseContentLengthHeader
protected long parseContentLengthHeader(io.vertx.core.http.HttpServerRequest request) Parses the Content-Length header of an incoming HTTP request.- Parameters:
request- the HTTP request- Returns:
- the parsed value or -1 if header is missing or unparseable
-
addSucceededRequests
Updates the number of successful requests counter.- Parameters:
count- a count of how many to addtransport- the OTLP transport usedencoding- the OTLP transport encoding of the requests
-
addPartiallySucceededRequests
Updates the number of partially successful requests counter.- Parameters:
count- a count of how many to addtransport- the OTLP transport usedencoding- the OTLP transport encoding of the requests
-
isRetryable
protected boolean isRetryable(io.vertx.grpc.common.GrpcStatus status) Check if a given gRPC error can be retried by the client.- Parameters:
status- the gRPC error code- Returns:
- true if error is retriable
-
isRetryable
protected boolean isRetryable(int httpResponseStatusCode) Check if a given HTTP error can be retried by the client.- Parameters:
httpResponseStatusCode- the HTTP error code- Returns:
- true if error is retriable
-
addGrpcFailedRequests
protected void addGrpcFailedRequests(long count, io.vertx.grpc.common.GrpcStatus status) Update the number of failed requests counter for a given gRPC error code.- Parameters:
count- the count to addstatus- the gRPC error code
-
addFailedRequests
Updates the number of failed requests counter.- Parameters:
count- a count of how many to addtransport- the OTLP transport usedencoding- the OTLP transport encoding of the requestsretryable- true if the error allows the client to retry
-
addHttpFailedRequests
Update the number of failed requests counter for a given HTTP error code.- Parameters:
count- the count to addhttpResponseStatusCode- the HTTP error codeencoding- the OTLP encoding used in the requests
-
addRequestsInProcess
Update the counter of number of requests currently being processed.- Parameters:
count- the count of requests to add or subtract (if negative)transport- the transport used by the requestsencoding- the transport encoding
-
addRequestItems
Update the counter of how many OTLP items (logs, spans, metrics) have been processed so far.- Parameters:
count- the number of items to addtransport- the OTLP transport usedencoding- the OTLP encoding used
-
addDroppedRequestItems
Update the counter of how many OTLP items (logs, spans, metrics) have been dropped so far.- Parameters:
count- the number of items to addtransport- the OTLP transport usedencoding- the OTLP encoding used
-
close
public void close()- Specified by:
closein interfaceAutoCloseable- Overrides:
closein classSubmissionPublisher<RECORD>
-
closeExceptionally
- Overrides:
closeExceptionallyin classSubmissionPublisher<RECORD>
-