Skip to content

Commit

Permalink
Update metrics example with resource detection (#370)
Browse files Browse the repository at this point in the history
* Add logging exporter to root gradle file

* Udpate dependency declaration

* Add resource detection to metrics sample

* Add safety checks to prevent errors

* Update script to run application in cloud run
  • Loading branch information
psx95 committed Sep 17, 2024
1 parent f29adf6 commit 10d8c86
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 30 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ subprojects {
opentelemetry_autoconfigure_spi : "io.opentelemetry:opentelemetry-sdk-extension-autoconfigure-spi:${openTelemetryVersion}",
opentelemetry_agent_extension : "io.opentelemetry.javaagent:opentelemetry-javaagent-extension-api:${openTelemetryInstrumentationVersion}-alpha",
opentelemetry_otlp_exporter : "io.opentelemetry:opentelemetry-exporter-otlp:${openTelemetryVersion}",
opentelemetry_logging_exporter : "io.opentelemetry:opentelemetry-exporter-logging:${openTelemetryVersion}",
opentelemetry_gcp_resources : "io.opentelemetry.contrib:opentelemetry-gcp-resources:${openTelemetryContribVersion}-alpha",
spring_boot_starter_web : "org.springframework.boot:spring-boot-starter-web:${springWebVersion}",
spring_cloud_starter_openfeign : "org.springframework.cloud:spring-cloud-starter-openfeign:${springOpenFeignVersion}",
Expand Down
6 changes: 5 additions & 1 deletion examples/metrics/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,9 @@ dependencies {
implementation(libraries.opentelemetry_sdk_metrics)
implementation(libraries.google_cloud_monitoring)
implementation project(':exporter-metrics')
runtimeOnly(libraries.opentelemetry_gcp_resources)
implementation(libraries.opentelemetry_gcp_resources)
// required by resource detection
implementation(libraries.opentelemetry_autoconfigure_spi)
// this helps in debugging as it outputs all export to std out
implementation(libraries.opentelemetry_logging_exporter)
}
57 changes: 30 additions & 27 deletions examples/metrics/run_as_cloud-run-job.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,39 +14,42 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
CONTAINER_REGISTRY=cloud-run-applications
REGISTRY_LOCATION=us-central1
ARTIFACT_REPOSITORY=cloud-run-applications
JOB_NAME=job-metrics-export

if [[ -z "${GOOGLE_CLOUD_PROJECT}" ]]; then
echo "GOOGLE_CLOUD_PROJECT environment variable not set"
exit 1
fi

if [[ -z "${GOOGLE_APPLICATION_CREDENTIALS}" ]]; then
echo "GOOGLE_APPLICATION_CREDENTIALS environment variable not set"
exit 1
fi

if [[ -z "${GOOGLE_CLOUD_RUN_REGION}" ]]; then
echo "GOOGLE_CLOUD_RUN_REGION environment variable not set"
exit 1
fi
# Verify necessary environment variables are set
echo "${GOOGLE_CLOUD_PROJECT:?${UNSET_WARNING}}"
echo "${GOOGLE_APPLICATION_CREDENTIALS:?${UNSET_WARNING}}"
echo "${GOOGLE_CLOUD_RUN_REGION:?${UNSET_WARNING}}"

echo "ENVIRONMENT VARIABLES VERIFIED"

echo "CREATING CLOUD ARTIFACT REPOSITORY"
gcloud artifacts repositories create ${CONTAINER_REGISTRY} --repository-format=docker --location=${REGISTRY_LOCATION} --description="Sample applications to run on Google Cloud Run"
echo "CREATED ${CONTAINER_REGISTRY} in ${REGISTRY_LOCATION}"
# Safety check to verify if repository already exists.
if gcloud artifacts repositories describe ${ARTIFACT_REPOSITORY} \
--location="${GOOGLE_CLOUD_RUN_REGION}"
then
echo "Repository ${ARTIFACT_REPOSITORY} already exists."
else
echo "CREATING CLOUD ARTIFACT REPOSITORY"
gcloud artifacts repositories create ${ARTIFACT_REPOSITORY} --repository-format=docker --location=${GOOGLE_CLOUD_RUN_REGION} --description="Sample applications to run on Google Cloud Run"
echo "CREATED ${ARTIFACT_REPOSITORY} in ${GOOGLE_CLOUD_RUN_REGION}"
fi

echo "BUILDING SAMPLE APP IMAGE"
gradle clean jib --image "${REGISTRY_LOCATION}-docker.pkg.dev/${GOOGLE_CLOUD_PROJECT}/${CONTAINER_REGISTRY}/metrics-export-java"

echo "CREATING A CLOUD RUN JOB TO RUN THE CONTAINER"
gcloud run jobs create job-metrics-export \
--image "${REGISTRY_LOCATION}-docker.pkg.dev/${GOOGLE_CLOUD_PROJECT}/${CONTAINER_REGISTRY}/metrics-export-java" \
--max-retries 5 \
--region ${GOOGLE_CLOUD_RUN_REGION} \
--project="${GOOGLE_CLOUD_PROJECT}"
gradle clean jib --image "${GOOGLE_CLOUD_RUN_REGION}-docker.pkg.dev/${GOOGLE_CLOUD_PROJECT}/${ARTIFACT_REPOSITORY}/metrics-export-java"

# Safety check to verify if the job already exists
if gcloud run jobs describe ${JOB_NAME} --region="${GOOGLE_CLOUD_RUN_REGION}"
then
echo "Job ${JOB_NAME} already exists"
else
echo "CREATING A CLOUD RUN JOB TO RUN THE CONTAINER"
gcloud run jobs create job-metrics-export \
--image="${GOOGLE_CLOUD_RUN_REGION}-docker.pkg.dev/${GOOGLE_CLOUD_PROJECT}/${ARTIFACT_REPOSITORY}/metrics-export-java" \
--max-retries=5 \
--region="${GOOGLE_CLOUD_RUN_REGION}" \
--project="${GOOGLE_CLOUD_PROJECT}"
fi

echo "SETTING CLOUD RUN JOB REGION"
gcloud config set run/region "${GOOGLE_CLOUD_RUN_REGION}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@
import io.grpc.ManagedChannelBuilder;
import io.opentelemetry.api.metrics.LongCounter;
import io.opentelemetry.api.metrics.Meter;
import io.opentelemetry.contrib.gcp.resource.GCPResourceProvider;
import io.opentelemetry.exporter.logging.LoggingMetricExporter;
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
import io.opentelemetry.sdk.metrics.export.MetricExporter;
import io.opentelemetry.sdk.metrics.export.PeriodicMetricReader;
import io.opentelemetry.sdk.resources.Resource;
import java.io.IOException;
import java.time.Duration;
import java.util.Random;

public class MetricsExporterExample {
Expand Down Expand Up @@ -75,13 +79,20 @@ private static MetricConfiguration generateMetricExporterConfig(boolean useDefau
}

private static void setupMetricExporter(MetricConfiguration metricConfiguration) {
GCPResourceProvider resourceProvider = new GCPResourceProvider();
MetricExporter metricExporter =
GoogleCloudMetricExporter.createWithConfiguration(metricConfiguration);
MetricExporter metricDebugExporter = LoggingMetricExporter.create();
METER_PROVIDER =
SdkMeterProvider.builder()
.setResource(Resource.create(resourceProvider.getAttributes()))
.registerMetricReader(
PeriodicMetricReader.builder(metricExporter)
.setInterval(java.time.Duration.ofSeconds(30))
.setInterval(Duration.ofSeconds(30))
.build())
.registerMetricReader(
PeriodicMetricReader.builder(metricDebugExporter)
.setInterval(Duration.ofSeconds(30))
.build())
.build();

Expand Down
2 changes: 1 addition & 1 deletion examples/spring/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ dependencies {
// auto exporter makes Google Cloud exporters available to autoconfigure module
implementation project(':exporter-auto')
// this helps in debugging as it outputs all export to std out
implementation 'io.opentelemetry:opentelemetry-exporter-logging:1.33.0'
implementation(libraries.opentelemetry_logging_exporter)
}

test {
Expand Down

0 comments on commit 10d8c86

Please sign in to comment.