Skip to content
Closed
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
13 changes: 13 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,16 @@ load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_depe
go_rules_dependencies()

go_register_toolchains(go_version = GO_VERSION)

# TODO(bianpengyuan): remove this googleapis dep when upstream envoy-wasm repo merges
# https://github.com/envoyproxy/envoy/pull/5387
load("//extensions/stackdriver/opencensus:opencensus.bzl", "telemetry_googleapis")

telemetry_googleapis()

load("@telemetry_googleapis//:repository_rules.bzl", "switched_rules_by_language")

switched_rules_by_language(
name = "com_google_googleapis_imports",
cc = True,
)
15 changes: 15 additions & 0 deletions extensions/stackdriver/opencensus/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2018, OpenCensus 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.

licenses(["notice"]) # Apache 2.0
9 changes: 9 additions & 0 deletions extensions/stackdriver/opencensus/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Opencensus Library

This folder includes opencensus library copied from <https://github.com/census-instrumentation/opencensus-cpp> with customization. Even though opencensus-cpp repo has already had bazel setup, the original code won't work out of box within a wasm sandbox or in an envoy worker silo, so we have to copy and customize it. Customization includes:

* Remove threading and thread annotations due to silo thread contrain.
* Remove assertion and stdio since they are not yet well supported by the emscripten ABI. All assertions and logs are about checking and logging misuse of opencensus library. It should not be a concern here since the library code is used in a certain way.
* Customized stackdriver exporter using WASM sandbox gRPC API.

The library is mainly used to do data aggregation for some predefined metrics and export them to Stackdriver. The code in this directory should mostly remain unchanged unless bug is found.
56 changes: 56 additions & 0 deletions extensions/stackdriver/opencensus/common/internal/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# OpenCensus C++ internal libraries.
#
# Copyright 2017, OpenCensus 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.

load(
"@envoy//bazel:envoy_build_system.bzl",
"envoy_cc_library",
)

load(
"//extensions/stackdriver/opencensus:copts.bzl",
"DEFAULT_COPTS",
)

licenses(["notice"]) # Apache 2.0

envoy_cc_library(
name = "hash_mix",
hdrs = ["hash_mix.h"],
copts = DEFAULT_COPTS,
repository = "@envoy",
visibility = ["//extensions/stackdriver:__subpackages__"],
)

envoy_cc_library(
name = "random_lib",
srcs = ["random.cc"],
hdrs = ["random.h"],
copts = DEFAULT_COPTS,
repository = "@envoy",
visibility = ["//extensions/stackdriver:__subpackages__"],
deps = [
"@envoy//source/extensions/common/wasm/null:null_lib",
],
)

envoy_cc_library(
name = "string_vector_hash",
hdrs = ["string_vector_hash.h"],
copts = DEFAULT_COPTS,
repository = "@envoy",
visibility = ["//extensions/stackdriver:__subpackages__"],
deps = [":hash_mix"],
)
49 changes: 49 additions & 0 deletions extensions/stackdriver/opencensus/common/internal/hash_mix.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2018, OpenCensus 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.

#ifndef OPENCENSUS_COMMON_INTERNAL_HASH_MIX_H_
#define OPENCENSUS_COMMON_INTERNAL_HASH_MIX_H_

#include <cstddef>
#include <limits>

namespace opencensus {
namespace common {

// HashMix provides efficient mixing of hash values.
class HashMix final {
public:
HashMix() : hash_(1) {}

// Mixes in another *hashed* value.
void Mix(std::size_t hash) {
// A multiplier that has been found to provide good mixing.
constexpr std::size_t kMul =
static_cast<std::size_t>(0xdc3eb94af8ab4c93ULL);
hash_ *= kMul;
hash_ =
((hash << 19) | (hash >> (std::numeric_limits<size_t>::digits - 19))) +
hash;
}

size_t get() const { return hash_; }

private:
std::size_t hash_;
};

} // namespace common
} // namespace opencensus

#endif // OPENCENSUS_COMMON_INTERNAL_HASH_MIX_H_
53 changes: 53 additions & 0 deletions extensions/stackdriver/opencensus/common/internal/random.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2017, OpenCensus 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.

#include "opencensus/common/internal/random.h"

#include <cstring>

