|
| 1 | +// Copyright 2020 New Relic, Inc. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +using System; |
| 5 | +using NewRelic.Agent.Api; |
| 6 | +using NewRelic.Agent.Extensions.Parsing; |
| 7 | +using NewRelic.Agent.Extensions.Providers.Wrapper; |
| 8 | +using NewRelic.Reflection; |
| 9 | + |
| 10 | +namespace NewRelic.Providers.Wrapper.Memcached |
| 11 | +{ |
| 12 | + public class MemcachedHelpers |
| 13 | + { |
| 14 | + private static bool _hasGetServerFailed = false; |
| 15 | + private const string AssemblyName = "EnyimMemcachedCore"; |
| 16 | + private static Func<object, object> _transformerGetter; |
| 17 | + private static Func<object, string, string> _transformMethod; |
| 18 | + private static Func<object, object> _poolGetter; |
| 19 | + private static Func<object, string, object> _locateMethod; |
| 20 | + private static Func<object, object> _endpointGetter; |
| 21 | + private static Func<object, object> _addressGetter; |
| 22 | + private static Func<object, int> _portGetter; |
| 23 | + |
| 24 | + // To get the ConnectionInfo we need to call the same Transform method that the library calls to get the node. |
| 25 | + // This is deterministic based reviewing the code at different versions. |
| 26 | + public static ConnectionInfo GetConnectionInfo(string key, object target, IAgent agent) |
| 27 | + { |
| 28 | + if (_hasGetServerFailed) |
| 29 | + { |
| 30 | + return new ConnectionInfo(DatastoreVendor.Memcached.ToKnownName(), null, -1, null); |
| 31 | + } |
| 32 | + |
| 33 | + try |
| 34 | + { |
| 35 | + var targetType = target.GetType(); |
| 36 | + _transformerGetter ??= VisibilityBypasser.Instance.GeneratePropertyAccessor<object>(targetType, "KeyTransformer"); |
| 37 | + var transformer = _transformerGetter(target); |
| 38 | + |
| 39 | + _transformMethod ??= VisibilityBypasser.Instance.GenerateOneParameterMethodCaller<string, string>(AssemblyName, transformer.GetType().FullName, "Transform"); |
| 40 | + var hashedKey = _transformMethod(transformer, key); |
| 41 | + |
| 42 | + _poolGetter ??= VisibilityBypasser.Instance.GeneratePropertyAccessor<object>(targetType, "Pool"); |
| 43 | + var pool = _poolGetter(target); |
| 44 | + |
| 45 | + _locateMethod ??= VisibilityBypasser.Instance.GenerateOneParameterMethodCaller<string, object>(AssemblyName, pool.GetType().FullName, "Enyim.Caching.Memcached.IServerPool.Locate"); |
| 46 | + var node = _locateMethod(pool, hashedKey); |
| 47 | + |
| 48 | + _endpointGetter ??= VisibilityBypasser.Instance.GeneratePropertyAccessor<object>(node.GetType(), "EndPoint"); |
| 49 | + var endpoint = _endpointGetter(node); |
| 50 | + |
| 51 | + var endpointType = endpoint.GetType(); |
| 52 | + if (endpointType.Name == "DnsEndPoint") // v2.X |
| 53 | + { |
| 54 | + _addressGetter ??= VisibilityBypasser.Instance.GeneratePropertyAccessor<object>(endpointType, "Host"); |
| 55 | + } |
| 56 | + else if (endpointType.Name == "IPEndPoint") // v3.X |
| 57 | + { |
| 58 | + _addressGetter ??= VisibilityBypasser.Instance.GeneratePropertyAccessor<object>(endpointType, "Address"); |
| 59 | + } |
| 60 | + else |
| 61 | + { |
| 62 | + throw new Exception("EndPoint type, "+ endpointType.Name + ", did not match supported types."); |
| 63 | + } |
| 64 | + |
| 65 | + var address = _addressGetter(endpoint).ToString(); |
| 66 | + |
| 67 | + _portGetter ??= VisibilityBypasser.Instance.GeneratePropertyAccessor<int>(endpointType, "Port"); |
| 68 | + int? port = _portGetter(endpoint); |
| 69 | + |
| 70 | + return new ConnectionInfo(DatastoreVendor.Memcached.ToKnownName(), address, port.HasValue ? port.Value : -1, null); |
| 71 | + } |
| 72 | + catch (Exception exception) |
| 73 | + { |
| 74 | + agent.Logger.Warn(exception, "Unable to get Memcached server address/port, likely to due to type differences. Server address/port will not be available."); |
| 75 | + _hasGetServerFailed = true; |
| 76 | + return new ConnectionInfo(DatastoreVendor.Memcached.ToKnownName(), null, -1, null); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments