Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add meter provider configurer to change observable up down counter to… #89

Merged
merged 4 commits into from
Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions java/agent-nr-config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ This project demonstrates a simple Java application running with the OpenTelemet
The project consists of two modules:

1. [application](./application): Contains a simple Spring Boot application configured to run with OpenTelemetry.
2. [otel-initializer](./otel-initializer): Contains SPI configuration code, which allows for optional additional configuration not available via environment variables. The contents are packaged as a shadow jar, which the `application` module is configured to use as an initializer jar.
2. [config-extension](./config-extension): Contains SPI configuration code, which allows for optional additional configuration not available via environment variables. The contents are packaged as a shadow jar, which the `application` module is configured to use as an extension jar.

## Run

Expand All @@ -27,7 +27,7 @@ Run the application from a shell in the [java root](../) via:

The `bootRun` command will:
- Download the OpenTelemetry Java agent.
- Build the `otel-initializer` shadow jar.
- Build the `config-extension` shadow jar.
- Build the application executable jar.
- Run the application executable jar with jvmArgs that attach the OpenTelemetry Java agent. See the `bootRun` task config in [./application/build.gradle](./application/build.gradle) to see the jvmArg environment variable configuration.

Expand Down
12 changes: 6 additions & 6 deletions java/agent-nr-config/application/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ def agentPath = project.buildDir.toString() + "/otel/opentelemetry-javaagent-all
bootRun {
mainClass.set 'com.newrelic.app.Application'

// Before running, build the otel-intializer shadow jar
dependsOn(":agent-nr-config:otel-initializer:shadowJar")
// Before running, build the config-extension shadow jar
dependsOn(":agent-nr-config:config-extension:shadowJar")
// Before running, download the opentelemetry-java-instrumentation agent
dependsOn("downloadAgent")

def initializerPath = project(":agent-nr-config:otel-initializer").buildDir.toString() + "/libs/otel-initializer.jar"
def extensionPath = project(":agent-nr-config:config-extension").buildDir.toString() + "/libs/config-extension.jar"

jvmArgs = [
// Set the opentelemetry-java-instrumentation agent as the javaagent
"-javaagent:${agentPath}",
// Use the otel-initializer shadowJar to configure the agent via SPI
"-Dotel.javaagent.experimental.initializer.jar=${initializerPath}"
// Use the config-extension shadowJar to configure the agent via SPI
"-Dotel.javaagent.extensions=${extensionPath}"
]

// Additional configuration using standard autoconfiguration environment variables defined at:
Expand All @@ -39,7 +39,7 @@ bootRun {
"OTEL_LOGS_EXPORTER": "otlp",
// Enable experimental retry support because the internet is unreliable
"OTEL_EXPERIMENTAL_EXPORTER_OTLP_RETRY_ENABLED": "true"
// TODO: configure service.instance.id via environment variables and get rid of otel-initializer
// TODO: configure service.instance.id via environment variables and get rid of config-extension
])
}

Expand Down
22 changes: 22 additions & 0 deletions java/agent-nr-config/config-extension/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
plugins {
id 'java-library'
id 'com.github.johnrengelman.shadow' version '6.1.0'
}

tasks {
assemble.dependsOn(shadowJar)
}

shadowJar {
archiveClassifier.set("")
}

jar {
// Disable standard jar
enabled = false
}

