Skip to content

Commit

Permalink
add metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
lmolkova committed Dec 7, 2023
1 parent 88cb739 commit de59aed
Show file tree
Hide file tree
Showing 10 changed files with 808 additions and 5 deletions.
50 changes: 46 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ dependencies {
}

// start - define tasks to download, unzip, and generate from opentelemetry/semantic-conventions
var generatorVersion = "foo10"
var generatorVersion = "foo14"
val semanticConventionsRepoZip = "https://github.com/open-telemetry/semantic-conventions/archive/v$semanticConventionsVersion.zip"
val schemaUrl = "https://opentelemetry.io/schemas/$semanticConventionsVersion"

Expand Down Expand Up @@ -110,7 +110,27 @@ val generateStableSemanticAttributes by tasks.registering(Exec::class) {
"--template", "/templates/SemanticAttributes.java.j2",
"--output", "/output/Attributes.java",
"--file-per-group", "root_namespace",
"-Dattr_filter=is_stable",
"-Dfilter=is_stable",
"-Dpkg=io.opentelemetry.semconv"))
}

val generateStableSemanticMetrics by tasks.registering(Exec::class) {
dependsOn(unzipConfigurationSchema)

standardOutput = System.out
executable = "docker"
setArgs(listOf(
"run",
"--rm",
"-v", "$buildDir/semantic-conventions/model:/source",
"-v", "$projectDir/buildscripts/templates:/templates",
"-v", "$projectDir/src/main/java/io/opentelemetry/semconv/:/output",
"semconvgen:$generatorVersion",
"--yaml-root", "/source", "code",
"--template", "/templates/SemanticMetrics.java.j2",
"--output", "/output/Metrics.java",
"--file-per-group", "root_namespace",
"-Dfilter=is_stable",
"-Dpkg=io.opentelemetry.semconv"))
}

Expand All @@ -130,7 +150,27 @@ val generateExperimentalSemanticAttributes by tasks.registering(Exec::class) {
"--template", "/templates/SemanticAttributes.java.j2",
"--output", "/output/Attributes.java",
"--file-per-group", "root_namespace",
"-Dattr_filter=is_experimental",
"-Dfilter=is_experimental",
"-Dpkg=io.opentelemetry.semconv.$semanticConventionsVersionEscaped"))
}

val generateExperimentalSemanticMetrics by tasks.registering(Exec::class) {
dependsOn(unzipConfigurationSchema)

standardOutput = System.out
executable = "docker"
setArgs(listOf(
"run",
"--rm",
"-v", "$buildDir/semantic-conventions/model:/source",
"-v", "$projectDir/buildscripts/templates:/templates",
"-v", "$projectDir/src/main/java/io/opentelemetry/semconv/$semanticConventionsVersionEscaped/:/output",
"semconvgen:$generatorVersion",
"--yaml-root", "/source", "code",
"--template", "/templates/SemanticMetrics.java.j2",
"--output", "/output/Metrics.java",
"--file-per-group", "root_namespace",
"-Dfilter=is_experimental",
"-Dpkg=io.opentelemetry.semconv.$semanticConventionsVersionEscaped"))
}

Expand All @@ -142,14 +182,16 @@ val checkSchemaUrls by tasks.registering {
}

val schemaUrls = schemaUrlsFile.readLines()
if (!schemaUrls.contains(schemaUrl)) {
if (!schemaUrls.any { it.contains(schemaUrl) }) {
throw GradleException("SchemaUrls file does not contain $schemaUrl")
}
}

val generateSemanticConventions by tasks.registering {
dependsOn(generateStableSemanticAttributes)
dependsOn(generateExperimentalSemanticAttributes)
// dependsOn(generateStableSemanticMetrics)
dependsOn(generateExperimentalSemanticMetrics)
dependsOn(checkSchemaUrls)
}

