asafm commented on code in PR #3968:
URL: https://github.com/apache/bookkeeper/pull/3968#discussion_r1228576740


##########
stats/bookkeeper-stats-providers/otel-metrics-provider/src/main/java/org/apache/bookkeeper/stats/otel/OtelStatsLogger.java:
##########
@@ -0,0 +1,102 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations 
under
+ * the License.
+ */
+package org.apache.bookkeeper.stats.otel;
+
+import io.opentelemetry.api.common.Attributes;
+import java.util.StringJoiner;
+import org.apache.bookkeeper.stats.Counter;
+import org.apache.bookkeeper.stats.Gauge;
+import org.apache.bookkeeper.stats.OpStatsLogger;
+import org.apache.bookkeeper.stats.StatsLogger;
+
+public class OtelStatsLogger implements StatsLogger {
+
+    private final OtelMetricsProvider provider;
+    private final String scope;
+    private final Attributes attributes;
+
+    OtelStatsLogger(OtelMetricsProvider provider, String scope, Attributes 
attributes) {
+        this.provider = provider;
+        this.scope = scope;
+        this.attributes = attributes;
+    }
+
+    @Override
+    public OpStatsLogger getOpStatsLogger(String name) {
+        return provider.opStats.computeIfAbsent(scopeContext(name), sc -> new 
OtelOpStatsLogger(provider.meter, sc));
+    }
+
+    @Override
+    public OpStatsLogger getThreadScopedOpStatsLogger(String name) {
+        return getOpStatsLogger(name);

Review Comment:
   In the original implementation, they made a great effort to take attributes 
from the current thread. Isn't it helpful?



##########
stats/bookkeeper-stats-providers/otel-metrics-provider/src/main/java/org/apache/bookkeeper/stats/otel/OtelMetricsProvider.java:
##########
@@ -0,0 +1,150 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations 
under
+ * the License.
+ */
+package org.apache.bookkeeper.stats.otel;
+
+// CHECKSTYLE.OFF: IllegalImport
+import io.netty.util.internal.PlatformDependent;
+import io.opentelemetry.api.OpenTelemetry;
+import io.opentelemetry.api.common.Attributes;
+import io.opentelemetry.api.metrics.Meter;
+import io.opentelemetry.instrumentation.runtimemetrics.BufferPools;
+import io.opentelemetry.instrumentation.runtimemetrics.Classes;
+import io.opentelemetry.instrumentation.runtimemetrics.Cpu;
+import io.opentelemetry.instrumentation.runtimemetrics.GarbageCollector;
+import io.opentelemetry.instrumentation.runtimemetrics.MemoryPools;
+import io.opentelemetry.instrumentation.runtimemetrics.Threads;
+import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
+import io.opentelemetry.sdk.metrics.Aggregation;
+import io.opentelemetry.sdk.metrics.InstrumentSelector;
+import io.opentelemetry.sdk.metrics.InstrumentType;
+import io.opentelemetry.sdk.metrics.View;
+import java.lang.management.BufferPoolMXBean;
+import java.lang.management.ManagementFactory;
+import java.lang.reflect.Field;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Optional;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.function.Supplier;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.bookkeeper.stats.StatsLogger;
+import org.apache.bookkeeper.stats.StatsProvider;
+import org.apache.commons.configuration.Configuration;
+// CHECKSTYLE.ON: IllegalImport
+
+@Slf4j
+public class OtelMetricsProvider implements StatsProvider {
+
+    private static final String METER_NAME = "org.apache.bookkeeper";
+
+    /*
+     * These acts a registry of the metrics defined in this provider
+     */
+    final ConcurrentMap<ScopeContext, OtelCounter> counters = new 
ConcurrentHashMap<>();
+    final ConcurrentMap<ScopeContext, OtelOpStatsLogger> opStats = new 
ConcurrentHashMap<>();
+
+    private static final List<Double> histogramBuckets = Arrays.asList(
+            0.1, 0.2, 0.5,
+            1.0, 2.0, 5.0,
+            10.0, 20.0, 50.0,
+            100.0, 200.0, 500.0,
+            1_000.0, 2_000.0, 5_000.0,
+            10_000.0, 20_000.0, 50_000.0
+    );
+
+    private final OpenTelemetry openTelemetry;
+
+    final Meter meter;
+
+    OtelMetricsProvider() {
+        AutoConfiguredOpenTelemetrySdk sdk = 
AutoConfiguredOpenTelemetrySdk.builder()
+                .addMeterProviderCustomizer(
+                        (sdkMeterProviderBuilder, configProperties) ->
+                                sdkMeterProviderBuilder.registerView(
+                                        InstrumentSelector.builder()
+                                                .setMeterName(METER_NAME)
+                                                
.setType(InstrumentType.HISTOGRAM)
+                                                .build(),
+                                        View.builder()
+                                                
.setAggregation(Aggregation.explicitBucketHistogram(histogramBuckets))
+                                                .build())
+
+                ).build();
+        this.openTelemetry = sdk.getOpenTelemetrySdk();
+        this.meter = openTelemetry.getMeter(METER_NAME);
+    }
+
+    @Override
+    public void start(Configuration conf) {
+        boolean exposeDefaultJVMMetrics = 
conf.getBoolean("exposeDefaultJVMMetrics", true);
+        if (exposeDefaultJVMMetrics) {
+            // Include standard JVM stats
+            MemoryPools.registerObservers(openTelemetry);
+            BufferPools.registerObservers(openTelemetry);
+            Classes.registerObservers(openTelemetry);
+            Cpu.registerObservers(openTelemetry);
+            Threads.registerObservers(openTelemetry);
+            GarbageCollector.registerObservers(openTelemetry);
+
+            meter.gaugeBuilder("process.runtime.jvm.memory.direct_bytes_used")
+                    .buildWithCallback(odm -> 
odm.record(getDirectMemoryUsage.get()));
+
+            meter.gaugeBuilder("process.runtime.jvm.memory.direct_bytes_max")
+                    .buildWithCallback(odm -> 
odm.record(PlatformDependent.estimateMaxDirectMemory()));
+        }
+    }
+
+    @Override
+    public void stop() {
+    }
+
+    @Override
+    public StatsLogger getStatsLogger(String scope) {
+        return new OtelStatsLogger(this, scope, Attributes.empty());
+    }
+
+    /*
+     * Try to get Netty counter of used direct memory. This will be correct, 
unlike the JVM values.

Review Comment:
   Did you ever find out why the JVM report lies? :)



##########
stats/bookkeeper-stats-providers/otel-metrics-provider/src/main/java/org/apache/bookkeeper/stats/otel/OtelMetricsProvider.java:
##########
@@ -0,0 +1,150 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations 
under
+ * the License.
+ */
+package org.apache.bookkeeper.stats.otel;
+
+// CHECKSTYLE.OFF: IllegalImport
+import io.netty.util.internal.PlatformDependent;
+import io.opentelemetry.api.OpenTelemetry;
+import io.opentelemetry.api.common.Attributes;
+import io.opentelemetry.api.metrics.Meter;
+import io.opentelemetry.instrumentation.runtimemetrics.BufferPools;
+import io.opentelemetry.instrumentation.runtimemetrics.Classes;
+import io.opentelemetry.instrumentation.runtimemetrics.Cpu;
+import io.opentelemetry.instrumentation.runtimemetrics.GarbageCollector;
+import io.opentelemetry.instrumentation.runtimemetrics.MemoryPools;
+import io.opentelemetry.instrumentation.runtimemetrics.Threads;
+import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
+import io.opentelemetry.sdk.metrics.Aggregation;
+import io.opentelemetry.sdk.metrics.InstrumentSelector;
+import io.opentelemetry.sdk.metrics.InstrumentType;
+import io.opentelemetry.sdk.metrics.View;
+import java.lang.management.BufferPoolMXBean;
+import java.lang.management.ManagementFactory;
+import java.lang.reflect.Field;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Optional;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.function.Supplier;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.bookkeeper.stats.StatsLogger;
+import org.apache.bookkeeper.stats.StatsProvider;
+import org.apache.commons.configuration.Configuration;
+// CHECKSTYLE.ON: IllegalImport
+
+@Slf4j
+public class OtelMetricsProvider implements StatsProvider {
+
+    private static final String METER_NAME = "org.apache.bookkeeper";
+
+    /*
+     * These acts a registry of the metrics defined in this provider
+     */
+    final ConcurrentMap<ScopeContext, OtelCounter> counters = new 
ConcurrentHashMap<>();
+    final ConcurrentMap<ScopeContext, OtelOpStatsLogger> opStats = new 
ConcurrentHashMap<>();
+
+    private static final List<Double> histogramBuckets = Arrays.asList(
+            0.1, 0.2, 0.5,
+            1.0, 2.0, 5.0,
+            10.0, 20.0, 50.0,
+            100.0, 200.0, 500.0,
+            1_000.0, 2_000.0, 5_000.0,
+            10_000.0, 20_000.0, 50_000.0
+    );
+
+    private final OpenTelemetry openTelemetry;
+
+    final Meter meter;
+
+    OtelMetricsProvider() {
+        AutoConfiguredOpenTelemetrySdk sdk = 
AutoConfiguredOpenTelemetrySdk.builder()
+                .addMeterProviderCustomizer(
+                        (sdkMeterProviderBuilder, configProperties) ->
+                                sdkMeterProviderBuilder.registerView(
+                                        InstrumentSelector.builder()
+                                                .setMeterName(METER_NAME)
+                                                
.setType(InstrumentType.HISTOGRAM)
+                                                .build(),
+                                        View.builder()
+                                                
.setAggregation(Aggregation.explicitBucketHistogram(histogramBuckets))
+                                                .build())
+
+                ).build();
+        this.openTelemetry = sdk.getOpenTelemetrySdk();
+        this.meter = openTelemetry.getMeter(METER_NAME);
+    }
+
+    @Override
+    public void start(Configuration conf) {
+        boolean exposeDefaultJVMMetrics = 
conf.getBoolean("exposeDefaultJVMMetrics", true);
+        if (exposeDefaultJVMMetrics) {
+            // Include standard JVM stats
+            MemoryPools.registerObservers(openTelemetry);
+            BufferPools.registerObservers(openTelemetry);
+            Classes.registerObservers(openTelemetry);
+            Cpu.registerObservers(openTelemetry);
+            Threads.registerObservers(openTelemetry);
+            GarbageCollector.registerObservers(openTelemetry);
+
+            meter.gaugeBuilder("process.runtime.jvm.memory.direct_bytes_used")

Review Comment:
   This should be upDownCounter. Gauges are, by definition, values, you can't 
aggregate across attribute sets like the temperature of a room - doing a sum 
for all rooms is meaningless.



##########
stats/bookkeeper-stats-providers/otel-metrics-provider/src/main/java/org/apache/bookkeeper/stats/otel/OtelStatsLogger.java:
##########
@@ -0,0 +1,102 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations 
under
+ * the License.
+ */
+package org.apache.bookkeeper.stats.otel;
+
+import io.opentelemetry.api.common.Attributes;
+import java.util.StringJoiner;
+import org.apache.bookkeeper.stats.Counter;
+import org.apache.bookkeeper.stats.Gauge;
+import org.apache.bookkeeper.stats.OpStatsLogger;
+import org.apache.bookkeeper.stats.StatsLogger;
+
+public class OtelStatsLogger implements StatsLogger {
+
+    private final OtelMetricsProvider provider;
+    private final String scope;
+    private final Attributes attributes;
+
+    OtelStatsLogger(OtelMetricsProvider provider, String scope, Attributes 
attributes) {
+        this.provider = provider;
+        this.scope = scope;
+        this.attributes = attributes;
+    }
+
+    @Override
+    public OpStatsLogger getOpStatsLogger(String name) {
+        return provider.opStats.computeIfAbsent(scopeContext(name), sc -> new 
OtelOpStatsLogger(provider.meter, sc));
+    }
+
+    @Override
+    public OpStatsLogger getThreadScopedOpStatsLogger(String name) {
+        return getOpStatsLogger(name);
+    }
+
+    @Override
+    public Counter getCounter(String name) {
+        return provider.counters.computeIfAbsent(scopeContext(name), sc -> new 
OtelCounter(provider.meter, sc));
+    }
+
+    @Override
+    public Counter getThreadScopedCounter(String name) {
+        return getCounter(name);
+    }
+
+    @Override
+    public <T extends Number> void registerGauge(String name, Gauge<T> gauge) {
+        provider.meter.gaugeBuilder(completeName(name))

Review Comment:
   This should be async upDownCounter 



##########
stats/bookkeeper-stats-providers/otel-metrics-provider/src/main/java/org/apache/bookkeeper/stats/otel/OtelMetricsProvider.java:
##########
@@ -0,0 +1,150 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations 
under
+ * the License.
+ */
+package org.apache.bookkeeper.stats.otel;
+
+// CHECKSTYLE.OFF: IllegalImport
+import io.netty.util.internal.PlatformDependent;
+import io.opentelemetry.api.OpenTelemetry;
+import io.opentelemetry.api.common.Attributes;
+import io.opentelemetry.api.metrics.Meter;
+import io.opentelemetry.instrumentation.runtimemetrics.BufferPools;
+import io.opentelemetry.instrumentation.runtimemetrics.Classes;
+import io.opentelemetry.instrumentation.runtimemetrics.Cpu;
+import io.opentelemetry.instrumentation.runtimemetrics.GarbageCollector;
+import io.opentelemetry.instrumentation.runtimemetrics.MemoryPools;
+import io.opentelemetry.instrumentation.runtimemetrics.Threads;
+import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
+import io.opentelemetry.sdk.metrics.Aggregation;
+import io.opentelemetry.sdk.metrics.InstrumentSelector;
+import io.opentelemetry.sdk.metrics.InstrumentType;
+import io.opentelemetry.sdk.metrics.View;
+import java.lang.management.BufferPoolMXBean;
+import java.lang.management.ManagementFactory;
+import java.lang.reflect.Field;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Optional;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.function.Supplier;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.bookkeeper.stats.StatsLogger;
+import org.apache.bookkeeper.stats.StatsProvider;
+import org.apache.commons.configuration.Configuration;
+// CHECKSTYLE.ON: IllegalImport
+
+@Slf4j
+public class OtelMetricsProvider implements StatsProvider {
+
+    private static final String METER_NAME = "org.apache.bookkeeper";
+
+    /*
+     * These acts a registry of the metrics defined in this provider
+     */
+    final ConcurrentMap<ScopeContext, OtelCounter> counters = new 
ConcurrentHashMap<>();
+    final ConcurrentMap<ScopeContext, OtelOpStatsLogger> opStats = new 
ConcurrentHashMap<>();
+
+    private static final List<Double> histogramBuckets = Arrays.asList(
+            0.1, 0.2, 0.5,
+            1.0, 2.0, 5.0,
+            10.0, 20.0, 50.0,
+            100.0, 200.0, 500.0,
+            1_000.0, 2_000.0, 5_000.0,
+            10_000.0, 20_000.0, 50_000.0
+    );
+
+    private final OpenTelemetry openTelemetry;
+
+    final Meter meter;
+
+    OtelMetricsProvider() {
+        AutoConfiguredOpenTelemetrySdk sdk = 
AutoConfiguredOpenTelemetrySdk.builder()
+                .addMeterProviderCustomizer(
+                        (sdkMeterProviderBuilder, configProperties) ->
+                                sdkMeterProviderBuilder.registerView(
+                                        InstrumentSelector.builder()
+                                                .setMeterName(METER_NAME)
+                                                
.setType(InstrumentType.HISTOGRAM)
+                                                .build(),
+                                        View.builder()
+                                                
.setAggregation(Aggregation.explicitBucketHistogram(histogramBuckets))
+                                                .build())
+
+                ).build();
+        this.openTelemetry = sdk.getOpenTelemetrySdk();
+        this.meter = openTelemetry.getMeter(METER_NAME);
+    }
+
+    @Override
+    public void start(Configuration conf) {
+        boolean exposeDefaultJVMMetrics = 
conf.getBoolean("exposeDefaultJVMMetrics", true);
+        if (exposeDefaultJVMMetrics) {
+            // Include standard JVM stats
+            MemoryPools.registerObservers(openTelemetry);
+            BufferPools.registerObservers(openTelemetry);
+            Classes.registerObservers(openTelemetry);
+            Cpu.registerObservers(openTelemetry);
+            Threads.registerObservers(openTelemetry);
+            GarbageCollector.registerObservers(openTelemetry);
+
+            meter.gaugeBuilder("process.runtime.jvm.memory.direct_bytes_used")

Review Comment:
   Better do it how they do it:
   ```
       meter
           .upDownCounterBuilder("process.runtime.jvm.memory.usage")
           .setDescription("Measure of memory used")
           .setUnit("By")
           .buildWithCallback(callback(poolBeans, MemoryPoolMXBean::getUsage, 
MemoryUsage::getUsed));
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to