|
| 1 | +/* |
| 2 | + ** Copyright (c) 2026 Oracle and/or its affiliates. |
| 3 | + ** |
| 4 | + ** The Universal Permissive License (UPL), Version 1.0 |
| 5 | + ** |
| 6 | + ** Subject to the condition set forth below, permission is hereby granted to any |
| 7 | + ** person obtaining a copy of this software, associated documentation and/or data |
| 8 | + ** (collectively the "Software"), free of charge and under any and all copyright |
| 9 | + ** rights in the Software, and any and all patent rights owned or freely |
| 10 | + ** licensable by each licensor hereunder covering either (i) the unmodified |
| 11 | + ** Software as contributed to or provided by such licensor, or (ii) the Larger |
| 12 | + ** Works (as defined below), to deal in both |
| 13 | + ** |
| 14 | + ** (a) the Software, and |
| 15 | + ** (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if |
| 16 | + ** one is included with the Software (each a "Larger Work" to which the |
| 17 | + ** Software is contributed by such licensors), |
| 18 | + ** |
| 19 | + ** without restriction, including without limitation the rights to copy, create |
| 20 | + ** derivative works of, display, perform, and distribute the Software and make, |
| 21 | + ** use, sell, offer for sale, import, export, have made, and have sold the |
| 22 | + ** Software and the Larger Work(s), and to sublicense the foregoing rights on |
| 23 | + ** either these or other terms. |
| 24 | + ** |
| 25 | + ** This license is subject to the following condition: |
| 26 | + ** The above copyright notice and either this complete permission notice or at |
| 27 | + ** a minimum a reference to the UPL must be included in all copies or |
| 28 | + ** substantial portions of the Software. |
| 29 | + ** |
| 30 | + ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 31 | + ** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 32 | + ** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 33 | + ** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 34 | + ** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 35 | + ** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 36 | + ** SOFTWARE. |
| 37 | + */ |
| 38 | + |
| 39 | +package oracle.ucp.provider.observability; |
| 40 | + |
| 41 | +import oracle.ucp.events.core.UCPEventContext; |
| 42 | +import oracle.ucp.events.core.UCPEventListener; |
| 43 | +import oracle.ucp.events.core.UCPEventListenerProvider; |
| 44 | +import oracle.ucp.provider.observability.jfr.core.JFRUCPEventListenerProvider; |
| 45 | +import oracle.ucp.provider.observability.otel.OtelUCPEventListenerProvider; |
| 46 | + |
| 47 | +import java.util.Map; |
| 48 | +import java.util.Objects; |
| 49 | + |
| 50 | +/** |
| 51 | + * Composite UCP observability provider that publishes events to both JFR and |
| 52 | + * OpenTelemetry. |
| 53 | + * |
| 54 | + * <p>Use {@code ucp-observability-listener} when both backends should receive |
| 55 | + * the same UCP event stream. Use {@code jfr-ucp-listener} or |
| 56 | + * {@code otel-ucp-listener} when only one backend is needed. |
| 57 | + */ |
| 58 | +public final class ObservabilityUCPEventListenerProvider |
| 59 | + implements UCPEventListenerProvider { |
| 60 | + |
| 61 | + private static final String PROVIDER_NAME = "ucp-observability-listener"; |
| 62 | + |
| 63 | + private final UCPEventListenerProvider jfrProvider; |
| 64 | + private final UCPEventListenerProvider otelProvider; |
| 65 | + private volatile UCPEventListener listener; |
| 66 | + |
| 67 | + /** |
| 68 | + * Creates a provider that delegates to the built-in JFR and OpenTelemetry |
| 69 | + * UCP listeners. |
| 70 | + */ |
| 71 | + public ObservabilityUCPEventListenerProvider() { |
| 72 | + this( |
| 73 | + new JFRUCPEventListenerProvider(), |
| 74 | + new OtelUCPEventListenerProvider()); |
| 75 | + } |
| 76 | + |
| 77 | + ObservabilityUCPEventListenerProvider( |
| 78 | + UCPEventListenerProvider jfrProvider, |
| 79 | + UCPEventListenerProvider otelProvider) { |
| 80 | + this.jfrProvider = Objects.requireNonNull( |
| 81 | + jfrProvider, "jfrProvider cannot be null"); |
| 82 | + this.otelProvider = Objects.requireNonNull( |
| 83 | + otelProvider, "otelProvider cannot be null"); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public String getName() { |
| 88 | + return PROVIDER_NAME; |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public UCPEventListener getListener(Map<String, String> config) { |
| 93 | + if (listener == null) { |
| 94 | + synchronized (this) { |
| 95 | + if (listener == null) { |
| 96 | + listener = new CompositeUCPEventListener( |
| 97 | + jfrProvider.getListener(config), |
| 98 | + otelProvider.getListener(config)); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + return listener; |
| 103 | + } |
| 104 | + |
| 105 | + private static final class CompositeUCPEventListener |
| 106 | + implements UCPEventListener { |
| 107 | + |
| 108 | + private static final long serialVersionUID = 1L; |
| 109 | + |
| 110 | + private final UCPEventListener jfrListener; |
| 111 | + private final UCPEventListener otelListener; |
| 112 | + |
| 113 | + private CompositeUCPEventListener( |
| 114 | + UCPEventListener jfrListener, |
| 115 | + UCPEventListener otelListener) { |
| 116 | + this.jfrListener = Objects.requireNonNull( |
| 117 | + jfrListener, "jfrListener cannot be null"); |
| 118 | + this.otelListener = Objects.requireNonNull( |
| 119 | + otelListener, "otelListener cannot be null"); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public boolean isDesiredEvent(EventType eventType) { |
| 124 | + return jfrListener.isDesiredEvent(eventType) |
| 125 | + || otelListener.isDesiredEvent(eventType); |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public void onUCPEvent(EventType eventType, UCPEventContext ctx) { |
| 130 | + jfrListener.onUCPEvent(eventType, ctx); |
| 131 | + otelListener.onUCPEvent(eventType, ctx); |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments