-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
APM-AWS Entity Relationships: Kafka (#2636)
- Loading branch information
1 parent
dda9380
commit 7467c64
Showing
10 changed files
with
183 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/Agent/NewRelic/Agent/Extensions/Providers/Wrapper/Kafka/KafkaBuilderWrapper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright 2020 New Relic, Inc. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
using System; | ||
using System.Collections; | ||
using NewRelic.Agent.Api; | ||
using NewRelic.Agent.Extensions.Providers.Wrapper; | ||
using NewRelic.Reflection; | ||
|
||
namespace NewRelic.Providers.Wrapper.Kafka | ||
{ | ||
public class KafkaBuilderWrapper : IWrapper | ||
{ | ||
private Func<object, IEnumerable> _producerBuilderConfigGetter; | ||
private Func<object, IEnumerable> _consumerBuilderConfigGetter; | ||
|
||
private const string WrapperName = "KafkaBuilderWrapper"; | ||
private const string BootstrapServersKey = "bootstrap.servers"; | ||
|
||
public bool IsTransactionRequired => false; | ||
public CanWrapResponse CanWrap(InstrumentedMethodInfo instrumentedMethodInfo) | ||
{ | ||
return new CanWrapResponse(WrapperName.Equals(instrumentedMethodInfo.RequestedWrapperName)); | ||
} | ||
|
||
public AfterWrappedMethodDelegate BeforeWrappedMethod(InstrumentedMethodCall instrumentedMethodCall, IAgent agent, ITransaction transaction) | ||
{ | ||
var builder = instrumentedMethodCall.MethodCall.InvocationTarget; | ||
|
||
dynamic configuration = null; | ||
|
||
if (builder.GetType().Name == "ProducerBuilder`2") | ||
{ | ||
var configGetter = _producerBuilderConfigGetter ??= VisibilityBypasser.Instance.GeneratePropertyAccessor<IEnumerable>(builder.GetType(), "Config"); | ||
configuration = configGetter(builder) as dynamic; | ||
} | ||
else if (builder.GetType().Name == "ConsumerBuilder`2") | ||
{ | ||
var configGetter = _consumerBuilderConfigGetter ??= VisibilityBypasser.Instance.GeneratePropertyAccessor<IEnumerable>(builder.GetType(), "Config"); | ||
configuration = configGetter(builder) as dynamic; | ||
} | ||
|
||
if (configuration == null) | ||
return Delegates.NoOp; | ||
|
||
string bootstrapServers = null; | ||
|
||
foreach (var kvp in configuration) | ||
{ | ||
if (kvp.Key == BootstrapServersKey) | ||
{ | ||
bootstrapServers = kvp.Value as string; | ||
break; | ||
} | ||
} | ||
|
||
if (!string.IsNullOrEmpty(bootstrapServers)) | ||
return Delegates.GetDelegateFor<object>(onSuccess: (producerOrConsumerAsObject) => | ||
{ | ||
KafkaHelper.AddBootstrapServersToCache(producerOrConsumerAsObject, bootstrapServers); | ||
}); | ||
|
||
return Delegates.NoOp; | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/Agent/NewRelic/Agent/Extensions/Providers/Wrapper/Kafka/KafkaHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright 2020 New Relic, Inc. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using NewRelic.Agent.Api; | ||
|
||
namespace NewRelic.Providers.Wrapper.Kafka | ||
{ | ||
internal static class KafkaHelper | ||
{ | ||
private static readonly ConcurrentDictionary<object, List<string>> _bootstrapServerCache = new(); | ||
|
||
public static void AddBootstrapServersToCache(object producerOrConsumerInstance, string bootStrapServers) | ||
{ | ||
if (string.IsNullOrEmpty(bootStrapServers)) | ||
return; | ||
var kafkaBootstrapServers = new List<string>(); | ||
|
||
// parse bootStrapServers - it's a comma separated list of host:port pairs | ||
var servers = bootStrapServers.Split(','); | ||
foreach (var server in servers) | ||
{ | ||
kafkaBootstrapServers.Add(server); | ||
} | ||
|
||
_bootstrapServerCache[producerOrConsumerInstance] = kafkaBootstrapServers; | ||
} | ||
|
||
public static bool TryGetBootstrapServersFromCache(object producerOrConsumerInstance, out List<string> kafkaBootstrapServers) | ||
{ | ||
return _bootstrapServerCache.TryGetValue(producerOrConsumerInstance, out kafkaBootstrapServers); | ||
} | ||
|
||
public static void RecordKafkaNodeMetrics(IAgent agent, string topicName, List<string> bootstrapServers, bool isProducer) | ||
{ | ||
foreach (var server in bootstrapServers) | ||
{ | ||
var mode = (isProducer? "Produce" : "Consume"); | ||
|
||
agent.RecordCountMetric($"MessageBroker/Kafka/Nodes/{server}"); | ||
agent.RecordCountMetric($"MessageBroker/Kafka/Nodes/{server}/{mode}/{topicName}"); | ||
} | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters