Skip to content

Commit

Permalink
precommit
Browse files Browse the repository at this point in the history
  • Loading branch information
pgomulka committed Jul 3, 2024
1 parent 73c0e5f commit 665dc5f
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
*/

package org.elasticsearch.gradle.testclusters;
import com.google.protobuf.Any;
import com.google.protobuf.StringValue;

import io.grpc.CallOptions;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.MethodDescriptor;
import io.grpc.stub.ClientCalls;
import io.grpc.stub.StreamObserver;
import io.grpc.protobuf.ProtoUtils;
import io.grpc.stub.ClientCalls;

import com.google.protobuf.Any;
import com.google.protobuf.StringValue;

public class DynamicGrpcClient {
private final ManagedChannel channel;
Expand Down Expand Up @@ -54,9 +54,9 @@ public void shutdown() {
public static void main(String[] args) {

DynamicGrpcClient client = new DynamicGrpcClient("localhost", 9999);
// client.
// String response = client.sendMessage("Hello, dynamic message!");
// System.out.println("Response: " + response);
// client.shutdown();
// client.
// String response = client.sendMessage("Hello, dynamic message!");
// System.out.println("Response: " + response);
// client.shutdown();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,9 @@

package org.elasticsearch.gradle.testclusters;

import com.google.protobuf.ByteString;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

import io.grpc.BindableService;
import io.grpc.CallOptions;
import io.grpc.Channel;
import io.grpc.MethodDescriptor;
import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.ServerServiceDefinition;
import io.grpc.protobuf.ProtoUtils;
import io.grpc.stub.ServerCalls;
import io.grpc.stub.StreamObserver;
import com.google.protobuf.Any;
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.StringValue;

import io.opentelemetry.proto.collector.metrics.v1.ExportMetricsServiceRequest;
import io.opentelemetry.proto.collector.metrics.v1.ExportMetricsServiceResponse;
import io.opentelemetry.proto.collector.metrics.v1.MetricsServiceGrpc;
Expand All @@ -37,11 +21,7 @@
import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;

/**
* This is a server which just accepts lines of JSON code and if the JSON
Expand Down Expand Up @@ -72,11 +52,13 @@ public static void main(String[] args) throws IOException, InterruptedException
server.start();
server.blockUntilShutdown();
}

private void blockUntilShutdown() throws InterruptedException {
if (server != null) {
server.awaitTermination();
}
}

private Server server;

/**
Expand All @@ -88,16 +70,11 @@ private void blockUntilShutdown() throws InterruptedException {
public synchronized int start() throws IOException {
if (server != null) {
int port = server.getPort();
logger.lifecycle("MockApmServer is already running. Reusing on address:port "+ port);
logger.lifecycle("MockApmServer is already running. Reusing on address:port " + port);
return port;
}


server = ServerBuilder.forPort(port)
.addService(new TraceServiceImpl())
.addService(new MetricsServiceImpl())
.build()
.start();
server = ServerBuilder.forPort(port).addService(new TraceServiceImpl()).addService(new MetricsServiceImpl()).build().start();
System.out.println("Server started, listening on " + port);
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
System.err.println("*** shutting down gRPC server since JVM is shutting down");
Expand Down Expand Up @@ -125,14 +102,14 @@ static class TraceServiceImpl extends TraceServiceGrpc.TraceServiceImplBase {
@Override
public void export(ExportTraceServiceRequest request, StreamObserver<ExportTraceServiceResponse> responseObserver) {
super.export(request, responseObserver);
System.out.println("TRACE SERVICE RECEIVED "+request.toString());
System.out.println("TRACE SERVICE RECEIVED " + request.toString());
}
}

static class MetricsServiceImpl extends MetricsServiceGrpc.MetricsServiceImplBase {
@Override
public void export(ExportMetricsServiceRequest request, StreamObserver<ExportMetricsServiceResponse> responseObserver) {
System.out.println("METRICS SERVICE RECEIVED "+request.toString());
System.out.println("METRICS SERVICE RECEIVED " + request.toString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,44 +135,44 @@ class APMJvmOptions {
*/
static List<String> apmJvmOptions(Settings settings, @Nullable SecureSettings secrets, Path logsDir, Path tmpdir) throws UserException,
IOException {
// final Path agentJar = findAgentJar();
//
// if (agentJar == null) {
// return List.of();
// }
//
// final Map<String, String> propertiesMap = extractApmSettings(settings);
//
// // Configures a log file to write to. Don't disable writing to a log file,
// // as the agent will then require extra Security Manager permissions when
// // it tries to do something else, and it's just painful.
// propertiesMap.put("log_file", logsDir.resolve("apm-agent.json").toString());
//
// // No point doing anything if we don't have a destination for the trace data, and it can't be configured dynamically
// if (propertiesMap.containsKey("server_url") == false && propertiesMap.containsKey("server_urls") == false) {
// return List.of();
// }
//
// if (propertiesMap.containsKey("service_node_name") == false) {
// final String nodeName = settings.get("node.name");
// if (nodeName != null) {
// propertiesMap.put("service_node_name", nodeName);
// }
// }
//
// if (secrets != null) {
// extractSecureSettings(secrets, propertiesMap);
// }
// final Map<String, String> dynamicSettings = extractDynamicSettings(propertiesMap);
//
// final Path tmpProperties = writeApmProperties(tmpdir, propertiesMap);
// final Path agentJar = findAgentJar();
//
// if (agentJar == null) {
// return List.of();
// }
//
// final Map<String, String> propertiesMap = extractApmSettings(settings);
//
// // Configures a log file to write to. Don't disable writing to a log file,
// // as the agent will then require extra Security Manager permissions when
// // it tries to do something else, and it's just painful.
// propertiesMap.put("log_file", logsDir.resolve("apm-agent.json").toString());
//
// // No point doing anything if we don't have a destination for the trace data, and it can't be configured dynamically
// if (propertiesMap.containsKey("server_url") == false && propertiesMap.containsKey("server_urls") == false) {
// return List.of();
// }
//
// if (propertiesMap.containsKey("service_node_name") == false) {
// final String nodeName = settings.get("node.name");
// if (nodeName != null) {
// propertiesMap.put("service_node_name", nodeName);
// }
// }
//
// if (secrets != null) {
// extractSecureSettings(secrets, propertiesMap);
// }
// final Map<String, String> dynamicSettings = extractDynamicSettings(propertiesMap);
//
// final Path tmpProperties = writeApmProperties(tmpdir, propertiesMap);

final List<String> options = new ArrayList<>();
// Use an agent argument to specify the config file instead of e.g. `-Delastic.apm.config_file=...`
// because then the agent won't try to reload the file, and we can remove it after startup.
// options.add(agentCommandLineOption(agentJar, tmpProperties));
// options.add(agentCommandLineOption(agentJar, tmpProperties));

// dynamicSettings.forEach((key, value) -> options.add("-Delastic.apm." + key + "=" + value));
// dynamicSettings.forEach((key, value) -> options.add("-Delastic.apm." + key + "=" + value));

return options;
}
Expand Down
1 change: 1 addition & 0 deletions libs/telemetry/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

module org.elasticsearch.telemetry {
requires org.elasticsearch.base;

exports org.elasticsearch.telemetry;
exports org.elasticsearch.telemetry.metric;
exports org.elasticsearch.telemetry.tracing;
Expand Down
46 changes: 21 additions & 25 deletions modules/apm/src/main/java/org/elasticsearch/telemetry/apm/APM.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.exporter.otlp.metrics.OtlpGrpcMetricExporter;
import io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporter;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
import io.opentelemetry.sdk.metrics.export.PeriodicMetricReader;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.trace.SdkTracerProvider;
import io.opentelemetry.sdk.trace.export.BatchSpanProcessor;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand All @@ -26,16 +34,6 @@
import org.elasticsearch.telemetry.apm.internal.APMMeterService;
import org.elasticsearch.telemetry.apm.internal.APMTelemetryProvider;
import org.elasticsearch.telemetry.apm.internal.tracing.APMTracer;

import io.opentelemetry.exporter.otlp.metrics.OtlpGrpcMetricExporter;
import io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporter;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
import io.opentelemetry.sdk.metrics.export.PeriodicMetricReader;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.trace.SdkTracerProvider;
import io.opentelemetry.sdk.trace.export.BatchSpanProcessor;

import org.elasticsearch.telemetry.apm.jvm.JvmFdMetrics;

import java.security.AccessController;
Expand Down Expand Up @@ -69,20 +67,21 @@ public class APM extends Plugin implements NetworkPlugin, TelemetryPlugin {
private final SetOnce<APMTelemetryProvider> telemetryProvider = new SetOnce<>();
private final Settings settings;
JvmFdMetrics jvmFdMetrics = new JvmFdMetrics();

public APM(Settings settings) {
this.settings = settings;
}

@Override
public TelemetryProvider getTelemetryProvider(Settings settings) {
Log4jBridgeHandler.install(false,"",true);
//metrics to check https://test-apm-9fc321.kb.us-central1.gcp.cloud.es.io:9243/app/r/s/Zy7QT
Log4jBridgeHandler.install(false, "", true);
// metrics to check https://test-apm-9fc321.kb.us-central1.gcp.cloud.es.io:9243/app/r/s/Zy7QT
String ENDPOINT = settings.get("telemetry.agent.server_url");
TimeValue interval = settings.getAsTime("telemetry.agent.metrics_interval", TimeValue.ONE_MINUTE);
// String ENDPOINT = "https://3b4a40972a494625a76de9ef337ef425.apm.us-central1.gcp.cloud.es.io:443";
// String ENDPOINT = "https://3b4a40972a494625a76de9ef337ef425.apm.us-central1.gcp.cloud.es.io:443";

AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
createOtel(ENDPOINT,interval);
createOtel(ENDPOINT, interval);
return null;
});

Expand All @@ -94,36 +93,33 @@ public TelemetryProvider getTelemetryProvider(Settings settings) {
private static void createOtel(String ENDPOINT, TimeValue interval) {
OtlpGrpcSpanExporter spanExporter = OtlpGrpcSpanExporter.builder()
.setEndpoint(ENDPOINT)
// .addHeader("Authorization", "Bearer RXX6gm1WeXlx8qHw3a")
// .addHeader("Authorization", "Bearer RXX6gm1WeXlx8qHw3a")
.build();
OtlpGrpcMetricExporter metricExporter = OtlpGrpcMetricExporter.builder()
.setEndpoint(ENDPOINT)
// .addHeader("Authorization", "Bearer RXX6gm1WeXlx8qHw3a")
// .addHeader("Authorization", "Bearer RXX6gm1WeXlx8qHw3a")
.build();
Resource resource = Resource.getDefault().merge(Resource.create(
Attributes.of(AttributeKey.stringKey("service.name"), "testing_otel_sdk")));
Resource resource = Resource.getDefault()
.merge(Resource.create(Attributes.of(AttributeKey.stringKey("service.name"), "testing_otel_sdk")));

PeriodicMetricReader build = PeriodicMetricReader.builder(metricExporter).setInterval(interval.getSeconds(), TimeUnit.SECONDS).build();

SdkMeterProvider sdkMeterProvider = SdkMeterProvider.builder()
.setResource(resource)
.registerMetricReader(PeriodicMetricReader.builder(metricExporter).setInterval(interval.getSeconds(), TimeUnit.SECONDS).build())
.registerMetricReader(build)
.build();
SdkTracerProvider sdkTracerProvider = SdkTracerProvider.builder()
.setResource(resource)
.addSpanProcessor(BatchSpanProcessor.builder(spanExporter).build())
.build();

OpenTelemetrySdk.builder()
.setTracerProvider(sdkTracerProvider)
.setMeterProvider(sdkMeterProvider)
.buildAndRegisterGlobal();

OpenTelemetrySdk.builder().setTracerProvider(sdkTracerProvider).setMeterProvider(sdkMeterProvider).buildAndRegisterGlobal();

}

@Override
public Collection<?> createComponents(PluginServices services) {
//grpc won't work due to okhttp not being modular (relying on internal package)
// grpc won't work due to okhttp not being modular (relying on internal package)
final APMTracer apmTracer = telemetryProvider.get().getTracer();
final APMMeterService apmMeter = telemetryProvider.get().getMeterService();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@
import java.lang.invoke.MethodType;
import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;

import javax.annotation.Nullable;

public class JvmFdMetrics /*extends AbstractLifecycleListener */{
public class JvmFdMetrics /*extends AbstractLifecycleListener */ {

// using method handles to avoid direct reference to `com.sun.*` classes which we can't reference directly
// as it is not available on all JDKs this is further enforced by the animal sniffer plugin
Expand Down Expand Up @@ -57,10 +58,9 @@ private static void register(MeterRegistry registry, String name, final Operatin
if (methodHandle == NOOP) {
return;
}
registry.registerLongGauge("es."+name+".total", "","count",()-> {
registry.registerLongGauge("es." + name + ".total", "", "count", () -> {
return new LongWithAttributes(getLongWithAttributes(mxBean, methodHandle));
}
);
});

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -714,9 +714,9 @@ static class AllPermissionCheck implements BootstrapCheck {

@Override
public final BootstrapCheckResult check(BootstrapContext context) {
// if (isAllPermissionGranted()) {
// return BootstrapCheck.BootstrapCheckResult.failure("granting the all permission effectively disables security");
// }
// if (isAllPermissionGranted()) {
// return BootstrapCheck.BootstrapCheckResult.failure("granting the all permission effectively disables security");
// }
return BootstrapCheckResult.success();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ final class Security {
private Security() {}

static void setSecurityManager(@SuppressWarnings("removal") SecurityManager sm) {
// System.setSecurityManager(sm);
// System.setSecurityManager(sm);
}

/**
Expand Down
1 change: 0 additions & 1 deletion server/src/main/java/org/elasticsearch/node/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,6 @@ public void onTimeout(TimeValue timeout) {
injector.getInstance(NodeMetrics.class).start();
injector.getInstance(HealthPeriodicLogger.class).start();


logger.info("started {}", transportService.getLocalNode());

pluginsService.filterPlugins(ClusterPlugin.class).forEach(ClusterPlugin::onNodeStarted);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,22 +102,24 @@ public void testWeekBasedDates() {
// first week of 2015 starts on Monday 2014-12-29 because 4days belong to 2019
assertThat(DateFormatters.from(dateFormatter.parse("2015")), equalTo(ZonedDateTime.of(2014, 12, 29, 0, 0, 0, 0, ZoneOffset.UTC)));
}

public void testUtc() throws ParseException {
Date d;
Locale locale = Locale.forLanguageTag("sd-Arab");
//8u391 - 12:39:24 PM BST
// d = new SimpleDateFormat("hh:mm:ss a z", locale).parse("12:39:24 PM BST"); //Unparseable date: "12:39:24 PM BST"
// System.out.println(d);
//11.0.22 - 12:59:58 منجهند، شام GMT+٠١:٠٠
// 8u391 - 12:39:24 PM BST
// d = new SimpleDateFormat("hh:mm:ss a z", locale).parse("12:39:24 PM BST"); //Unparseable date: "12:39:24 PM BST"
// System.out.println(d);
// 11.0.22 - 12:59:58 منجهند، شام GMT+٠١:٠٠
d = new SimpleDateFormat("hh:mm:ss a z", locale).parse("12:59:58 منجهند، شام GMT+٠١:٠٠");
System.out.println(d);
//17.0.11 - 01:04:51 منجهند، شام GMT+٠١:٠٠
// 17.0.11 - 01:04:51 منجهند، شام GMT+٠١:٠٠
d = new SimpleDateFormat("hh:mm:ss a z", locale).parse("01:04:51 منجهند، شام GMT+٠١:٠٠");
System.out.println(d);
//22.0.1 - 01:09:33 منجهند، شام BST
// 22.0.1 - 01:09:33 منجهند، شام BST
d = new SimpleDateFormat("hh:mm:ss a z", locale).parse("01:09:33 منجهند، شام");
System.out.println(d);
}

public void testEpochMillisParser() {
DateFormatter formatter = DateFormatters.forPattern("epoch_millis");
{
Expand Down

0 comments on commit 665dc5f

Please sign in to comment.