|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.metrics; |
| 9 | + |
| 10 | +import io.opentelemetry.proto.collector.metrics.v1.ExportMetricsServiceRequest; |
| 11 | + |
| 12 | +import org.apache.logging.log4j.LogManager; |
| 13 | +import org.apache.logging.log4j.Logger; |
| 14 | +import org.elasticsearch.action.ActionListener; |
| 15 | +import org.elasticsearch.action.ActionRequest; |
| 16 | +import org.elasticsearch.action.ActionRequestValidationException; |
| 17 | +import org.elasticsearch.action.ActionResponse; |
| 18 | +import org.elasticsearch.action.ActionType; |
| 19 | +import org.elasticsearch.action.support.ActionFilters; |
| 20 | +import org.elasticsearch.action.support.HandledTransportAction; |
| 21 | +import org.elasticsearch.cluster.project.ProjectResolver; |
| 22 | +import org.elasticsearch.common.bytes.BytesReference; |
| 23 | +import org.elasticsearch.common.io.stream.StreamInput; |
| 24 | +import org.elasticsearch.common.io.stream.StreamOutput; |
| 25 | +import org.elasticsearch.common.util.concurrent.EsExecutors; |
| 26 | +import org.elasticsearch.indices.IndicesService; |
| 27 | +import org.elasticsearch.injection.guice.Inject; |
| 28 | +import org.elasticsearch.tasks.Task; |
| 29 | +import org.elasticsearch.transport.TransportService; |
| 30 | + |
| 31 | +import java.io.IOException; |
| 32 | + |
| 33 | +public class MetricsTransportAction extends HandledTransportAction< |
| 34 | + MetricsTransportAction.MetricsRequest, |
| 35 | + MetricsTransportAction.MetricsResponse> { |
| 36 | + |
| 37 | + public static final String NAME = "indices:data/write/metrics"; |
| 38 | + public static final ActionType<MetricsTransportAction.MetricsResponse> TYPE = new ActionType<>(NAME); |
| 39 | + |
| 40 | + private static final Logger logger = LogManager.getLogger(MetricsTransportAction.class); |
| 41 | + |
| 42 | + private final ProjectResolver projectResolver; |
| 43 | + private final IndicesService indicesService; |
| 44 | + |
| 45 | + @Inject |
| 46 | + public MetricsTransportAction( |
| 47 | + TransportService transportService, |
| 48 | + ActionFilters actionFilters, |
| 49 | + ProjectResolver projectResolver, |
| 50 | + IndicesService indicesService |
| 51 | + ) { |
| 52 | + super(NAME, transportService, actionFilters, MetricsRequest::new, EsExecutors.DIRECT_EXECUTOR_SERVICE); |
| 53 | + this.projectResolver = projectResolver; |
| 54 | + this.indicesService = indicesService; |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + protected void doExecute(Task task, MetricsRequest request, ActionListener<MetricsResponse> listener) { |
| 59 | + try { |
| 60 | + var metricsServiceRequest = ExportMetricsServiceRequest.parseFrom(request.exportMetricsServiceRequest.streamInput()); |
| 61 | + |
| 62 | + logger.info("Received " + metricsServiceRequest.getResourceMetricsCount() + " metrics"); |
| 63 | + |
| 64 | + // resolve index somehow |
| 65 | + logger.info("Indices service " + indicesService); |
| 66 | + |
| 67 | + listener.onResponse(new MetricsResponse()); |
| 68 | + } catch (Exception e) { |
| 69 | + listener.onFailure(e); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + public static class MetricsRequest extends ActionRequest { |
| 74 | + private String index; |
| 75 | + private BytesReference exportMetricsServiceRequest; |
| 76 | + |
| 77 | + public MetricsRequest(StreamInput in) throws IOException { |
| 78 | + super(in); |
| 79 | + index = in.readString(); |
| 80 | + exportMetricsServiceRequest = in.readBytesReference(); |
| 81 | + } |
| 82 | + |
| 83 | + public MetricsRequest(String index, BytesReference exportMetricsServiceRequest) { |
| 84 | + this.index = index; |
| 85 | + this.exportMetricsServiceRequest = exportMetricsServiceRequest; |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public ActionRequestValidationException validate() { |
| 90 | + return null; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + public static class MetricsResponse extends ActionResponse { |
| 95 | + @Override |
| 96 | + public void writeTo(StreamOutput out) throws IOException { |
| 97 | + |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments