Skip to content

[statsd] Add statsd exporter #543

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions .github/workflows/statsd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Statsd Metrics CI

on:
push:
branches: "*"
paths:
- "exporters/statsd/**"
- ".github/workflows/statsd.yml"
pull_request:
branches: [main]
paths:
- "exporters/statsd/**"
- ".github/workflows/statsd.yml"
jobs:
cmake_linux:
name: CMake on Linux
runs-on: ubuntu-latest
steps:
- name: checkout opentelemetry-cpp-contrib
uses: actions/checkout@v3
with:
path: opentelemetry-cpp-contrib
- name: checkout opentelemetry-cpp
uses: actions/checkout@v3
with:
repository: "open-telemetry/opentelemetry-cpp"
ref: "v1.20.0"
path: "opentelemetry-cpp"
submodules: "recursive"
- name: setup
run: |
sudo apt update -y
sudo apt install -y --no-install-recommends --no-install-suggests \
build-essential \
cmake \
ninja-build \
ca-certificates wget git valgrind lcov
- name: run tests
run: |
sudo $GITHUB_WORKSPACE/opentelemetry-cpp/ci/setup_googletest.sh
mkdir -p "$GITHUB_WORKSPACE/opentelemetry-cpp/build"
cd "$GITHUB_WORKSPACE/opentelemetry-cpp/build"
cmake .. -G Ninja -DOPENTELEMETRY_EXTERNAL_COMPONENT_PATH=$GITHUB_WORKSPACE/opentelemetry-cpp-contrib/exporters/statsd
cmake --build . -j$(nproc)
ctest -j1 --output-on-failure
144 changes: 144 additions & 0 deletions exporters/statsd/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
# Copyright 2021, OpenTelemetry 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.

cmake_minimum_required(VERSION 3.12)

# MAIN_PROJECT CHECK
## determine if statsd exporter is built as a subproject (using add_subdirectory) or if it is the main project
##
set(MAIN_PROJECT OFF)
if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
project(opentelemetry-cpp-statsd)
set(MAIN_PROJECT ON)
endif()

if (MAIN_PROJECT)
find_package(opentelemetry-cpp CONFIG QUIET)
if(opentelemetry-cpp_FOUND)
message("Using external opentelemetry-cpp")
else()
include(cmake/opentelemetry-cpp.cmake)
build_opentelemetry()
set(OPENTELEMETRY_CPP_INCLUDE_DIRS "")
set(OPENTELEMETRY_CPP_LIBRARIES "opentelemetry::libopentelemetry")
message("opentelemetry-cpp package was not found. Cloned from github")
endif()
endif()


include_directories(include)

# create statsd metrics exporter
if(WIN32)
add_library(
opentelemetry_exporter_statsd_metrics
src/exporter.cc src/etw_data_transport.cc
src/socket_data_transport.cc)
else()
add_library(opentelemetry_exporter_statsd_metrics
src/exporter.cc src/socket_data_transport.cc)
endif()

if(MAIN_PROJECT)
target_include_directories(opentelemetry_exporter_statsd_metrics
PRIVATE ${OPENTELEMETRY_CPP_INCLUDE_DIRS})
target_link_libraries(
opentelemetry_exporter_statsd_metrics
PUBLIC ${OPENTELEMETRY_CPP_LIBRARIES}
)
set_target_properties(opentelemetry_exporter_statsd_metrics
PROPERTIES EXPORT_NAME metrics)
else()
target_link_libraries(
opentelemetry_exporter_statsd_metrics
PUBLIC opentelemetry_trace opentelemetry_resources opentelemetry_common
)
endif()


if(MAIN_PROJECT)
option(WITH_EXAMPLES "Build examples" ON)
endif()

if (WITH_EXAMPLES)
add_subdirectory(example)
endif()

if(OPENTELEMETRY_INSTALL)
install(
TARGETS opentelemetry_exporter_statsd_metrics
EXPORT "${PROJECT_NAME}-target"
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})

install(
DIRECTORY include/opentelemetry/exporters/
DESTINATION include/opentelemetry/exporters/
FILES_MATCHING
PATTERN "*.h")
endif()

if(BUILD_TESTING)
if(MAIN_PROJECT)
find_package(GTest CONFIG REQUIRED)
else()
if (NOT DEFINED GTEST_BOTH_LIBRARIES)
message(STATUS_FATAL, "Test is not enable.")
endif()
endif()

endif() # BUILD_TESTING

if (MAIN_PROJECT)
# config file for find_packages(opentelemetry-cpp-statsd CONFIG)
include(GNUInstallDirs)
include(CMakePackageConfigHelpers)

set(OPENTELEMETRY_CPP_STATSD_VERSION "1.0.0")
set(INCLUDE_INSTALL_DIR "${CMAKE_INSTALL_INCLUDEDIR}")
configure_package_config_file(
"${CMAKE_CURRENT_LIST_DIR}/cmake/opentelemetry-cpp-statsd-config.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/cmake/${PROJECT_NAME}/${PROJECT_NAME}-config.cmake"
INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
# PATH_VARS OPENTELEMETRY_CPP_STATSD_VERSION PROJECT_NAME INCLUDE_INSTALL_DIR
# CMAKE_INSTALL_LIBDIR
PATH_VARS PROJECT_NAME INCLUDE_INSTALL_DIR CMAKE_INSTALL_LIBDIR
NO_CHECK_REQUIRED_COMPONENTS_MACRO)

# Write version file for find_packages(opentelemetry-cpp-statsd CONFIG)
write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/cmake/${PROJECT_NAME}/${PROJECT_NAME}-config-version.cmake"
VERSION ${OPENTELEMETRY_CPP_STATSD_VERSION}
COMPATIBILITY ExactVersion)

