|
| 1 | +/* |
| 2 | + * Copyright The Hypertrace 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 | + * 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 | +package org.hypertrace.agent.core; |
| 18 | + |
| 19 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; |
| 21 | +import com.google.common.annotations.VisibleForTesting; |
| 22 | +import com.google.protobuf.BoolValue; |
| 23 | +import com.google.protobuf.StringValue; |
| 24 | +import com.google.protobuf.util.JsonFormat; |
| 25 | +import java.io.File; |
| 26 | +import java.io.FileInputStream; |
| 27 | +import java.io.IOException; |
| 28 | +import java.io.InputStream; |
| 29 | +import org.hypertrace.agent.config.Config.AgentConfig; |
| 30 | +import org.hypertrace.agent.config.Config.DataCapture; |
| 31 | +import org.hypertrace.agent.config.Config.Message; |
| 32 | +import org.hypertrace.agent.config.Config.Reporting; |
| 33 | + |
| 34 | +/** {@link HypertraceConfig} loads a yaml config from file. */ |
| 35 | +public class HypertraceConfig { |
| 36 | + |
| 37 | + private HypertraceConfig() {} |
| 38 | + |
| 39 | + private static AgentConfig agentConfig; |
| 40 | + |
| 41 | + static final String DEFAULT_REPORTING_ADDRESS = "http://localhost:9411/api/v2/spans"; |
| 42 | + static final String DEFAULT_SERVICE_NAME = "default_service_name"; |
| 43 | + |
| 44 | + public static AgentConfig get() { |
| 45 | + if (agentConfig == null) { |
| 46 | + synchronized (HypertraceConfig.class) { |
| 47 | + if (agentConfig == null) { |
| 48 | + try { |
| 49 | + agentConfig = load(); |
| 50 | + } catch (IOException e) { |
| 51 | + throw new RuntimeException("Could not load config", e); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + return agentConfig; |
| 57 | + } |
| 58 | + |
| 59 | + /** Reset the config, use only in tests. */ |
| 60 | + public static void reset() { |
| 61 | + agentConfig = null; |
| 62 | + } |
| 63 | + |
| 64 | + private static AgentConfig load() throws IOException { |
| 65 | + String configFile = EnvironmentConfig.getProperty(EnvironmentConfig.CONFIG_FILE_PROPERTY); |
| 66 | + if (configFile == null) { |
| 67 | + return EnvironmentConfig.applyPropertiesAndEnvVars(applyDefaults(AgentConfig.newBuilder())) |
| 68 | + .build(); |
| 69 | + } |
| 70 | + return load(configFile); |
| 71 | + } |
| 72 | + |
| 73 | + @VisibleForTesting |
| 74 | + static AgentConfig load(String filename) throws IOException { |
| 75 | + File configFile = new File(filename); |
| 76 | + if (!configFile.exists() || configFile.isDirectory() || !configFile.canRead()) { |
| 77 | + throw new IllegalArgumentException( |
| 78 | + String.format("Config file %s either does not exist or cannot be read", configFile)); |
| 79 | + } |
| 80 | + |
| 81 | + InputStream fileInputStream = new FileInputStream(configFile); |
| 82 | + String json = convertYamlToJson(fileInputStream); |
| 83 | + |
| 84 | + AgentConfig.Builder configBuilder = AgentConfig.newBuilder(); |
| 85 | + JsonFormat.parser().ignoringUnknownFields().merge(json, configBuilder); |
| 86 | + |
| 87 | + return EnvironmentConfig.applyPropertiesAndEnvVars(applyDefaults(configBuilder)).build(); |
| 88 | + } |
| 89 | + |
| 90 | + private static AgentConfig.Builder applyDefaults(AgentConfig.Builder configBuilder) { |
| 91 | + if (configBuilder.getServiceName().isEmpty()) { |
| 92 | + configBuilder.setServiceName(DEFAULT_SERVICE_NAME); |
| 93 | + } |
| 94 | + |
| 95 | + Reporting.Builder reportingBuilder = |
| 96 | + applyReportingDefaults(configBuilder.getReporting().toBuilder()); |
| 97 | + configBuilder.setReporting(reportingBuilder); |
| 98 | + |
| 99 | + DataCapture.Builder dataCaptureBuilder = |
| 100 | + setDefaultsToDataCapture(configBuilder.getDataCapture().toBuilder()); |
| 101 | + configBuilder.setDataCapture(dataCaptureBuilder); |
| 102 | + return configBuilder; |
| 103 | + } |
| 104 | + |
| 105 | + private static Reporting.Builder applyReportingDefaults(Reporting.Builder builder) { |
| 106 | + if (!builder.hasAddress()) { |
| 107 | + builder.setAddress(StringValue.newBuilder().setValue(DEFAULT_REPORTING_ADDRESS).build()); |
| 108 | + } |
| 109 | + return builder; |
| 110 | + } |
| 111 | + |
| 112 | + private static DataCapture.Builder setDefaultsToDataCapture(DataCapture.Builder builder) { |
| 113 | + builder.setHttpHeaders(applyMessageDefaults(builder.getHttpHeaders().toBuilder())); |
| 114 | + builder.setHttpBody(applyMessageDefaults(builder.getHttpBody().toBuilder())); |
| 115 | + builder.setRpcMetadata(applyMessageDefaults(builder.getRpcMetadata().toBuilder())); |
| 116 | + builder.setRpcBody(applyMessageDefaults(builder.getRpcBody().toBuilder())); |
| 117 | + return builder; |
| 118 | + } |
| 119 | + |
| 120 | + private static Message.Builder applyMessageDefaults(Message.Builder builder) { |
| 121 | + if (!builder.hasRequest()) { |
| 122 | + builder.setRequest(BoolValue.newBuilder().setValue(true).build()); |
| 123 | + } |
| 124 | + if (!builder.hasResponse()) { |
| 125 | + builder.setResponse(BoolValue.newBuilder().setValue(true).build()); |
| 126 | + } |
| 127 | + return builder; |
| 128 | + } |
| 129 | + |
| 130 | + private static String convertYamlToJson(InputStream yaml) throws IOException { |
| 131 | + ObjectMapper yamlReader = new ObjectMapper(new YAMLFactory()); |
| 132 | + Object obj = yamlReader.readValue(yaml, Object.class); |
| 133 | + |
| 134 | + ObjectMapper jsonWriter = new ObjectMapper(); |
| 135 | + return jsonWriter.writeValueAsString(obj); |
| 136 | + } |
| 137 | +} |
0 commit comments