Expand Down
1 change: 1 addition & 0 deletions buildscripts/checkstyle-suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<!-- Suppress Javadoc-related checks in non-public code. -->
<suppress checks="JavadocParagraph|JavadocMethod" files="[\\/](jmh|test)[\\/]" />
<suppress checks="SummaryJavadoc" files=".*Attributes"/>
<suppress checks="SummaryJavadoc" files=".*Metrics"/>
<suppress checks="RedundantImport" files="[\\/]build[\\/]"/>
<suppress checks="PackageName" files=".*Attributes"/>
</suppressions>
2 changes: 1 addition & 1 deletion buildscripts/templates/SemanticAttributes.java.j2
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
{%- macro print_value(type, value) -%}
{{ "\"" if type == "String"}}{{value}}{{ "\"" if type == "String"}}
{%- endmacro %}
{%- set filtered_attributes = attributes_and_templates | select(attr_filter) | list %}
{%- set filtered_attributes = attributes_and_templates | select(filter) | list %}
{%- if filtered_attributes | count > 0 %}
/*
* Copyright The OpenTelemetry Authors
Expand Down
50 changes: 50 additions & 0 deletions buildscripts/templates/SemanticMetrics.java.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{%- macro to_java_instrument_builder_factory(instrument) -%}
{%- if instrument == "counter" -%}
counterBuilder
{%- elif instrument == "histogram" -%}
histogramBuilder
{%- elif instrument == "updowncounter" -%}
upDownCounterBuilder
{%- elif instrument == "gauge" -%}
gaugeBuilder
{%- endif -%}
{%- endmacro %}
{%- macro to_java_instrument_builder_type(instrument) -%}
{%- if instrument == "counter" -%}
LongCounterBuilder
{%- elif instrument == "histogram" -%}
DoubleHistogramBuilder
{%- elif instrument == "updowncounter" -%}
LongUpDownCounterBuilder
{%- elif instrument == "gauge" -%}
DoubleGaugeBuilder
{%- endif -%}
{%- endmacro %}
{%- set filtered_metrics = metrics | select(filter) | list %}
{%- if filtered_metrics | count > 0 %}
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.api.semconv;

import io.opentelemetry.api.metrics.DoubleGaugeBuilder;
import io.opentelemetry.api.metrics.DoubleHistogramBuilder;
import io.opentelemetry.api.metrics.LongCounterBuilder;
import io.opentelemetry.api.metrics.LongUpDownCounterBuilder;
import io.opentelemetry.api.metrics.Meter;

class {{ root_namespace | to_camelcase(True) }}Metrics {
{%- for metric in filtered_metrics %}
/**
* {{metric.brief | to_doc_brief}}
*/
public static final {{ to_java_instrument_builder_type(metric.instrument) }} create{{metric.metric_name | to_camelcase(True)}}(Meter meter) {
return meter.{{to_java_instrument_builder_factory(metric.instrument)}}("{{ metric.metric_name }}")
.setDescription("{{ metric.brief }}")
.setUnit("{{ metric.unit }}");
}
{% endfor %}
}
{%endif%}
92 changes: 92 additions & 0 deletions src/main/java/io/opentelemetry/semconv/v1_23_1/DbMetrics.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.api.semconv;

import io.opentelemetry.api.metrics.DoubleHistogramBuilder;
import io.opentelemetry.api.metrics.LongCounterBuilder;
import io.opentelemetry.api.metrics.LongUpDownCounterBuilder;
import io.opentelemetry.api.metrics.Meter;