dependencies {
compileOnly 'io.opentelemetry:opentelemetry-sdk-extension-autoconfigure'
compileOnly 'io.opentelemetry:opentelemetry-semconv'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.newrelic.otel.extension;

import static io.opentelemetry.semconv.resource.attributes.ResourceAttributes.SERVICE_INSTANCE_ID;

import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizer;
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider;
import io.opentelemetry.sdk.metrics.common.InstrumentType;
import io.opentelemetry.sdk.metrics.view.Aggregation;
import io.opentelemetry.sdk.metrics.view.InstrumentSelector;
import io.opentelemetry.sdk.metrics.view.View;
import io.opentelemetry.sdk.resources.Resource;
import java.util.UUID;

/**
* Note this class is wired into SPI via {@code
* resources/META-INF/services/io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider}
*/
public class Customizer implements AutoConfigurationCustomizerProvider {

@Override
public void customize(AutoConfigurationCustomizer autoConfiguration) {
// Add additional resource attributes programmatically
autoConfiguration.addResourceCustomizer(
(resource, configProperties) ->
resource.merge(
Resource.builder().put(SERVICE_INSTANCE_ID, UUID.randomUUID().toString()).build()));

// Aggregate OBSERVABLE_UP_DOWN_COUNTER as gauge instead of sum. This allows
// OBSERVABLE_UP_DOWN_COUNTER
// data to still be useful when aggregation temporality is set to DELTA.
autoConfiguration.addMeterProviderCustomizer(
(meterProviderBuilder, configProperties) ->
meterProviderBuilder.registerView(
InstrumentSelector.builder()
.setInstrumentType(InstrumentType.OBSERVABLE_UP_DOWN_COUNTER)
.build(),
View.builder().setAggregation(Aggregation.lastValue()).build()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.newrelic.otel.extension.Customizer
41 changes: 0 additions & 41 deletions java/agent-nr-config/otel-initializer/build.gradle

This file was deleted.

This file was deleted.

This file was deleted.

8 changes: 4 additions & 4 deletions java/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ gson.version=2.8.9
jackson.version=2.13.1
newrelic-agent.version=7.4.3
okhttp.version=4.9.0
otel.version=1.10.1
otel-agent.version=1.10.1
otel-alpha.version=1.10.1-alpha
otel-instrumentation.version=1.10.1-alpha
otel.version=1.11.0
otel-agent.version=1.11.0
otel-alpha.version=1.11.0-alpha
otel-instrumentation.version=1.11.0-alpha
otel-proto.version=0.11.0-alpha
protobuf-java-util.version=3.19.1
2 changes: 1 addition & 1 deletion java/settings.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
rootProject.name = "newrelic-opentelemetry-examples-java"
include 'agent-nr-config:application'
include 'agent-nr-config:otel-initializer'
include 'agent-nr-config:config-extension'
include 'autoconfigure-nr-config'
include 'logs-in-context-log4j2'
include 'otel-nr-dt:nr-app'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,23 @@

import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator;
import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.exporter.internal.retry.RetryPolicy;
import io.opentelemetry.exporter.internal.retry.RetryUtil;
import io.opentelemetry.exporter.logging.LoggingMetricExporter;
import io.opentelemetry.exporter.logging.LoggingSpanExporter;
import io.opentelemetry.exporter.otlp.internal.retry.RetryPolicy;
import io.opentelemetry.exporter.otlp.internal.retry.RetryUtil;
import io.opentelemetry.exporter.otlp.logs.OtlpGrpcLogExporter;
import io.opentelemetry.exporter.otlp.metrics.OtlpGrpcMetricExporter;
import io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporter;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.logs.SdkLogEmitterProvider;
import io.opentelemetry.sdk.logs.export.BatchLogProcessor;
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
import io.opentelemetry.sdk.metrics.common.InstrumentType;
import io.opentelemetry.sdk.metrics.data.AggregationTemporality;
import io.opentelemetry.sdk.metrics.export.PeriodicMetricReader;
import io.opentelemetry.sdk.metrics.view.Aggregation;
import io.opentelemetry.sdk.metrics.view.InstrumentSelector;
import io.opentelemetry.sdk.metrics.view.View;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.trace.SdkTracerProvider;
import io.opentelemetry.sdk.trace.export.BatchSpanProcessor;
Expand Down Expand Up @@ -70,7 +74,17 @@ public static OpenTelemetrySdk configureGlobal(String defaultServiceName) {
}

// Configure metrics
var meterProviderBuilder = SdkMeterProvider.builder().setResource(resource);
var meterProviderBuilder =
SdkMeterProvider.builder()
.setResource(resource)
// Aggregate OBSERVABLE_UP_DOWN_COUNTER as gauge instead of sum. This allows
// OBSERVABLE_UP_DOWN_COUNTER
// data to still be useful when aggregation temporality is set to DELTA.
.registerView(
InstrumentSelector.builder()
.setInstrumentType(InstrumentType.OBSERVABLE_UP_DOWN_COUNTER)
.build(),
View.builder().setAggregation(Aggregation.lastValue()).build());

var metricExporterBuilder =
OtlpGrpcMetricExporter.builder()
Expand Down