forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds Metering instrument interfaces and adapter implementations for opentelemetry instrument types: * Gauge - a single number that can go up or down * Histogram - bucketed samples * Counter - monotonically increasing summed value * UpDownCounter - summed value that may decrease Supports both Long* and Double* versions of the instruments. Instruments can be registered and retrieved by name through APMMeter which is available via the APMTelemetryProvider. The metering provider starts as the open telemetry noop provider. `telemetry.metrics.enabled` turns on metering.
- Loading branch information
1 parent
56706d0
commit c1fa624
Showing
33 changed files
with
1,811 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pr: 99832 | ||
summary: APM Metering API | ||
area: Infra/Core | ||
type: enhancement | ||
issues: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
180 changes: 180 additions & 0 deletions
180
modules/apm/src/main/java/org/elasticsearch/telemetry/apm/internal/metrics/APMMeter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.telemetry.apm.internal.metrics; | ||
|
||
import io.opentelemetry.api.GlobalOpenTelemetry; | ||
import io.opentelemetry.api.OpenTelemetry; | ||
import io.opentelemetry.api.metrics.Meter; | ||
|
||
import org.elasticsearch.cluster.service.ClusterService; | ||
import org.elasticsearch.common.component.AbstractLifecycleComponent; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.telemetry.apm.internal.APMTelemetryProvider; | ||
import org.elasticsearch.telemetry.metric.DoubleCounter; | ||
import org.elasticsearch.telemetry.metric.DoubleGauge; | ||
import org.elasticsearch.telemetry.metric.DoubleHistogram; | ||
import org.elasticsearch.telemetry.metric.DoubleUpDownCounter; | ||
import org.elasticsearch.telemetry.metric.LongCounter; | ||
import org.elasticsearch.telemetry.metric.LongGauge; | ||
import org.elasticsearch.telemetry.metric.LongHistogram; | ||
import org.elasticsearch.telemetry.metric.LongUpDownCounter; | ||
|
||
import java.security.AccessController; | ||
import java.security.PrivilegedAction; | ||
import java.util.function.Supplier; | ||
|
||
import static org.elasticsearch.telemetry.apm.internal.APMAgentSettings.TELEMETRY_METRICS_ENABLED_SETTING; | ||
|
||
public class APMMeter extends AbstractLifecycleComponent implements org.elasticsearch.telemetry.metric.Meter { | ||
private final Instruments instruments; | ||
|
||
private final Supplier<Meter> otelMeterSupplier; | ||
private final Supplier<Meter> noopMeterSupplier; | ||
|
||
private volatile boolean enabled; | ||
|
||
public APMMeter(Settings settings) { | ||
this(settings, APMMeter.otelMeter(), APMMeter.noopMeter()); | ||
} | ||
|
||
public APMMeter(Settings settings, Supplier<Meter> otelMeterSupplier, Supplier<Meter> noopMeterSupplier) { | ||
this.enabled = TELEMETRY_METRICS_ENABLED_SETTING.get(settings); | ||
this.otelMeterSupplier = otelMeterSupplier; | ||
this.noopMeterSupplier = noopMeterSupplier; | ||
this.instruments = new Instruments(enabled ? createOtelMeter() : createNoopMeter()); | ||
} | ||
|
||
/** | ||
* @see org.elasticsearch.telemetry.apm.internal.APMAgentSettings#addClusterSettingsListeners(ClusterService, APMTelemetryProvider) | ||
*/ | ||
public void setEnabled(boolean enabled) { | ||
this.enabled = enabled; | ||
if (enabled) { | ||
instruments.setProvider(createOtelMeter()); | ||
} else { | ||
instruments.setProvider(createNoopMeter()); | ||
} | ||
} | ||
|
||
@Override | ||
protected void doStart() {} | ||
|
||
@Override | ||
protected void doStop() { | ||
instruments.setProvider(createNoopMeter()); | ||
} | ||
|
||
@Override | ||
protected void doClose() {} | ||
|
||
@Override | ||
public DoubleCounter registerDoubleCounter(String name, String description, String unit) { | ||
return instruments.registerDoubleCounter(name, description, unit); | ||
} | ||
|
||
@Override | ||
public DoubleCounter getDoubleCounter(String name) { | ||
return instruments.getDoubleCounter(name); | ||
} | ||
|
||
@Override | ||
public DoubleUpDownCounter registerDoubleUpDownCounter(String name, String description, String unit) { | ||
return instruments.registerDoubleUpDownCounter(name, description, unit); | ||
} | ||
|
||
@Override | ||
public DoubleUpDownCounter getDoubleUpDownCounter(String name) { | ||
return instruments.getDoubleUpDownCounter(name); | ||
} | ||
|
||
@Override | ||
public DoubleGauge registerDoubleGauge(String name, String description, String unit) { | ||
return instruments.registerDoubleGauge(name, description, unit); | ||
} | ||
|
||
@Override | ||
public DoubleGauge getDoubleGauge(String name) { | ||
return instruments.getDoubleGauge(name); | ||
} | ||
|
||
@Override | ||
public DoubleHistogram registerDoubleHistogram(String name, String description, String unit) { | ||
return instruments.registerDoubleHistogram(name, description, unit); | ||
} | ||
|
||
@Override | ||
public DoubleHistogram getDoubleHistogram(String name) { | ||
return instruments.getDoubleHistogram(name); | ||
} | ||
|
||
@Override | ||
public LongCounter registerLongCounter(String name, String description, String unit) { | ||
return instruments.registerLongCounter(name, description, unit); | ||
} | ||
|
||
@Override | ||
public LongCounter getLongCounter(String name) { | ||
return instruments.getLongCounter(name); | ||
} | ||
|
||
@Override | ||
public LongUpDownCounter registerLongUpDownCounter(String name, String description, String unit) { | ||
return instruments.registerLongUpDownCounter(name, description, unit); | ||
} | ||
|
||
@Override | ||
public LongUpDownCounter getLongUpDownCounter(String name) { | ||
return instruments.getLongUpDownCounter(name); | ||
} | ||
|
||
@Override | ||
public LongGauge registerLongGauge(String name, String description, String unit) { | ||
return instruments.registerLongGauge(name, description, unit); | ||
} | ||
|
||
@Override | ||
public LongGauge getLongGauge(String name) { | ||
return instruments.getLongGauge(name); | ||
} | ||
|
||
@Override | ||
public LongHistogram registerLongHistogram(String name, String description, String unit) { | ||
return instruments.registerLongHistogram(name, description, unit); | ||
} | ||
|
||
@Override | ||
public LongHistogram getLongHistogram(String name) { | ||
return instruments.getLongHistogram(name); | ||
} | ||
|
||
Meter createOtelMeter() { | ||
assert this.enabled; | ||
return AccessController.doPrivileged((PrivilegedAction<Meter>) otelMeterSupplier::get); | ||
} | ||
|
||
private Meter createNoopMeter() { | ||
return noopMeterSupplier.get(); | ||
} | ||
|
||
private static Supplier<Meter> noopMeter() { | ||
return () -> OpenTelemetry.noop().getMeter("noop"); | ||
} | ||
|
||
// to be used within doPrivileged block | ||
private static Supplier<Meter> otelMeter() { | ||
var openTelemetry = GlobalOpenTelemetry.get(); | ||
var meter = openTelemetry.getMeter("elasticsearch"); | ||
return () -> meter; | ||
} | ||
|
||
// scope for testing | ||
Instruments getInstruments() { | ||
return instruments; | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
...pm/src/main/java/org/elasticsearch/telemetry/apm/internal/metrics/AbstractInstrument.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.telemetry.apm.internal.metrics; | ||
|
||
import io.opentelemetry.api.metrics.Meter; | ||
|
||
import org.elasticsearch.core.Nullable; | ||
import org.elasticsearch.telemetry.metric.Instrument; | ||
|
||
import java.security.AccessController; | ||
import java.security.PrivilegedAction; | ||
import java.util.Objects; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
/** | ||
* An instrument that contains the name, description and unit. The delegate may be replaced when | ||
* the provider is updated. | ||
* Subclasses should implement the builder, which is used on initialization and provider updates. | ||
* @param <T> delegated instrument | ||
*/ | ||
public abstract class AbstractInstrument<T> implements Instrument { | ||
private final AtomicReference<T> delegate; | ||
private final String name; | ||
private final String description; | ||
private final String unit; | ||
|
||
public AbstractInstrument(Meter meter, String name, String description, String unit) { | ||
this.name = Objects.requireNonNull(name); | ||
this.description = Objects.requireNonNull(description); | ||
this.unit = Objects.requireNonNull(unit); | ||
this.delegate = new AtomicReference<>(doBuildInstrument(meter)); | ||
} | ||
|
||
private T doBuildInstrument(Meter meter) { | ||
return AccessController.doPrivileged((PrivilegedAction<T>) () -> buildInstrument(meter)); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getUnit() { | ||
return unit.toString(); | ||
} | ||
|
||
T getInstrument() { | ||
return delegate.get(); | ||
} | ||
|
||
String getDescription() { | ||
return description; | ||
} | ||
|
||
void setProvider(@Nullable Meter meter) { | ||
delegate.set(doBuildInstrument(Objects.requireNonNull(meter))); | ||
} | ||
|
||
abstract T buildInstrument(Meter meter); | ||
} |
Oops, something went wrong.