Skip to content

Commit

Permalink
Merge branch '1.8' of https://github.com/alibaba/ilogtail into featur…
Browse files Browse the repository at this point in the history
…e/taiye/add_go_plugin_metric

Conflicts:
	core/plugin/PluginRegistry.cpp
	core/plugin/PluginRegistry.h
  • Loading branch information
linrunqi08 committed Jan 16, 2024
2 parents b12a775 + 31b3428 commit 20480fe
Show file tree
Hide file tree
Showing 24 changed files with 410 additions and 54 deletions.
2 changes: 1 addition & 1 deletion core/pipeline/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2022 iLogtail Authors
# Copyright 2023 iLogtail Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion core/plugin/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2022 iLogtail Authors
# Copyright 2023 iLogtail Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
66 changes: 50 additions & 16 deletions core/plugin/PluginRegistry.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 iLogtail Authors
* Copyright 2023 iLogtail Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -26,9 +26,17 @@
#include <string>

#include "app_config/AppConfig.h"
// #include "flusher/FlusherSLS.h"
// #include "input/InputFile.h"
// #include "input/InputObserverNetwork.h"
#ifdef __ENTERPRISE__
#include "input/InputStream.h"
#endif
#include "logger/Logger.h"
#include "plugin/creator/CProcessor.h"
#include "plugin/creator/DynamicCProcessorCreator.h"
#include "plugin/creator/StaticFlusherCreator.h"
#include "plugin/creator/StaticInputCreator.h"
#include "plugin/creator/StaticProcessorCreator.h"
#include "processor/ProcessorDesensitizeNative.h"
#include "processor/ProcessorFilterNative.h"
Expand All @@ -43,9 +51,8 @@

