Skip to content

Commit 4a943c8

Browse files
tylerbensontrask
authored andcommitted
Use Spotless for formatting (DataDog/dd-trace-java#1619)
1 parent b558c50 commit 4a943c8

File tree

26 files changed

+356
-201
lines changed

26 files changed

+356
-201
lines changed

.editorconfig

+2-4
Original file line numberDiff line numberDiff line change
@@ -663,10 +663,8 @@ ij_groovy_while_brace_force = never
663663
ij_groovy_while_on_new_line = false
664664
ij_groovy_wrap_long_lines = false
665665

666-
[{*.gradle.kts,*.kt,*.kts,*.main.kts}]
667-
indent_size = 4
668-
tab_width = 4
669-
ij_continuation_indent_size = 8
666+
[{*.kt,*.kts}]
667+
ij_continuation_indent_size = 2
670668
ij_kotlin_align_in_columns_case_branch = false
671669
ij_kotlin_align_multiline_binary_operation = false
672670
ij_kotlin_align_multiline_extends_list = false

.githooks/pre-commit

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
# http://redsymbol.net/articles/unofficial-bash-strict-mode/
3+
set -euo pipefail
4+
IFS=$'\n\t'
5+
6+
if ! ./gradlew spotlessCheck; then
7+
./gradlew spotlessApply
8+
echo ""
9+
echo ""
10+
echo -e "\033[0;33mCode has been formatted; please git diff/add and recommit."
11+
echo ""
12+
echo ""
13+
exit 1
14+
fi

CONTRIBUTING.md

+20-17
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ Gradle plugin.
8383
#### Snapshot builds
8484

8585
For developers testing code changes before a release is complete, there are
86-
snapshot builds of the `master` branch. They are available from
87-
[JFrog OSS repository](https://oss.jfrog.org/artifactory/oss-snapshot-local/io/opentelemetry/auto/)
86+
snapshot builds of the `master` branch. They are available from
87+
[JFrog OSS repository](https://oss.jfrog.org/artifactory/oss-snapshot-local/io/opentelemetry/auto/)
8888

8989
#### Building from source
9090

@@ -145,31 +145,34 @@ We follow the [Google Java Style
145145
Guide](https://google.github.io/styleguide/javaguide.html). Our build will
146146
fail if source code is not formatted according to that style.
147147

148-
To verify code style manually run the following command, which uses
149-
[google-java-format](https://github.com/google/google-java-format) library:
148+
The main goal is to avoid extensive reformatting caused by different IDEs having different opinion
149+
about how things should be formatted by establishing.
150150

151-
`./gradlew verifyGoogleJavaFormat`
151+
Running
152152

153-
or on Windows
153+
```bash
154+
./gradlew spotlessApply
155+
```
154156

155-
`gradlew.bat verifyGoogleJavaFormat`
157+
reformats all the files that need reformatting.
156158

157-
Instead of fixing style inconsistencies by hand, you can run gradle task
158-
`googleJavaFormat` to automatically fix all found issues:
159+
Running
159160

160-
`./gradlew googleJavaFormat`
161+
```bash
162+
./gradlew spotlessCheck
163+
```
161164

162-
or on Windows
163-
164-
`gradlew.bat googleJavaFormat`
165+
runs formatting verify task only.
165166

166167
#### Pre-commit hook
167168

168-
To completely delegate code style formatting to the machine, you can add [git
169-
pre-commit hook](https://git-scm.com/docs/githooks). We provide an example
170-
script in `buildscripts/pre-commit` file. Just copy or symlink it into
171-
`.git/hooks` folder.
169+
To completely delegate code style formatting to the machine,
170+
there is a pre-commit hook setup to verify formatting before committing.
171+
It can be activated with this command:
172172

173+
```bash
174+
git config core.hooksPath .githooks
175+
```
173176

174177
#### Editorconfig
175178

README.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
This project provides a Java agent JAR that can be attached to any Java 7+
1212
application and dynamically injects bytecode to capture telemetry from a
13-
number of popular libraries and frameworks.
14-
The telemetry data can be exported in a variety of formats.
13+
number of popular libraries and frameworks.
14+
The telemetry data can be exported in a variety of formats.
1515
In addition, the agent and exporter can be configured via command line arguments
1616
or environment variables. The net result is the ability to gather telemetry
1717
data from a Java application without code changes.
@@ -29,11 +29,11 @@ The instrumentation agent is enabled using the `-javaagent` flag to the JVM.
2929
java -javaagent:path/to/opentelemetry-auto-all.jar \
3030
-jar myapp.jar
3131
```
32-
By default OpenTelemetry Java agent uses
32+
By default OpenTelemetry Java agent uses
3333
[OTLP exporter](https://github.com/open-telemetry/opentelemetry-java/tree/master/exporters/otlp)
34-
configured to send data to
34+
configured to send data to
3535
[OpenTelemetry collector](https://github.com/open-telemetry/opentelemetry-collector/blob/master/receiver/otlpreceiver/README.md)
36-
at `localhost:55680`.
36+
at `localhost:55680`.
3737

3838
Configuration parameters are passed as Java system properties (`-D` flags) or
3939
as environment variables (see below for full list). For example:
@@ -67,7 +67,7 @@ A simple wrapper for the Zipkin exporter of opentelemetry-java. It POSTs json in
6767
|----------------------------------|----------------------------------|----------------------------------------------------------------------|
6868
| ota.exporter=zipkin | OTA_EXPORTER=zipkin | To select Zipkin exporter |
6969
| ota.exporter.zipkin.endpoint | OTA_EXPORTER_ZIPKIN_ENDPOINT | The Zipkin endpoint to connect to. Currently only HTTP is supported. |
70-
| ota.exporter.zipkin.service.name | OTA_EXPORTER_ZIPKIN_SERVICE_NAME | The service name of this JVM instance
70+
| ota.exporter.zipkin.service.name | OTA_EXPORTER_ZIPKIN_SERVICE_NAME | The service name of this JVM instance
7171

7272
#### OTLP exporter
7373

@@ -94,7 +94,7 @@ attributes to stdout. It is used mainly for testing and debugging.
9494
*This is highly advanced behavior and still in the prototyping phase. It may change drastically or be removed completely. Use
9595
with caution*
9696

97-
The OpenTelemetry API exposes SPI [hooks](https://github.com/open-telemetry/opentelemetry-java/blob/master/api/src/main/java/io/opentelemetry/trace/spi/TracerProviderFactory.java)
97+
The OpenTelemetry API exposes SPI [hooks](https://github.com/open-telemetry/opentelemetry-java/blob/master/api/src/main/java/io/opentelemetry/trace/spi/TracerProviderFactory.java)
9898
for customizing its behavior, such as the `Resource` attached to spans or the `Sampler`.
9999

100100
Because the auto instrumentation runs in a separate classpath than the instrumented application, it is not possible for customization in the application to take advantage of this customization. In order to provide such customization, you can
@@ -163,7 +163,7 @@ For this reason the following instrumentations are disabled by default:
163163
- `jdbc-datasource` which creates spans whenever `java.sql.DataSource#getConnection` method is called.
164164
- `servlet-filter` which creates spans around Servlet Filter methods.
165165
- `servlet-service` which creates spans around Servlet methods.
166-
166+
167167
To enable them, add `ota.integration.<name>.enabled` system property:
168168
`-Dota.integration.jdbc-datasource.enabled=true`
169169

RELEASING.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ to calculate the current version based on git tags. This plugin looks for the la
99

1010
## Snapshot builds
1111
Every successful CI build of the master branch automatically executes `./gradlew snapshot` as the last task.
12-
This signals Nebula plugin to build and publish to
12+
This signals Nebula plugin to build and publish to
1313
[JFrog OSS repository](https://oss.jfrog.org/artifactory/oss-snapshot-local/io/opentelemetry/auto/)next _minor_ release version.
1414
This means version `vX.(Y+1).0-SNAPSHOT`.
1515

@@ -23,12 +23,12 @@ Do the following:
2323
On new tag creation a CI will start a new release build.
2424
It will do the following:
2525
- Checkout requested tag.
26-
- Run `./gradlew -Prelease.useLastTag=true final`.
27-
This signals Nebula plugin to build `X.Y.0` version and to publish it to
26+
- Run `./gradlew -Prelease.useLastTag=true final`.
27+
This signals Nebula plugin to build `X.Y.0` version and to publish it to
2828
[Bintray repository](https://bintray.com/open-telemetry/maven/opentelemetry-java-instrumentation).
2929

3030
## Patch releases
3131
Whenever a fix is needed to any older branch, a PR should be made into the corresponding maintenance branch.
3232
When that PR is merge, CI will notice the new commit into maintenance branch and will initiate a new build for this.
3333
That build, after usual building and checking, will run `./gradlew -Prelease.scope=patch final`.
34-
This will signal Nebula plugin to build a new version `vX.Y.(Z+1)` and publish it to Bintray repo.
34+
This will signal Nebula plugin to build a new version `vX.Y.(Z+1)` and publish it to Bintray repo.

build.gradle

+17-4
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,11 @@ plugins {
88
id 'org.unbroken-dome.test-sets' version '2.2.1'
99
id 'com.github.ben-manes.versions' version '0.27.0'
1010

11-
// Not applying google java format by default because it gets confused by stray java build
12-
// files in 'workspace' build directory in CI
13-
id 'com.github.sherter.google-java-format' version '0.8' apply false
1411
id 'com.dorongold.task-tree' version '1.5'
1512

1613
id "com.github.johnrengelman.shadow" version "5.2.0"
1714

18-
id "com.diffplug.gradle.spotless" version "3.28.1"
15+
id "com.diffplug.gradle.spotless" version "4.3.0"
1916
id "com.github.spotbugs" version "4.0.1"
2017
}
2118

@@ -67,3 +64,19 @@ task writeMuzzleTasksToFile {
6764
.join('\n')
6865
}
6966
}
67+
68+
apply plugin: 'com.diffplug.gradle.spotless'
69+
70+
spotless {
71+
// this formatting is applied at the root level, as some of these files are not in a submodules
72+
// and would be missed otherwise
73+
format 'misc', {
74+
target '**/.gitignore', '**/*.md', '**/*.sh'
75+
indentWithSpaces()
76+
trimTrailingWhitespace()
77+
endWithNewline()
78+
}
79+
}
80+
81+
task formatCode(dependsOn: ['spotlessApply'])
82+
check.dependsOn 'spotlessCheck'

buildSrc/build.gradle.kts

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
plugins {
22
groovy
33
`java-gradle-plugin`
4+
id("com.diffplug.gradle.spotless") version "4.3.0"
45
}
56

67
gradlePlugin {

buildscripts/pre-commit

-3
This file was deleted.

gradle/checkstyle.gradle

-12
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,3 @@ check.dependsOn checkstyleTasks
3434
tasks.withType(Test).configureEach {
3535
mustRunAfter checkstyleTasks
3636
}
37-
38-
// Verification seems broken on Java 9.
39-
apply plugin: 'com.github.sherter.google-java-format'
40-
41-
googleJavaFormat {
42-
source = sourceSets*.allJava
43-
exclude '**/build/**/*.java'
44-
}
45-
46-
tasks.withType(Checkstyle).configureEach {
47-
mustRunAfter verifyGoogleJavaFormat
48-
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Disable formatting errors
2+
ignoreFormatterProblems=true
3+
org.eclipse.jdt.core.formatter.tabulation.char=space
4+
org.eclipse.jdt.core.formatter.tabulation.size=2
5+
org.eclipse.jdt.core.formatter.indentation.size=1
6+
org.eclipse.jdt.core.formatter.indentation.text_block_indentation=indent by one
7+
org.eclipse.jdt.core.formatter.indent_empty_lines=false
8+
org.eclipse.jdt.core.formatter.continuation_indentation=1
9+
org.eclipse.jdt.core.formatter.continuation_indentation_for_array_initializer=1
10+
groovy.formatter.longListLength=50
11+
groovy.formatter.multiline.indentation=1
12+
groovy.formatter.remove.unnecessary.semicolons=true

gradle/spotless.gradle

+7
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,23 @@ apply plugin: 'com.diffplug.gradle.spotless'
22

33
spotless {
44
java {
5+
googleJavaFormat()
56
licenseHeaderFile rootProject.file('gradle/enforcement/spotless.license.java'), '(package|import|public)'
67
target 'src/**/*.java'
78
}
89
groovy {
910
licenseHeaderFile rootProject.file('gradle/enforcement/spotless.license.java'), '(package|import|class)'
1011
}
1112
scala {
13+
scalafmt()
1214
licenseHeaderFile rootProject.file('gradle/enforcement/spotless.license.java'), '(package|import|public)'
1315
}
1416
kotlin {
17+
// ktfmt() // only supports 4 space indentation
18+
ktlint().userData(['indent_size': '2', 'continuation_indent_size': '2'])
1519
licenseHeaderFile rootProject.file('gradle/enforcement/spotless.license.java'), '(package|import|public)'
1620
}
1721
}
22+
23+
task formatCode(dependsOn: ['spotlessApply'])
24+
check.dependsOn 'spotlessApply'

0 commit comments

Comments
 (0)