install(
FILES
"${CMAKE_CURRENT_BINARY_DIR}/cmake/${PROJECT_NAME}/${PROJECT_NAME}-config.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/cmake/${PROJECT_NAME}/${PROJECT_NAME}-config-version.cmake"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")

# Export all components
export(
EXPORT "${PROJECT_NAME}-target"
NAMESPACE "${PROJECT_NAME}::"
FILE "${CMAKE_CURRENT_BINARY_DIR}/cmake/${PROJECT_NAME}/${PROJECT_NAME}-target.cmake"
)

install(
EXPORT "${PROJECT_NAME}-target"
NAMESPACE "${PROJECT_NAME}::"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")

endif()
7 changes: 7 additions & 0 deletions exporters/statsd/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# statsd Exporter for OpenTelemetry C++

## SPEC
Check [metric-instrument-optional-refinements](https://github.com/open-telemetry/opentelemetry-specification/blob/main/oteps/metrics/0088-metric-instrument-optional-refinements.md#statsd) for what we plan to support.

## Installation
todo
59 changes: 59 additions & 0 deletions exporters/statsd/cmake/opentelemetry-cpp-statsd-config.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#.rst:
# opentelemetry-cpp-statsd.config.cmake
# --------
#
# Find the native opentelemetry-cpp-statsd includes and library.
#
#
# Result Variables
# ^^^^^^^^^^^^^^^^
#
# This module defines the following variables:
#
# ::
#
# OPENTELEMETRY_CPP_STATSD_INCLUDE_DIRS - Include directories of opentelemetry-cpp-statsd.
# OPENTELEMETRY_CPP_STATSD_LIBRARY_DIRS - Link directories of opentelemetry-cpp-statsd.
# OPENTELEMETRY_CPP_STATSD_LIBRARIES - List of libraries when using opentelemetry-cpp-statsd.
# OPENTELEMETRY_CPP_STATSD_FOUND - True if opentelemetry-cpp-statsd found.
# OPENTELEMETRY_CPP_STATSD_VERSION - Version of opentelemetry-cpp-statsd.
#
# ::
# opentelemetry-cpp-statsd::metrics - Imported target of oopentelemetry-cpp-statsd::metrics

# =============================================================================
# Copyright 2020 opentelemetry.
#
# Distributed under the Apache License (the "License"); see accompanying file
# LICENSE for details.
# =============================================================================
set(OPENTELEMETRY_CPP_STATSD_VERSION
"@OPENTELEMETRY_CPP_STATSD_VERSION@"
CACHE STRING "opentelemetry-cpp-statsd version" FORCE)

@PACKAGE_INIT@

message(OPENTELEMETRY_CPP_STATSD_VERSION)
find_package(Threads)

set_and_check(OPENTELEMETRY_CPP_STATSD_INCLUDE_DIRS "@PACKAGE_INCLUDE_INSTALL_DIR@")
set_and_check(OPENTELEMETRY_CPP_STATSD_LIBRARY_DIRS "@PACKAGE_CMAKE_INSTALL_LIBDIR@")

include("${CMAKE_CURRENT_LIST_DIR}/@[email protected]")

set(OPENTELEMETRY_CPP_STATSD_LIBRARIES)

set(_OPENTELEMETRY_CPP_STATSD_LIBRARIES_TEST_TARGETS metrics)

foreach(_TEST_TARGET IN LISTS _OPENTELEMETRY_CPP_STATSD_LIBRARIES_TEST_TARGETS)
if(TARGET opentelemetry-cpp-statsd::${_TEST_TARGET})
list(APPEND OPENTELEMETRY_CPP_STATSD_LIBRARIES opentelemetry-cpp-statsd::${_TEST_TARGET})
else()
message("Target not found: " ${_TEST_TARGET})
endif()
endforeach()

# handle the QUIETLY and REQUIRED arguments and set opentelemetry-cpp_FOUND to
# TRUE if all variables listed contain valid results, e.g. valid file paths.
include(CMakeFindDependencyMacro)
find_dependency(opentelemetry-cpp CONFIG)
70 changes: 70 additions & 0 deletions exporters/statsd/cmake/opentelemetry-cpp.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
if("${opentelemetry-cpp-tag}" STREQUAL "")
set(opentelemetry-cpp-tag "v1.20.0")
endif()
function(target_create _target _lib)
add_library(${_target} STATIC IMPORTED)
set_target_properties(
${_target} PROPERTIES IMPORTED_LOCATION
"${opentelemetry_BINARY_DIR}/${_lib}")
endfunction()

function(build_opentelemetry)
set(opentelemetry_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/opentelemetry-cpp")
set(opentelemetry_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/opentelemetry-cpp")
set(opentelemetry_cpp_targets opentelemetry_trace opentelemetry_logs)
set(opentelemetry_CMAKE_ARGS -DCMAKE_POSITION_INDEPENDENT_CODE=ON
-DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}
-DCMAKE_C_FLAGS=${CMAKE_C_FLAGS}
-DWITH_LOGS_PREVIEW=ON
-DBUILD_TESTING=OFF
-DWITH_EXAMPLES=OFF)

set(opentelemetry_libs
${opentelemetry_BINARY_DIR}/sdk/src/trace/libopentelemetry_trace.a
${opentelemetry_BINARY_DIR}/sdk/src/logs/libopentelemetry_logs.a
${opentelemetry_BINARY_DIR}/sdk/src/resource/libopentelemetry_resources.a
${opentelemetry_BINARY_DIR}/sdk/src/common/libopentelemetry_common.a
${CURL_LIBRARIES}
)

set(opentelemetry_include_dir ${opentelemetry_SOURCE_DIR}/api/include/
${opentelemetry_SOURCE_DIR}/ext/include/
${opentelemetry_SOURCE_DIR}/sdk/include/
)

include_directories(SYSTEM ${opentelemetry_include_dir})

set(opentelemetry_deps opentelemetry_trace opentelemetry_logs opentelemetry_resources opentelemetry_common ${CURL_LIBRARIES})

set(make_cmd ${CMAKE_COMMAND} --build <BINARY_DIR> --target
${opentelemetry_cpp_targets})

include(ExternalProject)
ExternalProject_Add(
opentelemetry-cpp
GIT_REPOSITORY https://github.com/open-telemetry/opentelemetry-cpp.git
GIT_TAG "${opentelemetry-cpp-tag}"
GIT_SUBMODULES "third_party/opentelemetry-proto"
SOURCE_DIR ${opentelemetry_SOURCE_DIR}
PREFIX "opentelemetry-cpp"
CMAKE_ARGS ${opentelemetry_CMAKE_ARGS}
BUILD_COMMAND ${make_cmd}
BINARY_DIR ${opentelemetry_BINARY_DIR}
INSTALL_COMMAND ""
BUILD_BYPRODUCTS ${opentelemetry_libs}
DEPENDS ${dependencies}
LOG_BUILD ON)

target_create("opentelemetry_trace" "sdk/src/trace/libopentelemetry_trace.a")
target_create("opentelemetry_logs" "sdk/src/logs/libopentelemetry_logs.a")
target_create("opentelemetry_resources"
"sdk/src/resource/libopentelemetry_resources.a")
target_create("opentelemetry_common"
"sdk/src/common/libopentelemetry_common.a")
add_library(opentelemetry::libopentelemetry INTERFACE IMPORTED)
add_dependencies(opentelemetry::libopentelemetry opentelemetry-cpp)
set_target_properties(
opentelemetry::libopentelemetry
PROPERTIES
INTERFACE_LINK_LIBRARIES "${opentelemetry_deps}")
endfunction()
Empty file.
Loading
Loading