|
| 1 | +/* |
| 2 | + * Copyright DataStax, Inc. |
| 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 | + * http://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 | + * Copyright (C) 2021 ScyllaDB |
| 19 | + * |
| 20 | + * Modified by ScyllaDB |
| 21 | + */ |
| 22 | +package com.datastax.driver.examples.opentelemetry; |
| 23 | + |
| 24 | +import com.datastax.driver.core.BoundStatement; |
| 25 | +import com.datastax.driver.core.Cluster; |
| 26 | +import com.datastax.driver.core.PreparedStatement; |
| 27 | +import com.datastax.driver.core.ResultSetFuture; |
| 28 | +import com.datastax.driver.core.Session; |
| 29 | +import com.datastax.driver.core.tracing.TracingInfoFactory; |
| 30 | +import com.datastax.driver.opentelemetry.OpenTelemetryTracingInfoFactory; |
| 31 | +import io.opentelemetry.api.OpenTelemetry; |
| 32 | +import io.opentelemetry.api.trace.Span; |
| 33 | +import io.opentelemetry.api.trace.Tracer; |
| 34 | +import io.opentelemetry.context.Scope; |
| 35 | + |
| 36 | +/** |
| 37 | + * Creates a keyspace and tables, and loads some data into them. Sends OpenTelemetry tracing data to |
| 38 | + * Zipkin tracing backend |
| 39 | + * |
| 40 | + * <p>Preconditions: - a Scylla cluster is running and accessible through the contacts points |
| 41 | + * identified by CONTACT_POINTS and PORT and Zipkin backend is running and accessible through the |
| 42 | + * contacts points identified by ZIPKIN_CONTACT_POINT and ZIPKIN_PORT. |
| 43 | + * |
| 44 | + * <p>Side effects: - creates a new keyspace "simplex" in the cluster. If a keyspace with this name |
| 45 | + * already exists, it will be reused; - creates two tables "simplex.songs" and "simplex.playlists". |
| 46 | + * If they exist already, they will be reused; - inserts a row in each table. |
| 47 | + */ |
| 48 | +public class ZipkinUsage { |
| 49 | + private static final String CONTACT_POINT = "127.0.0.1"; |
| 50 | + private static final int PORT = 9042; |
| 51 | + |
| 52 | + private static final String ZIPKIN_CONTACT_POINT = "127.0.0.1"; |
| 53 | + private static final int ZIPKIN_PORT = 9411; |
| 54 | + |
| 55 | + private Cluster cluster; |
| 56 | + private Session session; |
| 57 | + |
| 58 | + private Tracer tracer; |
| 59 | + |
| 60 | + public static void main(String[] args) { |
| 61 | + // Workaround for setting ContextStorage to ThreadLocalContextStorage. |
| 62 | + System.setProperty("io.opentelemetry.context.contextStorageProvider", "default"); |
| 63 | + |
| 64 | + ZipkinUsage client = new ZipkinUsage(); |
| 65 | + |
| 66 | + try { |
| 67 | + client.connect(); |
| 68 | + client.createSchema(); |
| 69 | + client.loadData(); |
| 70 | + client.querySchema(); |
| 71 | + System.out.println( |
| 72 | + "All requests have been completed. Now you can visit Zipkin at " |
| 73 | + + "http://" |
| 74 | + + ZIPKIN_CONTACT_POINT |
| 75 | + + ":" |
| 76 | + + ZIPKIN_PORT |
| 77 | + + " and examine the produced trace."); |
| 78 | + } finally { |
| 79 | + client.close(); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + /** Initiates a connection to the cluster. */ |
| 84 | + public void connect() { |
| 85 | + cluster = Cluster.builder().addContactPoints(CONTACT_POINT).withPort(PORT).build(); |
| 86 | + |
| 87 | + System.out.printf("Connected to cluster: %s%n", cluster.getMetadata().getClusterName()); |
| 88 | + |
| 89 | + OpenTelemetry openTelemetry = |
| 90 | + OpenTelemetryConfiguration.initializeForZipkin(ZIPKIN_CONTACT_POINT, ZIPKIN_PORT); |
| 91 | + tracer = openTelemetry.getTracerProvider().get("this"); |
| 92 | + TracingInfoFactory tracingInfoFactory = new OpenTelemetryTracingInfoFactory(tracer); |
| 93 | + cluster.setTracingInfoFactory(tracingInfoFactory); |
| 94 | + |
| 95 | + session = cluster.connect(); |
| 96 | + } |
| 97 | + |
| 98 | + /** Creates the schema (keyspace) and tables for this example. */ |
| 99 | + public void createSchema() { |
| 100 | + session.execute("DROP KEYSPACE IF EXISTS simplex;"); |
| 101 | + Span parentSpan = tracer.spanBuilder("create schema").startSpan(); |
| 102 | + try (Scope parentScope = parentSpan.makeCurrent()) { |
| 103 | + { |
| 104 | + Span span = tracer.spanBuilder("create simplex").startSpan(); |
| 105 | + try (Scope scope = span.makeCurrent()) { |
| 106 | + session.execute( |
| 107 | + "CREATE KEYSPACE IF NOT EXISTS simplex WITH replication " |
| 108 | + + "= {'class':'SimpleStrategy', 'replication_factor':1};"); |
| 109 | + |
| 110 | + } finally { |
| 111 | + span.end(); |
| 112 | + } |
| 113 | + } |
| 114 | + { |
| 115 | + Span span = tracer.spanBuilder("create simplex.songs").startSpan(); |
| 116 | + try (Scope scope = span.makeCurrent()) { |
| 117 | + ResultSetFuture f = |
| 118 | + session.executeAsync( |
| 119 | + "CREATE TABLE IF NOT EXISTS simplex.songs (" |
| 120 | + + "id uuid," |
| 121 | + + "title text," |
| 122 | + + "album text," |
| 123 | + + "artist text," |
| 124 | + + "tags set<text>," |
| 125 | + + "data blob," |
| 126 | + + "PRIMARY KEY ((title, artist), album)" |
| 127 | + + ");"); |
| 128 | + f.getUninterruptibly(); |
| 129 | + } finally { |
| 130 | + span.end(); |
| 131 | + } |
| 132 | + } |
| 133 | + { |
| 134 | + Span span = tracer.spanBuilder("create simplex.playlists").startSpan(); |
| 135 | + try (Scope scope = span.makeCurrent()) { |
| 136 | + session.execute( |
| 137 | + "CREATE TABLE IF NOT EXISTS simplex.playlists (" |
| 138 | + + "id uuid," |
| 139 | + + "title text," |
| 140 | + + "album text, " |
| 141 | + + "artist text," |
| 142 | + + "song_id uuid," |
| 143 | + + "PRIMARY KEY (id, title, album, artist)" |
| 144 | + + ");"); |
| 145 | + |
| 146 | + } finally { |
| 147 | + span.end(); |
| 148 | + } |
| 149 | + } |
| 150 | + } finally { |
| 151 | + parentSpan.end(); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + /** Inserts data into the tables. */ |
| 156 | + public void loadData() { |
| 157 | + Span parentSpan = tracer.spanBuilder("load data").startSpan(); |
| 158 | + try (Scope parentScope = parentSpan.makeCurrent()) { |
| 159 | + |
| 160 | + Span prepareSpan = tracer.spanBuilder("prepare").startSpan(); |
| 161 | + PreparedStatement ps; |
| 162 | + try (Scope prepareScope = prepareSpan.makeCurrent()) { |
| 163 | + ps = |
| 164 | + session.prepare( |
| 165 | + "INSERT INTO simplex.songs (id, title, album, artist, tags) " |
| 166 | + + "VALUES (" |
| 167 | + + "756716f7-2e54-4715-9f00-91dcbea6cf50," |
| 168 | + + "?," |
| 169 | + + "?," |
| 170 | + + "?," |
| 171 | + + "{'jazz', '2013'})" |
| 172 | + + ";"); |
| 173 | + } finally { |
| 174 | + prepareSpan.end(); |
| 175 | + } |
| 176 | + |
| 177 | + Span bindSpan = tracer.spanBuilder("bind").startSpan(); |
| 178 | + BoundStatement bound; |
| 179 | + try (Scope bindScope = bindSpan.makeCurrent()) { |
| 180 | + bound = ps.bind("La Petite Tonkinoise", "Bye Bye Blackbird", "Joséphine Baker"); |
| 181 | + } finally { |
| 182 | + bindSpan.end(); |
| 183 | + } |
| 184 | + |
| 185 | + Span span = tracer.spanBuilder("insert simplex.songs").startSpan(); |
| 186 | + try (Scope scope = span.makeCurrent()) { |
| 187 | + session.execute(bound); |
| 188 | + } finally { |
| 189 | + span.end(); |
| 190 | + } |
| 191 | + |
| 192 | + } finally { |
| 193 | + parentSpan.end(); |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + public void querySchema() { |
| 198 | + Span parentSpan = tracer.spanBuilder("query schema").startSpan(); |
| 199 | + try (Scope parentScope = parentSpan.makeCurrent()) { |
| 200 | + |
| 201 | + Span prepareSpan = tracer.spanBuilder("prepare").startSpan(); |
| 202 | + PreparedStatement ps; |
| 203 | + try (Scope prepareScope = prepareSpan.makeCurrent()) { |
| 204 | + ps = session.prepare("SELECT * FROM simplex.songs WHERE artist = ? AND title = ?;"); |
| 205 | + } finally { |
| 206 | + prepareSpan.end(); |
| 207 | + } |
| 208 | + |
| 209 | + Span bindSpan = tracer.spanBuilder("bind").startSpan(); |
| 210 | + BoundStatement bound; |
| 211 | + try (Scope bindScope = bindSpan.makeCurrent()) { |
| 212 | + bound = ps.bind("Joséphine Baker", "La Petite Tonkinoise"); |
| 213 | + } finally { |
| 214 | + bindSpan.end(); |
| 215 | + } |
| 216 | + |
| 217 | + Span span = tracer.spanBuilder("query simplex.songs").startSpan(); |
| 218 | + try (Scope scope = span.makeCurrent()) { |
| 219 | + session.execute(bound); |
| 220 | + } finally { |
| 221 | + span.end(); |
| 222 | + } |
| 223 | + |
| 224 | + } finally { |
| 225 | + parentSpan.end(); |
| 226 | + } |
| 227 | + } |
| 228 | + |
| 229 | + /** Closes the session and the cluster. */ |
| 230 | + public void close() { |
| 231 | + if (session != null) session.close(); |
| 232 | + if (cluster != null) cluster.close(); |
| 233 | + } |
| 234 | +} |
0 commit comments