class DbMetrics {
/** The time it took to create a new connection */
public static final DoubleHistogramBuilder createDbClientConnectionsCreateTime(Meter meter) {
return meter
.histogramBuilder("db.client.connections.create_time")
.setDescription("The time it took to create a new connection")
.setUnit("ms");
}

/** The maximum number of idle open connections allowed */
public static final LongUpDownCounterBuilder createDbClientConnectionsIdleMax(Meter meter) {
return meter
.upDownCounterBuilder("db.client.connections.idle.max")
.setDescription("The maximum number of idle open connections allowed")
.setUnit("{connection}");
}

/** The minimum number of idle open connections allowed */
public static final LongUpDownCounterBuilder createDbClientConnectionsIdleMin(Meter meter) {
return meter
.upDownCounterBuilder("db.client.connections.idle.min")
.setDescription("The minimum number of idle open connections allowed")
.setUnit("{connection}");
}

/** The maximum number of open connections allowed */
public static final LongUpDownCounterBuilder createDbClientConnectionsMax(Meter meter) {
return meter
.upDownCounterBuilder("db.client.connections.max")
.setDescription("The maximum number of open connections allowed")
.setUnit("{connection}");
}

/** The number of pending requests for an open connection, cumulative for the entire pool */
public static final LongUpDownCounterBuilder createDbClientConnectionsPendingRequests(
Meter meter) {
return meter
.upDownCounterBuilder("db.client.connections.pending_requests")
.setDescription(
"The number of pending requests for an open connection, cumulative for the entire pool")
.setUnit("{request}");
}

/**
* The number of connection timeouts that have occurred trying to obtain a connection from the
* pool
*/
public static final LongCounterBuilder createDbClientConnectionsTimeouts(Meter meter) {
return meter
.counterBuilder("db.client.connections.timeouts")
.setDescription(
"The number of connection timeouts that have occurred trying to obtain a connection from the pool")
.setUnit("{timeout}");
}

/** The number of connections that are currently in state described by the `state` attribute */
public static final LongUpDownCounterBuilder createDbClientConnectionsUsage(Meter meter) {
return meter
.upDownCounterBuilder("db.client.connections.usage")
.setDescription(
"The number of connections that are currently in state described by the `state` attribute")
.setUnit("{connection}");
}

/** The time between borrowing a connection and returning it to the pool */
public static final DoubleHistogramBuilder createDbClientConnectionsUseTime(Meter meter) {
return meter
.histogramBuilder("db.client.connections.use_time")
.setDescription("The time between borrowing a connection and returning it to the pool")
.setUnit("ms");
}

/** The time it took to obtain an open connection from the pool */
public static final DoubleHistogramBuilder createDbClientConnectionsWaitTime(Meter meter) {
return meter
.histogramBuilder("db.client.connections.wait_time")
.setDescription("The time it took to obtain an open connection from the pool")
.setUnit("ms");
}
}
85 changes: 85 additions & 0 deletions src/main/java/io/opentelemetry/semconv/v1_23_1/FaasMetrics.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.api.semconv;

import io.opentelemetry.api.metrics.DoubleHistogramBuilder;
import io.opentelemetry.api.metrics.LongCounterBuilder;
import io.opentelemetry.api.metrics.Meter;

class FaasMetrics {
/** Number of invocation cold starts */
public static final LongCounterBuilder createFaasColdstarts(Meter meter) {
return meter
.counterBuilder("faas.coldstarts")
.setDescription("Number of invocation cold starts")
.setUnit("{coldstart}");
}

/** Distribution of CPU usage per invocation */
public static final DoubleHistogramBuilder createFaasCpuUsage(Meter meter) {
return meter
.histogramBuilder("faas.cpu_usage")
.setDescription("Distribution of CPU usage per invocation")
.setUnit("s");
}

/** Number of invocation errors */
public static final LongCounterBuilder createFaasErrors(Meter meter) {
return meter
.counterBuilder("faas.errors")
.setDescription("Number of invocation errors")
.setUnit("{error}");
}

/** Measures the duration of the function's initialization, such as a cold start */
public static final DoubleHistogramBuilder createFaasInitDuration(Meter meter) {
return meter
.histogramBuilder("faas.init_duration")
.setDescription(
"Measures the duration of the function's initialization, such as a cold start")
.setUnit("s");
}

/** Number of successful invocations */
public static final LongCounterBuilder createFaasInvocations(Meter meter) {
return meter
.counterBuilder("faas.invocations")
.setDescription("Number of successful invocations")
.setUnit("{invocation}");
}

/** Measures the duration of the function's logic execution */
public static final DoubleHistogramBuilder createFaasInvokeDuration(Meter meter) {
return meter
.histogramBuilder("faas.invoke_duration")
.setDescription("Measures the duration of the function's logic execution")
.setUnit("s");
}

/** Distribution of max memory usage per invocation */
public static final DoubleHistogramBuilder createFaasMemUsage(Meter meter) {
return meter
.histogramBuilder("faas.mem_usage")
.setDescription("Distribution of max memory usage per invocation")
.setUnit("By");
}

/** Distribution of net I/O usage per invocation */
public static final DoubleHistogramBuilder createFaasNetIo(Meter meter) {
return meter
.histogramBuilder("faas.net_io")
.setDescription("Distribution of net I/O usage per invocation")
.setUnit("By");
}

/** Number of invocation timeouts */
public static final LongCounterBuilder createFaasTimeouts(Meter meter) {
return meter
.counterBuilder("faas.timeouts")
.setDescription("Number of invocation timeouts")
.setUnit("{timeout}");
}
}
Loading

0 comments on commit de59aed

Please sign in to comment.