namespace logtail {

PluginRegistry* PluginRegistry::GetInstance() {
static PluginRegistry instance;
return &instance;
PluginRegistry::PluginRegistry() {
mGoPlugins = {""};
}

void PluginRegistry::LoadPlugins() {
Expand All @@ -68,23 +75,32 @@ void PluginRegistry::UnloadPlugins() {
mPluginDict.clear();
}

std::unique_ptr<InputInstance> PluginRegistry::CreateInput(const std::string& name, const PluginInstance::PluginMeta& pluginMeta) {
return std::unique_ptr<InputInstance>(static_cast<InputInstance*>(Create(INPUT_PLUGIN, name, pluginMeta).release()));
}

std::unique_ptr<ProcessorInstance> PluginRegistry::CreateProcessor(const std::string& name,
const PluginInstance::PluginMeta& pluginMeta) {
return std::unique_ptr<ProcessorInstance>(
static_cast<ProcessorInstance*>(Create(PROCESSOR_PLUGIN, name, pluginMeta).release()));
}

std::unique_ptr<PluginInstance>
PluginRegistry::Create(PluginCat cat, const std::string& name, const PluginInstance::PluginMeta& pluginMeta) {
std::unique_ptr<PluginInstance> ins;
auto creatorEntry = mPluginDict.find(PluginKey(cat, name));
if (creatorEntry != mPluginDict.end()) {
ins = creatorEntry->second->Create(pluginMeta);
}
return ins;
std::unique_ptr<FlusherInstance> PluginRegistry::CreateFlusher(const std::string& name, const PluginInstance::PluginMeta& pluginMeta) {
return std::unique_ptr<FlusherInstance>(
static_cast<FlusherInstance*>(Create(FLUSHER_PLUGIN, name, pluginMeta).release()));
}

bool PluginRegistry::IsValidGoPlugin(const std::string& name) {
return mGoPlugins.find(name) != mGoPlugins.end();
}

void PluginRegistry::LoadStaticPlugins() {
// RegisterInputCreator(new StaticInputCreator<InputFile>());
// RegisterInputCreator(new StaticInputCreator<InputObserverNetwork>());
#ifdef __ENTERPRISE__
RegisterInputCreator(new StaticInputCreator<InputStream>());
#endif

RegisterProcessorCreator(new StaticProcessorCreator<ProcessorSplitLogStringNative>());
RegisterProcessorCreator(new StaticProcessorCreator<ProcessorSplitRegexNative>());
RegisterProcessorCreator(new StaticProcessorCreator<ProcessorParseApsaraNative>());
Expand All @@ -96,7 +112,7 @@ void PluginRegistry::LoadStaticPlugins() {
RegisterProcessorCreator(new StaticProcessorCreator<ProcessorTagNative>());
RegisterProcessorCreator(new StaticProcessorCreator<ProcessorFilterNative>());

/* more native plugin registers here */
// RegisterFlusherCreator(new StaticFlusherCreator<FlusherSLS>());
}

void PluginRegistry::LoadDynamicPlugins(const std::set<std::string>& plugins) {
Expand All @@ -119,6 +135,18 @@ void PluginRegistry::LoadDynamicPlugins(const std::set<std::string>& plugins) {
}
}

void PluginRegistry::RegisterInputCreator(PluginCreator* creator) {
RegisterCreator(INPUT_PLUGIN, creator);
}

void PluginRegistry::RegisterProcessorCreator(PluginCreator* creator) {
RegisterCreator(PROCESSOR_PLUGIN, creator);
}

void PluginRegistry::RegisterFlusherCreator(PluginCreator* creator) {
RegisterCreator(FLUSHER_PLUGIN, creator);
}

PluginCreator* PluginRegistry::LoadProcessorPlugin(DynamicLibLoader& loader, const std::string pluginName) {
std::string error;
processor_interface_t* plugin = (processor_interface_t*)loader.LoadMethod("processor_interface", error);
Expand Down Expand Up @@ -148,8 +176,14 @@ void PluginRegistry::RegisterCreator(PluginCat cat, PluginCreator* creator) {
mPluginDict.emplace(PluginKey(cat, creator->Name()), std::shared_ptr<PluginCreator>(creator));
}

void PluginRegistry::RegisterProcessorCreator(PluginCreator* creator) {
RegisterCreator(PROCESSOR_PLUGIN, creator);
std::unique_ptr<PluginInstance>
PluginRegistry::Create(PluginCat cat, const std::string& name, const PluginInstance::PluginMeta& pluginMeta) {
std::unique_ptr<PluginInstance> ins;
auto creatorEntry = mPluginDict.find(PluginKey(cat, name));
if (creatorEntry != mPluginDict.end()) {
ins = creatorEntry->second->Create(pluginMeta);
}
return ins;
}

} // namespace logtail
} // namespace logtail
47 changes: 29 additions & 18 deletions core/plugin/PluginRegistry.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 iLogtail Authors
* Copyright 2023 iLogtail Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,6 +23,8 @@

#include "common/DynamicLibHelper.h"
#include "plugin/creator/PluginCreator.h"
#include "plugin/instance/FlusherInstance.h"
#include "plugin/instance/InputInstance.h"
#include "plugin/instance/PluginInstance.h"
#include "plugin/instance/ProcessorInstance.h"

Expand All @@ -32,29 +34,24 @@ namespace logtail {

class PluginRegistry {
public:
static PluginRegistry* GetInstance();
// 加载所有插件
void LoadPlugins();
PluginRegistry(const PluginRegistry&) = delete;
PluginRegistry& operator=(const PluginRegistry&) = delete;

// 卸载所有插件
void UnloadPlugins();
static PluginRegistry* GetInstance() {
static PluginRegistry instance;
return &instance;
}

// std::unique_ptr<InputInstance> CreateInput(const std::string& name, const std::string& pluginId);
void LoadPlugins();
void UnloadPlugins();
std::unique_ptr<InputInstance> CreateInput(const std::string& name, const PluginInstance::PluginMeta& pluginMeta);
std::unique_ptr<ProcessorInstance> CreateProcessor(const std::string& name, const PluginInstance::PluginMeta& pluginMeta);
// std::unique_ptr<FlusherInstance> CreateFlusher(const std::string& name, const std::string& pluginId);
std::unique_ptr<FlusherInstance> CreateFlusher(const std::string& name, const PluginInstance::PluginMeta& pluginMeta);
bool IsValidGoPlugin(const std::string& name);

private:
enum PluginCat { INPUT_PLUGIN, PROCESSOR_PLUGIN, FLUSHER_PLUGIN };

void LoadStaticPlugins();
void LoadDynamicPlugins(const std::set<std::string>& plugins);
// void RegisterInputCreator(PluginCreator* creator);
void RegisterProcessorCreator(PluginCreator* creator);
// void RegisterFlusherCreator(PluginCreator* creator);
PluginCreator* LoadProcessorPlugin(DynamicLibLoader& loader, const std::string pluginName);
void RegisterCreator(PluginCat cat, PluginCreator* creator);
std::unique_ptr<PluginInstance> Create(PluginCat cat, const std::string& name, const PluginInstance::PluginMeta& pluginMeta);

struct PluginKey {
PluginCat cat;
std::string name;
Expand All @@ -67,11 +64,25 @@ class PluginRegistry {
return std::hash<int>()(obj.cat) ^ std::hash<std::string>()(obj.name);
}
};

PluginRegistry();
~PluginRegistry() = default;

void LoadStaticPlugins();
void LoadDynamicPlugins(const std::set<std::string>& plugins);
void RegisterInputCreator(PluginCreator* creator);
void RegisterProcessorCreator(PluginCreator* creator);
void RegisterFlusherCreator(PluginCreator* creator);
PluginCreator* LoadProcessorPlugin(DynamicLibLoader& loader, const std::string pluginName);
void RegisterCreator(PluginCat cat, PluginCreator* creator);
std::unique_ptr<PluginInstance> Create(PluginCat cat, const std::string& name, const PluginInstance::PluginMeta& pluginMeta);

std::unordered_map<PluginKey, std::shared_ptr<PluginCreator>, PluginKeyHash> mPluginDict;
std::unordered_set<std::string> mGoPlugins;

#ifdef APSARA_UNIT_TEST_MAIN
friend class PluginRegistryUnittest;
#endif
};

} // namespace logtail
} // namespace logtail
2 changes: 1 addition & 1 deletion core/plugin/creator/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2022 iLogtail Authors
# Copyright 2023 iLogtail Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
18 changes: 17 additions & 1 deletion core/plugin/creator/CProcessor.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2023 iLogtail Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#ifdef __cplusplus
Expand Down Expand Up @@ -30,4 +46,4 @@ typedef struct processor_instance_t {

#ifdef __cplusplus
}
#endif
#endif
4 changes: 2 additions & 2 deletions core/plugin/creator/DynamicCProcessorCreator.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 iLogtail Authors
* Copyright 2023 iLogtail Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -38,4 +38,4 @@ std::unique_ptr<PluginInstance> DynamicCProcessorCreator::Create(const PluginIns
return std::unique_ptr<ProcessorInstance>(new ProcessorInstance(plugin, pluginMeta));
}

} // namespace logtail
} // namespace logtail
4 changes: 2 additions & 2 deletions core/plugin/creator/DynamicCProcessorCreator.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 iLogtail Authors
* Copyright 2023 iLogtail Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -34,4 +34,4 @@ class DynamicCProcessorCreator : public PluginCreator {
void* _handle;
};

} // namespace logtail
} // namespace logtail
5 changes: 3 additions & 2 deletions core/plugin/creator/PluginCreator.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 iLogtail Authors
* Copyright 2023 iLogtail Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,4 +30,5 @@ class PluginCreator {
virtual bool IsDynamic() = 0;
virtual std::unique_ptr<PluginInstance> Create(const PluginInstance::PluginMeta& pluginMeta) = 0;
};
} // namespace logtail

} // namespace logtail
34 changes: 34 additions & 0 deletions core/plugin/creator/StaticFlusherCreator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2023 iLogtail Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include "plugin/creator/PluginCreator.h"
#include "plugin/instance/FlusherInstance.h"