namespace opencensus {
namespace common {

uint64_t Generator::Random64() { return rng_(); }

Random* Random::GetRandom() {
static auto* const global_random = new Random;
return global_random;
}

uint32_t Random::GenerateRandom32() { return gen_.Random64(); }

uint64_t Random::GenerateRandom64() { return gen_.Random64(); }

float Random::GenerateRandomFloat() {
return static_cast<float>(gen_.Random64()) / static_cast<float>(UINT64_MAX);
}

double Random::GenerateRandomDouble() {
return static_cast<double>(gen_.Random64()) / static_cast<double>(UINT64_MAX);
}

void Random::GenerateRandomBuffer(uint8_t* buf, size_t buf_size) {
for (size_t i = 0; i < buf_size; i += sizeof(uint64_t)) {
uint64_t value = gen_.rng_();
if (i + sizeof(uint64_t) <= buf_size) {
memcpy(&buf[i], &value, sizeof(uint64_t));
} else {
memcpy(&buf[i], &value, buf_size - i);
}
}
}

} // namespace common
} // namespace opencensus
77 changes: 77 additions & 0 deletions extensions/stackdriver/opencensus/common/internal/random.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright 2017, OpenCensus 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.

#ifndef OPENCENSUS_COMMON_INTERNAL_RANDOM_H_
#define OPENCENSUS_COMMON_INTERNAL_RANDOM_H_

#include <cstddef>
#include <cstdint>
#include <random>

#ifndef NULL_PLUGIN
#include "api/wasm/cpp/proxy_wasm_intrinsics.h"
#else
#include "extensions/common/wasm/null/null.h"
using namespace Envoy::Extensions::Common::Wasm::Null::Plugin;
#endif

namespace opencensus {
namespace common {

class Generator {
public:
Generator() : rng_(proxy_getCurrentTimeNanoseconds()) {}
explicit Generator(uint64_t seed) : rng_(seed) {}

uint64_t Random64();

private:
friend class Random;

std::mt19937_64 rng_;
};

class Random {
public:
// Initializes and returns a singleton Random generator.
static Random* GetRandom();

// Generating functions.
// Generates a random uint32_t
uint32_t GenerateRandom32();
// Generates a random uint64_t
uint64_t GenerateRandom64();
// Generates a random float between [0.0, 1.0]
float GenerateRandomFloat();
// Generates a random double between [0.0, 1.0]
double GenerateRandomDouble();
// Fills the given buffer with uniformly random bits.
void GenerateRandomBuffer(uint8_t* buf, size_t buf_size);

private:
Random() = default;

Random(const Random&) = delete;
Random(Random&&) = delete;
Random& operator=(const Random&) = delete;
Random& operator=(Random&&) = delete;

uint64_t GenerateValue();
Generator gen_;
};

} // namespace common
} // namespace opencensus

#endif // OPENCENSUS_COMMON_INTERNAL_RANDOM_H_
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2017, OpenCensus 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.

#ifndef OPENCENSUS_COMMON_INTERNAL_STRING_VECTOR_HASH_H_
#define OPENCENSUS_COMMON_INTERNAL_STRING_VECTOR_HASH_H_

#include <cstddef>
#include <string>
#include <vector>

#include "opencensus/common/internal/hash_mix.h"

namespace opencensus {
namespace common {

struct StringVectorHash {
std::size_t operator()(const std::vector<std::string>& container) const {
std::hash<std::string> hasher;
HashMix mixer;
for (const auto& elem : container) {
mixer.Mix(hasher(elem));
}
return mixer.get();
}
};

} // namespace common
} // namespace opencensus

#endif // OPENCENSUS_COMMON_INTERNAL_STRING_VECTOR_HASH_H_
44 changes: 44 additions & 0 deletions extensions/stackdriver/opencensus/context/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# OpenCensus C++ Context library.
# See context.h for details.
#
# Copyright 2018, OpenCensus 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.

load(
"@envoy//bazel:envoy_build_system.bzl",
"envoy_cc_library",
)

load(
"//extensions/stackdriver/opencensus:copts.bzl",
"DEFAULT_COPTS",
)

envoy_cc_library(
name = "context",
srcs = [
"internal/context.cc",
"internal/with_context.cc",
],
hdrs = [
"context.h",
"with_context.h",
],
copts = DEFAULT_COPTS,
repository = "@envoy",
visibility = ["//extensions/stackdriver:__subpackages__"],
deps = [
"//extensions/stackdriver/opencensus/tags",
],
)
Loading