|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * The OpenSearch Contributors require contributions made to |
| 6 | + * this file be licensed under the Apache-2.0 license or a |
| 7 | + * compatible open source license. |
| 8 | + * |
| 9 | + */ |
| 10 | + |
| 11 | +package org.opensearch.dataprepper.plugins.otel.common; |
| 12 | + |
| 13 | +import static org.opensearch.dataprepper.plugins.otel.common.OTelSpanDerivationUtil.getStringAttribute; |
| 14 | + |
| 15 | +import java.util.Map; |
| 16 | + |
| 17 | +public class RemoteOperationAndServiceProviders { |
| 18 | + public final PeerServiceRemoteOperationServiceExtractor PeerServiceRemoteOperationServiceExtractor = new PeerServiceRemoteOperationServiceExtractor(); |
| 19 | + public final GraphQlRemoteOperationServiceExtractor GraphQlRemoteOperationServiceExtractor = new GraphQlRemoteOperationServiceExtractor(); |
| 20 | + public final MessagingSystemRemoteOperationServiceExtractor MessagingSystemRemoteOperationServiceExtractor = new MessagingSystemRemoteOperationServiceExtractor(); |
| 21 | + public final FaasRemoteOperationServiceExtractor FaasRemoteOperationServiceExtractor = new FaasRemoteOperationServiceExtractor(); |
| 22 | + public final DbRemoteOperationServiceExtractor DbRemoteOperationServiceExtractor = new DbRemoteOperationServiceExtractor(); |
| 23 | + public final DbQueryRemoteOperationServiceExtractor DbQueryRemoteOperationServiceExtractor = new DbQueryRemoteOperationServiceExtractor(); |
| 24 | + public final AwsRpcRemoteOperationServiceExtractor AwsRpcRemoteOperationServiceExtractor = new AwsRpcRemoteOperationServiceExtractor(); |
| 25 | + |
| 26 | + private static boolean appliesToSpan(Map<String, Object> spanAttributes, final String attribute1) { |
| 27 | + return getStringAttribute(spanAttributes, attribute1) != null; |
| 28 | + } |
| 29 | + |
| 30 | + private static boolean appliesToSpan(Map<String, Object> spanAttributes, final String attribute1, final String attribute2) { |
| 31 | + return getStringAttribute(spanAttributes, attribute1) != null || getStringAttribute(spanAttributes, attribute2) != null; |
| 32 | + } |
| 33 | + |
| 34 | + private static boolean appliesToSpan(Map<String, Object> spanAttributes, final String attribute1, final String attribute2, final String attribute3) { |
| 35 | + return getStringAttribute(spanAttributes, attribute1) != null || getStringAttribute(spanAttributes, attribute2) != null || getStringAttribute(spanAttributes, attribute3) != null; |
| 36 | + } |
| 37 | + |
| 38 | + public static class PeerServiceRemoteOperationServiceExtractor implements RemoteOperationServiceExtractor { |
| 39 | + public boolean appliesToSpan(Map<String, Object> spanAttributes) { |
| 40 | + return RemoteOperationAndServiceProviders.appliesToSpan(spanAttributes, "peer.service"); |
| 41 | + } |
| 42 | + |
| 43 | + public RemoteOperationAndService getRemoteOperationAndService(Map<String, Object> spanAttributes, Object optionalArg) { |
| 44 | + return new RemoteOperationAndService(null, getStringAttribute(spanAttributes, "peer.service")); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + public static class GraphQlRemoteOperationServiceExtractor implements RemoteOperationServiceExtractor { |
| 49 | + public boolean appliesToSpan(Map<String, Object> spanAttributes) { |
| 50 | + return RemoteOperationAndServiceProviders.appliesToSpan(spanAttributes, "graphql.operation.type"); |
| 51 | + } |
| 52 | + |
| 53 | + public RemoteOperationAndService getRemoteOperationAndService(Map<String, Object> spanAttributes, Object optionalArg) { |
| 54 | + final String graphQLOperation = getStringAttribute(spanAttributes, "graphql.operation.type"); |
| 55 | + if (graphQLOperation != null) { |
| 56 | + return new RemoteOperationAndService(graphQLOperation, "graphql"); |
| 57 | + } |
| 58 | + return new RemoteOperationAndService(null, null); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + public static class MessagingSystemRemoteOperationServiceExtractor implements RemoteOperationServiceExtractor { |
| 63 | + public boolean appliesToSpan(Map<String, Object> spanAttributes) { |
| 64 | + return RemoteOperationAndServiceProviders.appliesToSpan(spanAttributes, "messaging.system", "messaging.operation"); |
| 65 | + } |
| 66 | + |
| 67 | + public RemoteOperationAndService getRemoteOperationAndService(Map<String, Object> spanAttributes, Object optionalArg) { |
| 68 | + return new RemoteOperationAndService(getStringAttribute(spanAttributes, "messaging.operation"), getStringAttribute(spanAttributes, "messaging.system")); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + public static class FaasRemoteOperationServiceExtractor implements RemoteOperationServiceExtractor { |
| 73 | + |
| 74 | + public boolean appliesToSpan(Map<String, Object> spanAttributes) { |
| 75 | + return RemoteOperationAndServiceProviders.appliesToSpan(spanAttributes, "faas.invoked_name", "faas.trigger"); |
| 76 | + } |
| 77 | + |
| 78 | + public RemoteOperationAndService getRemoteOperationAndService(Map<String, Object> spanAttributes, Object optionalArg) { |
| 79 | + return new RemoteOperationAndService(getStringAttribute(spanAttributes, "faas.trigger"), getStringAttribute(spanAttributes, "faas.invoked_name")); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + public static class DbRemoteOperationServiceExtractor implements RemoteOperationServiceExtractor { |
| 84 | + public boolean appliesToSpan(Map<String, Object> spanAttributes) { |
| 85 | + return RemoteOperationAndServiceProviders.appliesToSpan(spanAttributes, "db.system", "db.operation", "db.statment"); |
| 86 | + } |
| 87 | + |
| 88 | + public RemoteOperationAndService getRemoteOperationAndService(Map<String, Object> spanAttributes, Object optionalArg) { |
| 89 | + String remoteOperation = getStringAttribute(spanAttributes, "db.operation"); |
| 90 | + if (remoteOperation == null) { |
| 91 | + remoteOperation = getStringAttribute(spanAttributes, "db.statement"); |
| 92 | + } |
| 93 | + if (remoteOperation != null) { |
| 94 | + remoteOperation = remoteOperation.trim().split("\\s+")[0].toUpperCase(); |
| 95 | + } |
| 96 | + return new RemoteOperationAndService(remoteOperation, |
| 97 | + getStringAttribute(spanAttributes, "db.system")); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + public static class DbQueryRemoteOperationServiceExtractor implements RemoteOperationServiceExtractor { |
| 102 | + public boolean appliesToSpan(Map<String, Object> spanAttributes) { |
| 103 | + return RemoteOperationAndServiceProviders.appliesToSpan(spanAttributes, "db.system.name", "db.operation.name", "db.query.text"); |
| 104 | + } |
| 105 | + |
| 106 | + public RemoteOperationAndService getRemoteOperationAndService(Map<String, Object> spanAttributes, Object optionalArg) { |
| 107 | + String remoteOperation = getStringAttribute(spanAttributes, "db.operation.name"); |
| 108 | + if (remoteOperation == null) { |
| 109 | + remoteOperation = getStringAttribute(spanAttributes, "db.query.text"); |
| 110 | + } |
| 111 | + if (remoteOperation != null) { |
| 112 | + remoteOperation = remoteOperation.trim().split("\\s+")[0].toUpperCase(); |
| 113 | + } |
| 114 | + return new RemoteOperationAndService(remoteOperation, getStringAttribute(spanAttributes, "db.system.name")); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + public static class AwsRpcRemoteOperationServiceExtractor implements RemoteOperationServiceExtractor { |
| 119 | + public boolean appliesToSpan(Map<String, Object> spanAttributes) { |
| 120 | + return RemoteOperationAndServiceProviders.appliesToSpan(spanAttributes, "rpc.service", "rpc.method"); |
| 121 | + } |
| 122 | + |
| 123 | + public RemoteOperationAndService getRemoteOperationAndService(Map<String, Object> spanAttributes, Object awsServiceMappings) { |
| 124 | + String remoteService = null; |
| 125 | + String rpcService = getStringAttribute(spanAttributes, "rpc.service"); |
| 126 | + String rpcSystem = getStringAttribute(spanAttributes, "rpc.system"); |
| 127 | + if (rpcSystem != null && rpcSystem.equals("aws-api")) { |
| 128 | + remoteService = (String)((Map<String, Object>)awsServiceMappings).getOrDefault(rpcService, "AWS::" + rpcService); |
| 129 | + } |
| 130 | + return new RemoteOperationAndService(getStringAttribute(spanAttributes, "rpc.method"), remoteService); |
| 131 | + } |
| 132 | + } |
| 133 | +} |
| 134 | + |
0 commit comments