Skip to content

Commit

Permalink
Fix "expected label name, got \"BCLOSE\"" error
Browse files Browse the repository at this point in the history
Signed-off-by: wangkaish <[email protected]>
  • Loading branch information
wangkaish committed Jun 29, 2023
1 parent db2304d commit f647a36
Showing 1 changed file with 50 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package io.prometheus.client.exporter.common;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

Expand All @@ -22,7 +22,8 @@ public class TextFormat {
*
* @since 0.10.0
*/
public final static String CONTENT_TYPE_OPENMETRICS_100 = "application/openmetrics-text; version=1.0.0; charset=utf-8";
public final static String CONTENT_TYPE_OPENMETRICS_100 =
"application/openmetrics-text; version=1.0.0; charset=utf-8";

/**
* Return the content type that should be used for a given Accept HTTP header.
Expand All @@ -48,14 +49,15 @@ public static String chooseContentType(String acceptHeader) {
*
* @since 0.10.0
*/
public static void writeFormat(String contentType, Writer writer, Enumeration<Collector.MetricFamilySamples> mfs) throws IOException {
public static void writeFormat(
String contentType, Writer writer, Enumeration<Collector.MetricFamilySamples> mfs) throws IOException {
if (CONTENT_TYPE_004.equals(contentType)) {
write004(writer, mfs);
return;
write004(writer, mfs);
return;
}
if (CONTENT_TYPE_OPENMETRICS_100.equals(contentType)) {
writeOpenMetrics100(writer, mfs);
return;
writeOpenMetrics100(writer, mfs);
return;
}
throw new IllegalArgumentException("Unknown contentType " + contentType);
}
Expand All @@ -67,7 +69,7 @@ public static void write004(Writer writer, Enumeration<Collector.MetricFamilySam
Map<String, Collector.MetricFamilySamples> omFamilies = new TreeMap<String, Collector.MetricFamilySamples>();
/* See http://prometheus.io/docs/instrumenting/exposition_formats/
* for the output format specification. */
while(mfs.hasMoreElements()) {
while (mfs.hasMoreElements()) {
Collector.MetricFamilySamples metricFamilySamples = mfs.nextElement();
String name = metricFamilySamples.name;
writer.write("# HELP ");
Expand Down Expand Up @@ -97,33 +99,23 @@ public static void write004(Writer writer, Enumeration<Collector.MetricFamilySam
String createdName = name + "_created";
String gcountName = name + "_gcount";
String gsumName = name + "_gsum";
for (Collector.MetricFamilySamples.Sample sample: metricFamilySamples.samples) {
for (Collector.MetricFamilySamples.Sample sample : metricFamilySamples.samples) {
/* OpenMetrics specific sample, put in a gauge at the end. */
if (sample.name.equals(createdName)
|| sample.name.equals(gcountName)
|| sample.name.equals(gsumName)) {
|| sample.name.equals(gcountName)
|| sample.name.equals(gsumName)) {
Collector.MetricFamilySamples omFamily = omFamilies.get(sample.name);
if (omFamily == null) {
omFamily = new Collector.MetricFamilySamples(sample.name, Collector.Type.GAUGE, metricFamilySamples.help, new ArrayList<Collector.MetricFamilySamples.Sample>());
omFamily = new Collector.MetricFamilySamples(sample.name,
Collector.Type.GAUGE, metricFamilySamples.help,
new ArrayList<Collector.MetricFamilySamples.Sample>());
omFamilies.put(sample.name, omFamily);
}
omFamily.samples.add(sample);
continue;
}
writer.write(sample.name);
if (sample.labelNames.size() > 0) {
writer.write('{');
for (int i = 0; i < sample.labelNames.size(); ++i) {
writer.write(sample.labelNames.get(i));
writer.write("=\"");
writeEscapedLabelValue(writer, sample.labelValues.get(i));
writer.write("\",");
}
writer.write('}');
}
writer.write(' ');
writer.write(Collector.doubleToGoString(sample.value));
if (sample.timestampMs != null){
appendSamples(writer, sample);
if (sample.timestampMs != null) {
writer.write(' ');
writer.write(sample.timestampMs.toString());
}
Expand All @@ -136,6 +128,30 @@ public static void write004(Writer writer, Enumeration<Collector.MetricFamilySam
}
}

private static void appendSamples(Writer writer, Collector.MetricFamilySamples.Sample sample) throws IOException {
writer.write(sample.name);
List<String> labelNames = sample.labelNames;
List<String> labelValues = sample.labelValues;
int sampleSize = labelNames.size();
if (sampleSize > 0) {
writer.write('{');
writer.write(labelNames.get(0));
writer.write("=\"");
writeEscapedLabelValue(writer, labelValues.get(0));
writer.write("\"");
for (int i = 1; i < sampleSize; ++i) {
writer.write(",");
writer.write(labelNames.get(i));
writer.write("=\"");
writeEscapedLabelValue(writer, labelValues.get(i));
writer.write("\"");
}
writer.write('}');
}
writer.write(' ');
writer.write(Collector.doubleToGoString(sample.value));
}

private static void writeEscapedHelp(Writer writer, String s) throws IOException {
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
Expand Down Expand Up @@ -197,8 +213,9 @@ private static String typeString(Collector.Type t) {
*
* @since 0.10.0
*/
public static void writeOpenMetrics100(Writer writer, Enumeration<Collector.MetricFamilySamples> mfs) throws IOException {
while(mfs.hasMoreElements()) {
public static void writeOpenMetrics100(
Writer writer, Enumeration<Collector.MetricFamilySamples> mfs) throws IOException {
while (mfs.hasMoreElements()) {
Collector.MetricFamilySamples metricFamilySamples = mfs.nextElement();
String name = metricFamilySamples.name;

Expand All @@ -221,31 +238,16 @@ public static void writeOpenMetrics100(Writer writer, Enumeration<Collector.Metr
writer.write(' ');
writeEscapedLabelValue(writer, metricFamilySamples.help);
writer.write('\n');

for (Collector.MetricFamilySamples.Sample sample: metricFamilySamples.samples) {
writer.write(sample.name);
if (sample.labelNames.size() > 0) {
writer.write('{');
for (int i = 0; i < sample.labelNames.size(); ++i) {
if (i > 0) {
writer.write(",");
}
writer.write(sample.labelNames.get(i));
writer.write("=\"");
writeEscapedLabelValue(writer, sample.labelValues.get(i));
writer.write("\"");
}
writer.write('}');
}
writer.write(' ');
writer.write(Collector.doubleToGoString(sample.value));
if (sample.timestampMs != null){

for (Collector.MetricFamilySamples.Sample sample : metricFamilySamples.samples) {
appendSamples(writer, sample);
if (sample.timestampMs != null) {
writer.write(' ');
omWriteTimestamp(writer, sample.timestampMs);
}
if (sample.exemplar != null) {
writer.write(" # {");
for (int i=0; i<sample.exemplar.getNumberOfLabels(); i++) {
for (int i = 0; i < sample.exemplar.getNumberOfLabels(); i++) {
if (i > 0) {
writer.write(",");
}
Expand Down

0 comments on commit f647a36

Please sign in to comment.