Skip to content

Commit c25c34f

Browse files
Add substitutions for DriverFactory#getOrCreateMetricsProvider. (#1183) (#1189)
This prevents an eager class loading of Micrometer when compiling on GraalVM native image and fixes #1182. Co-authored-by: Michael Simons <[email protected]>
1 parent 1ed07b8 commit c25c34f

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright (c) "Neo4j"
3+
* Neo4j Sweden AB [http://neo4j.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.neo4j.driver.internal.svm;
20+
21+
import com.oracle.svm.core.annotate.Substitute;
22+
import com.oracle.svm.core.annotate.TargetClass;
23+
24+
import org.neo4j.driver.Config;
25+
import org.neo4j.driver.MetricsAdapter;
26+
import org.neo4j.driver.internal.DriverFactory;
27+
import org.neo4j.driver.internal.metrics.DevNullMetricsProvider;
28+
import org.neo4j.driver.internal.metrics.InternalMetricsProvider;
29+
import org.neo4j.driver.internal.metrics.MetricsProvider;
30+
import org.neo4j.driver.internal.metrics.MicrometerMetricsProvider;
31+
import org.neo4j.driver.internal.util.Clock;
32+
33+
@TargetClass( DriverFactory.class )
34+
final class Target_org_neo4j_driver_internal_DriverFactory
35+
{
36+
37+
/**
38+
* Substitutes metrics adatper in such a way that it falls back to off when Micrometer is not available.
39+
*
40+
* @param config Drivers config
41+
* @param clock Clock to use
42+
* @return A metrics provider, never null
43+
*/
44+
@Substitute
45+
protected static MetricsProvider getOrCreateMetricsProvider( Config config, Clock clock )
46+
{
47+
MetricsAdapter metricsAdapter = config.metricsAdapter();
48+
if ( metricsAdapter == null )
49+
{
50+
metricsAdapter = config.isMetricsEnabled() ? MetricsAdapter.DEFAULT : MetricsAdapter.DEV_NULL;
51+
}
52+
switch ( metricsAdapter )
53+
{
54+
case DEV_NULL:
55+
return DevNullMetricsProvider.INSTANCE;
56+
case DEFAULT:
57+
return new InternalMetricsProvider( clock, config.logging() );
58+
case MICROMETER:
59+
try
60+
{
61+
@SuppressWarnings( "unused" ) Class<?> metricsClass = Class.forName( "io.micrometer.core.instrument.Metrics" );
62+
return MicrometerMetricsProvider.forGlobalRegistry();
63+
}
64+
catch ( ClassNotFoundException e )
65+
{
66+
return DevNullMetricsProvider.INSTANCE;
67+
}
68+
}
69+
throw new IllegalStateException( "Unknown or unsupported MetricsAdapter: " + metricsAdapter );
70+
}
71+
}
72+
73+
class MicrometerSubstitutions
74+
{
75+
}

0 commit comments

Comments
 (0)