-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
with this change we're doing our best to parse your sources to retrieve information about your global and local ObservationConvention implementations / interfaces. If we can we try to retrieve the information about the applicable Observation.Context type too.
- Loading branch information
1 parent
41f7a89
commit a04ddae
Showing
9 changed files
with
368 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
...enerator-commons/src/main/java/io/micrometer/docs/commons/ObservationConventionEntry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/* | ||
* Copyright 2013-2020 the original author or authors. | ||
* | ||
* Licensed 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 | ||
* | ||
* https://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 io.micrometer.docs.commons; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import io.micrometer.common.util.internal.logging.InternalLogger; | ||
import io.micrometer.common.util.internal.logging.InternalLoggerFactory; | ||
import io.micrometer.docs.commons.utils.StringUtils; | ||
|
||
public class ObservationConventionEntry implements Comparable<ObservationConventionEntry> { | ||
|
||
private static final InternalLogger logger = InternalLoggerFactory.getInstance(ObservationConventionEntry.class); | ||
|
||
private final String className; | ||
|
||
private final Type type; | ||
|
||
private final String contextClassName; | ||
|
||
public ObservationConventionEntry(String className, Type type, String contextClassName) { | ||
this.className = className; | ||
this.type = type; | ||
this.contextClassName = StringUtils.hasText(contextClassName) ? contextClassName : "Unable to resolve"; | ||
} | ||
|
||
public String getClassName() { | ||
return className; | ||
} | ||
|
||
public String getContextClassName() { | ||
return contextClassName; | ||
} | ||
|
||
@Override | ||
public int compareTo(ObservationConventionEntry o) { | ||
int compare = this.contextClassName.compareTo(o.contextClassName); | ||
if (compare != 0) { | ||
return compare; | ||
} | ||
compare = this.type.compareTo(o.type); | ||
if (compare != 0) { | ||
return compare; | ||
} | ||
if (this.className != null) { | ||
return this.className.compareTo(o.className); | ||
} | ||
return compare; | ||
} | ||
|
||
public enum Type { | ||
GLOBAL, LOCAL | ||
} | ||
|
||
public static void saveEntriesAsAdocTableInAFile(Collection<ObservationConventionEntry> entries, File outputFile) throws IOException { | ||
if (entries.isEmpty()) { | ||
logger.debug("No ObservationConventions found - will not output anything"); | ||
return; | ||
} | ||
|
||
StringBuilder builder = new StringBuilder(); | ||
List<ObservationConventionEntry> global = entries.stream().filter(e -> e.type == Type.GLOBAL).collect(Collectors.toList()); | ||
List<ObservationConventionEntry> local = entries.stream().filter(e -> e.type == Type.LOCAL).collect(Collectors.toList()); | ||
|
||
Path output = outputFile.toPath(); | ||
logger.debug("======================================"); | ||
logger.debug("Summary of sources analysis for conventions"); | ||
logger.debug("Found [" + entries.size() + "] conventions"); | ||
logger.debug( | ||
"Found [" + global.size() + "] GlobalObservationConvention implementations"); | ||
logger.debug( | ||
"Found [" + local.size() + "] ObservationConvention implementations"); | ||
|
||
builder.append("[[observability-conventions]]\n") | ||
.append("=== Observability - Conventions\n\n") | ||
.append("Below you can find a list of all `GlobalObservabilityConventions` and `ObservabilityConventions` declared by this project.") | ||
.append("\n\n"); | ||
|
||
if (!global.isEmpty()) { | ||
builder.append(".GlobalObservationConvention implementations\n") | ||
.append("|===\n") | ||
.append("|GlobalObservationConvention Class Name | Applicable ObservationContext Class Name\n") | ||
.append(global.stream().map(e -> "|`" + e.getClassName() + "`|`" + e.contextClassName + "`").collect(Collectors.joining("\n"))) | ||
.append("\n|==="); | ||
|
||
builder.append("\n\n"); | ||
} | ||
|
||
// TODO: Duplication | ||
if (!local.isEmpty()) { | ||
|
||
builder.append(".ObservationConvention implementations\n") | ||
.append("|===\n") | ||
.append("|ObservationConvention Class Name | Applicable ObservationContext Class Name\n") | ||
.append(local.stream().map(e -> "|`" + e.getClassName() + "`|`" + e.contextClassName + "`").collect(Collectors.joining("\n"))) | ||
.append("\n|==="); | ||
} | ||
|
||
Files.write(output, builder.toString().getBytes()); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...tor-commons/src/test/java/io/micrometer/docs/commons/ObservationConventionEntryTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright 2013-2020 the original author or authors. | ||
* | ||
* Licensed 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 | ||
* | ||
* https://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 io.micrometer.docs.commons; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.util.Arrays; | ||
|
||
import org.assertj.core.api.BDDAssertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class ObservationConventionEntryTests { | ||
File output = new File(".", "build/conventions"); | ||
|
||
@BeforeEach | ||
void setup() { | ||
output.mkdirs(); | ||
} | ||
|
||
//TODO: Write other test cases | ||
@Test | ||
void should_save_conventions_as_adoc_table() throws IOException { | ||
ObservationConventionEntry localEntry = new ObservationConventionEntry("foo.bar.LocalBaz", ObservationConventionEntry.Type.LOCAL, "Observation.Context"); | ||
ObservationConventionEntry globalEntry = new ObservationConventionEntry("foo.bar.GlobalBaz", ObservationConventionEntry.Type.GLOBAL, "Foo"); | ||
File file = new File(this.output, "_success.adoc"); | ||
|
||
ObservationConventionEntry.saveEntriesAsAdocTableInAFile(Arrays.asList(localEntry, globalEntry), file); | ||
|
||
BDDAssertions.then(new String(Files.readAllBytes(file.toPath()))) | ||
.contains("|`foo.bar.GlobalBaz`|`Foo`") | ||
.contains("|`foo.bar.LocalBaz`|`Observation.Context`"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.