Skip to content

Commit 2d6f248

Browse files
makingmhalbritter
authored andcommitted
Provide auto configuration for OpenTelemetry Logs
See gh-40961
1 parent 895fbd7 commit 2d6f248

File tree

12 files changed

+896
-0
lines changed

12 files changed

+896
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.autoconfigure.logs;
18+
19+
import io.opentelemetry.api.OpenTelemetry;
20+
import io.opentelemetry.sdk.logs.LogRecordProcessor;
21+
import io.opentelemetry.sdk.logs.SdkLoggerProvider;
22+
import io.opentelemetry.sdk.logs.SdkLoggerProviderBuilder;
23+
import io.opentelemetry.sdk.logs.export.BatchLogRecordProcessor;
24+
import io.opentelemetry.sdk.logs.export.LogRecordExporter;
25+
import io.opentelemetry.sdk.resources.Resource;
26+
27+
import org.springframework.beans.factory.ObjectProvider;
28+
import org.springframework.boot.autoconfigure.AutoConfiguration;
29+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
30+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
31+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
32+
import org.springframework.context.annotation.Bean;
33+
34+
/**
35+
* {@link EnableAutoConfiguration Auto-configuration} for OpenTelemetry Logs.
36+
*
37+
* @author Toshiaki Maki
38+
* @since 3.4.0
39+
*/
40+
@AutoConfiguration("openTelemetryLogsAutoConfiguration")
41+
@ConditionalOnClass({ SdkLoggerProvider.class, OpenTelemetry.class })
42+
public class OpenTelemetryAutoConfiguration {
43+
44+
@Bean
45+
@ConditionalOnMissingBean
46+
public BatchLogRecordProcessor batchLogRecordProcessor(ObjectProvider<LogRecordExporter> logRecordExporters) {
47+
return BatchLogRecordProcessor.builder(LogRecordExporter.composite(logRecordExporters.orderedStream().toList()))
48+
.build();
49+
}
50+
51+
@Bean
52+
@ConditionalOnMissingBean
53+
public SdkLoggerProvider otelSdkLoggerProvider(Resource resource,
54+
ObjectProvider<LogRecordProcessor> logRecordProcessors,
55+
ObjectProvider<SdkLoggerProviderBuilderCustomizer> customizers) {
56+
SdkLoggerProviderBuilder builder = SdkLoggerProvider.builder().setResource(resource);
57+
logRecordProcessors.orderedStream().forEach(builder::addLogRecordProcessor);
58+
customizers.orderedStream().forEach((customizer) -> customizer.customize(builder));
59+
return builder.build();
60+
}
61+
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.autoconfigure.logs;
18+
19+
import io.opentelemetry.sdk.logs.SdkLoggerProvider;
20+
import io.opentelemetry.sdk.logs.SdkLoggerProviderBuilder;
21+
22+
/**
23+
* Callback interface that can be used to customize the {@link SdkLoggerProviderBuilder}
24+
* that is used to create the auto-configured {@link SdkLoggerProvider}.
25+
*
26+
* @author Toshiaki Maki
27+
* @since 3.4.0
28+
*/
29+
@FunctionalInterface
30+
public interface SdkLoggerProviderBuilderCustomizer {
31+
32+
/**
33+
* Customize the given {@code builder}.
34+
* @param builder the builder to customize
35+
*/
36+
void customize(SdkLoggerProviderBuilder builder);
37+
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.autoconfigure.logs.otlp;
18+
19+
import io.opentelemetry.api.OpenTelemetry;
20+
import io.opentelemetry.exporter.otlp.http.logs.OtlpHttpLogRecordExporter;
21+
import io.opentelemetry.sdk.logs.SdkLoggerProvider;
22+
23+
import org.springframework.boot.autoconfigure.AutoConfiguration;
24+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
25+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
26+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
27+
import org.springframework.context.annotation.Import;
28+
29+
/**
30+
* {@link EnableAutoConfiguration Auto-configuration} for OTLP Logs.
31+
*
32+
* @author Toshiaki Maki
33+
* @since 3.4.0
34+
*/
35+
@AutoConfiguration
36+
@ConditionalOnClass({ SdkLoggerProvider.class, OpenTelemetry.class, OtlpHttpLogRecordExporter.class })
37+
@EnableConfigurationProperties(OtlpProperties.class)
38+
@Import({ OtlpLogsConfigurations.ConnectionDetails.class, OtlpLogsConfigurations.Exporters.class })
39+
public class OtlpLogsAutoConfiguration {
40+
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.autoconfigure.logs.otlp;
18+
19+
import java.util.Locale;
20+
21+
import io.opentelemetry.exporter.otlp.http.logs.OtlpHttpLogRecordExporter;
22+
import io.opentelemetry.exporter.otlp.http.logs.OtlpHttpLogRecordExporterBuilder;
23+
24+
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
25+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
26+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
27+
import org.springframework.context.annotation.Bean;
28+
import org.springframework.context.annotation.Configuration;
29+
30+
/**
31+
* Configurations imported by {@link OtlpLogsAutoConfiguration}.
32+
*
33+
* @author Toshiaki Maki
34+
* @since 3.4.0
35+
*/
36+
public class OtlpLogsConfigurations {
37+
38+
@Configuration(proxyBeanMethods = false)
39+
static class ConnectionDetails {
40+
41+
@Bean
42+
@ConditionalOnMissingBean
43+
@ConditionalOnProperty(prefix = "management.otlp.logs", name = "endpoint")
44+
OtlpLogsConnectionDetails otlpLogsConnectionDetails(OtlpProperties properties) {
45+
return new PropertiesOtlpLogsConnectionDetails(properties);
46+
}
47+
48+
/**
49+
* Adapts {@link OtlpProperties} to {@link OtlpLogsConnectionDetails}.
50+
*/
51+
static class PropertiesOtlpLogsConnectionDetails implements OtlpLogsConnectionDetails {
52+
53+
private final OtlpProperties properties;
54+
55+
PropertiesOtlpLogsConnectionDetails(OtlpProperties properties) {
56+
this.properties = properties;
57+
}
58+
59+
@Override
60+
public String getUrl() {
61+
return this.properties.getEndpoint();
62+
}
63+
64+
}
65+
66+
}
67+
68+
@Configuration(proxyBeanMethods = false)
69+
static class Exporters {
70+
71+
@ConditionalOnMissingBean(value = OtlpHttpLogRecordExporter.class,
72+
type = "io.opentelemetry.exporter.otlp.logs.OtlpGrpcLogRecordExporter")
73+
@ConditionalOnBean(OtlpLogsConnectionDetails.class)
74+
@Bean
75+
OtlpHttpLogRecordExporter otlpHttpLogRecordExporter(OtlpProperties properties,
76+
OtlpLogsConnectionDetails connectionDetails) {
77+
OtlpHttpLogRecordExporterBuilder builder = OtlpHttpLogRecordExporter.builder()
78+
.setEndpoint(connectionDetails.getUrl())
79+
.setCompression(properties.getCompression().name().toLowerCase(Locale.US))
80+
.setTimeout(properties.getTimeout());
81+
properties.getHeaders().forEach(builder::addHeader);
82+
return builder.build();
83+
}
84+
85+
}
86+
87+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.autoconfigure.logs.otlp;
18+
19+
import org.springframework.boot.autoconfigure.service.connection.ConnectionDetails;
20+
21+
/**
22+
* Details required to establish a connection to an OpenTelemetry logs service.
23+
*
24+
* @author Toshiaki Maki
25+
* @since 3.4.0
26+
*/
27+
public interface OtlpLogsConnectionDetails extends ConnectionDetails {
28+
29+
/**
30+
* Address to where logs will be published.
31+
* @return the address to where logs will be published
32+
*/
33+
String getUrl();
34+
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.autoconfigure.logs.otlp;
18+
19+
import java.time.Duration;
20+
import java.util.HashMap;
21+
import java.util.Map;
22+
23+
import org.springframework.boot.context.properties.ConfigurationProperties;
24+
25+
/**
26+
* Configuration properties for exporting logs using OTLP.
27+
*
28+
* @author Jonatan Ivanov
29+
* @since 3.4.0
30+
*/
31+
@ConfigurationProperties("management.otlp.logs")
32+
public class OtlpProperties {
33+
34+
/**
35+
* URL to the OTel collector's HTTP API.
36+
*/
37+
private String endpoint;
38+
39+
/**
40+
* Call timeout for the OTel Collector to process an exported batch of data. This
41+
* timeout spans the entire call: resolving DNS, connecting, writing the request body,
42+
* server processing, and reading the response body. If the call requires redirects or
43+
* retries all must complete within one timeout period.
44+
*/
45+
private Duration timeout = Duration.ofSeconds(10);
46+
47+
/**
48+
* Method used to compress the payload.
49+
*/
50+
private Compression compression = Compression.NONE;
51+
52+
/**
53+
* Custom HTTP headers you want to pass to the collector, for example auth headers.
54+
*/
55+
private Map<String, String> headers = new HashMap<>();
56+
57+
public String getEndpoint() {
58+
return this.endpoint;
59+
}
60+
61+
public void setEndpoint(String endpoint) {
62+
this.endpoint = endpoint;
63+
}
64+
65+
public Duration getTimeout() {
66+
return this.timeout;
67+
}
68+
69+
public void setTimeout(Duration timeout) {
70+
this.timeout = timeout;
71+
}
72+
73+
public Compression getCompression() {
74+
return this.compression;
75+
}
76+
77+
public void setCompression(Compression compression) {
78+
this.compression = compression;
79+
}
80+
81+
public Map<String, String> getHeaders() {
82+
return this.headers;
83+
}
84+
85+
public void setHeaders(Map<String, String> headers) {
86+
this.headers = headers;
87+
}
88+
89+
public enum Compression {
90+
91+
/**
92+
* Gzip compression.
93+
*/
94+
GZIP,
95+
96+
/**
97+
* No compression.
98+
*/
99+
NONE
100+
101+
}
102+
103+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* Auto-configuration for OpenTelemetry logs with OTLP.
19+
*/
20+
package org.springframework.boot.actuate.autoconfigure.logs.otlp;

0 commit comments

Comments
 (0)