|
| 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) 2022 ScyllaDB |
| 19 | + * |
| 20 | + * Modified by ScyllaDB |
| 21 | + */ |
| 22 | +package com.datastax.driver.examples.opentelemetry; |
| 23 | + |
| 24 | +import com.datastax.driver.core.BatchStatement; |
| 25 | +import com.datastax.driver.core.Cluster; |
| 26 | +import com.datastax.driver.core.Session; |
| 27 | +import com.datastax.driver.core.SimpleStatement; |
| 28 | +import com.datastax.driver.core.tracing.TracingInfoFactory; |
| 29 | +import com.datastax.driver.opentelemetry.OpenTelemetryTracingInfoFactory; |
| 30 | +import io.opentelemetry.api.OpenTelemetry; |
| 31 | +import io.opentelemetry.api.trace.Span; |
| 32 | +import io.opentelemetry.api.trace.Tracer; |
| 33 | +import io.opentelemetry.context.Scope; |
| 34 | + |
| 35 | +/** |
| 36 | + * Creates a keyspace and tables, and loads some data into them. Sends OpenTelemetry tracing data to |
| 37 | + * Zipkin tracing backend |
| 38 | + * |
| 39 | + * <p>Preconditions: - a Scylla cluster is running and accessible through the contacts points |
| 40 | + * identified by CONTACT_POINTS and PORT and Zipkin backend is running and accessible through the |
| 41 | + * contacts points identified by ZIPKIN_CONTACT_POINT and ZIPKIN_PORT. |
| 42 | + * |
| 43 | + * <p>Side effects: - creates a new keyspace "simplex" in the cluster. If a keyspace with this name |
| 44 | + * already exists, it will be reused; - creates two tables "simplex.songs" and "simplex.playlists". |
| 45 | + * If they exist already, they will be reused; - inserts a row in each table. |
| 46 | + */ |
| 47 | +public class ReadFromCacheTest { |
| 48 | + private static final String CONTACT_POINT = "127.0.0.1"; |
| 49 | + private static final int PORT = 9042; |
| 50 | + |
| 51 | + private static final String ZIPKIN_CONTACT_POINT = "127.0.0.1"; |
| 52 | + private static final int ZIPKIN_PORT = 9411; |
| 53 | + |
| 54 | + private Cluster cluster; |
| 55 | + private Session session; |
| 56 | + |
| 57 | + private Tracer tracer; |
| 58 | + |
| 59 | + public static void main(String[] args) { |
| 60 | + // Workaround for setting ContextStorage to ThreadLocalContextStorage. |
| 61 | + System.setProperty("io.opentelemetry.context.contextStorageProvider", "default"); |
| 62 | + |
| 63 | + ReadFromCacheTest client = new ReadFromCacheTest(); |
| 64 | + |
| 65 | + client.connect(); |
| 66 | + |
| 67 | + try { |
| 68 | + client.prepare(); |
| 69 | + client.test(); |
| 70 | + System.out.println( |
| 71 | + "All requests have been completed. Now you can visit Zipkin at " |
| 72 | + + "http://" |
| 73 | + + ZIPKIN_CONTACT_POINT |
| 74 | + + ":" |
| 75 | + + ZIPKIN_PORT |
| 76 | + + " and examine the produced trace."); |
| 77 | + } finally { |
| 78 | + client.close(); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /** Initiates a connection to the cluster. */ |
| 83 | + public void connect() { |
| 84 | + cluster = Cluster.builder().addContactPoints(CONTACT_POINT).withPort(PORT).build(); |
| 85 | + |
| 86 | + System.out.println("Connected to cluster: " + cluster.getMetadata().getClusterName()); |
| 87 | + |
| 88 | + OpenTelemetry openTelemetry = |
| 89 | + OpenTelemetryConfiguration.initializeForZipkin(ZIPKIN_CONTACT_POINT, ZIPKIN_PORT); |
| 90 | + tracer = openTelemetry.getTracerProvider().get("this"); |
| 91 | + session = cluster.connect(); |
| 92 | + } |
| 93 | + |
| 94 | + /** Creates the schema (keyspace) and table for this example. */ |
| 95 | + public void prepare() { |
| 96 | + session.execute("DROP KEYSPACE IF EXISTS otel;"); |
| 97 | + session.execute( |
| 98 | + "CREATE KEYSPACE IF NOT EXISTS otel WITH " |
| 99 | + + "replication = {'class':'SimpleStrategy', 'replication_factor':2};"); |
| 100 | + session.execute("DROP TABLE IF EXISTS otel.test;"); |
| 101 | + session.execute("CREATE TABLE otel.test (id int, value int, PRIMARY KEY (id));"); |
| 102 | + BatchStatement batchStatement = new BatchStatement(); |
| 103 | + batchStatement.add(new SimpleStatement("INSERT INTO otel.test (id, value) VALUES (4, 2);")); |
| 104 | + batchStatement.add(new SimpleStatement("INSERT INTO otel.test (id, value) VALUES (2, 1);")); |
| 105 | + batchStatement.add(new SimpleStatement("INSERT INTO otel.test (id, value) VALUES (3, 7);")); |
| 106 | + session.execute(batchStatement); |
| 107 | + } |
| 108 | + |
| 109 | + /** Executes queries, which are testing cache usage. */ |
| 110 | + public void test() { |
| 111 | + Span parentSpan = tracer.spanBuilder("test").startSpan(); |
| 112 | + try (Scope parentScope = parentSpan.makeCurrent()) { |
| 113 | + session.close(); |
| 114 | + TracingInfoFactory tracingInfoFactory = new OpenTelemetryTracingInfoFactory(tracer); |
| 115 | + cluster.setTracingInfoFactory(tracingInfoFactory); |
| 116 | + session = cluster.connect(); |
| 117 | + |
| 118 | + session.execute("SELECT * FROM otel.test;"); |
| 119 | + session.execute("SELECT * FROM otel.test WHERE id = 4;"); |
| 120 | + session.execute("SELECT * FROM otel.test BYPASS CACHE;"); |
| 121 | + session.execute("SELECT * FROM otel.test WHERE id = 4 BYPASS CACHE;"); |
| 122 | + } finally { |
| 123 | + parentSpan.end(); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + /** Closes the session and the cluster. */ |
| 128 | + public void close() { |
| 129 | + session.close(); |
| 130 | + cluster.close(); |
| 131 | + } |
| 132 | +} |
0 commit comments