namespace logtail {

template <typename T>
class StaticFlusherCreator : public PluginCreator {
public:
const char* Name() override { return T::sName.c_str(); }
bool IsDynamic() override { return false; }
std::unique_ptr<PluginInstance> Create(const std::string& pluginId) override {
return std::unique_ptr<FlusherInstance>(new FlusherInstance(new T, pluginId));
}
};

} // namespace logtail
34 changes: 34 additions & 0 deletions core/plugin/creator/StaticInputCreator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2023 iLogtail Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include "plugin/creator/PluginCreator.h"
#include "plugin/instance/InputInstance.h"

namespace logtail {

template <typename T>
class StaticInputCreator : public PluginCreator {
public:
const char* Name() override { return T::sName.c_str(); }
bool IsDynamic() override { return false; }
std::unique_ptr<PluginInstance> Create(const std::string& pluginId) override {
return std::unique_ptr<InputInstance>(new InputInstance(new T, pluginId));
}
};

} // namespace logtail
5 changes: 2 additions & 3 deletions core/plugin/creator/StaticProcessorCreator.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 iLogtail Authors
* Copyright 2023 iLogtail Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,11 +20,10 @@
#include "plugin/instance/ProcessorInstance.h"

namespace logtail {

template <typename T>
class StaticProcessorCreator : public PluginCreator {
public:
StaticProcessorCreator() {}
const char* Name() override { return T::sName.c_str(); }
bool IsDynamic() override { return false; }
std::unique_ptr<PluginInstance> Create(const PluginInstance::PluginMeta& pluginMeta) override {
Expand Down
2 changes: 1 addition & 1 deletion core/plugin/instance/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2022 iLogtail Authors
# Copyright 2023 iLogtail Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
Loading

0 comments on commit 20480fe

Please sign in to comment.