This repository has been archived by the owner on Oct 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 83
WIP: Add Grpc Interceptor and integration test #189
Open
chingor13
wants to merge
8
commits into
master
Choose a base branch
from
grpc-interceptor
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
dcd1ce0
Add integration test against grpc master with interceptor support
chingor13 0332cec
Run the grpc tests
chingor13 0188073
grpc needs zlib1g-dev to compile
chingor13 e4bdc23
only replace opencensus/opencensus branch
chingor13 bf5c737
Add deprecation notice to use the Grpc interceptor
chingor13 a8fdb11
Use the PHP only repo for the dev source - smaller repo
chingor13 fc97a48
Fix phpcs
chingor13 c89f316
Revert "Use the PHP only repo for the dev source - smaller repo"
chingor13 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,163 @@ | ||
<?php | ||
/** | ||
* 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. | ||
*/ | ||
|
||
namespace OpenCensus\Trace\Integrations\Grpc; | ||
|
||
use Grpc\Interceptor; | ||
use OpenCensus\Trace\Propagator\PropagatorInterface; | ||
use OpenCensus\Trace\Propagator\GrpcMetadataPropagator; | ||
use OpenCensus\Trace\Span; | ||
use OpenCensus\Trace\Tracer; | ||
|
||
/** | ||
* This interceptor creates a Span for each type of gRPC call and injects a | ||
* metadata attribute to propagate SpanContext to downstream services. | ||
* | ||
* Example: | ||
* ``` | ||
* $interceptor = new TraceInterceptor(); | ||
* $channel = new Grpc\Channel('127.0.0.1:1234'); | ||
* $channel = Grpc\Interceptor::intercept($channel, $interceptor); | ||
* $client = new MyGrpcClient('127.0.0.1:1234', $channel); | ||
* ``` | ||
*/ | ||
class TraceInterceptor extends Interceptor | ||
{ | ||
/* @var PropagatorInterface */ | ||
private $propagator; | ||
|
||
/** | ||
* Create a new TraceInterceptor. | ||
* | ||
* @param PropagatorInterface $propagator The | ||
*/ | ||
public function __construct($propagator = null) | ||
{ | ||
$this->propagator = $propagator ?: new GrpcMetadataPropagator(); | ||
} | ||
|
||
/** | ||
* This interceptor is for simple unary requests. | ||
* | ||
* @param string $method The name of the method to call | ||
* @param mixed $argument The argument to the method | ||
* @param array $metadata A metadata map to send to the server (optional) | ||
* @param array $options An array of options (optional) | ||
* @param callable $continuation The next interceptor to call | ||
*/ | ||
public function interceptUnaryUnary( | ||
$method, | ||
$argument, | ||
array $metadata, | ||
array $options, | ||
$continuation | ||
) { | ||
$spanOptions = [ | ||
'name' => 'grpc/simpleRequest', | ||
'attributes' => [ | ||
'method' => $method | ||
], | ||
'kind' => Span::KIND_CLIENT | ||
]; | ||
$metadata = $this->injectMetadata($metadata); | ||
|
||
return Tracer::inSpan($spanOptions, $continuation, [$method, $argument, $metadata, $options]); | ||
} | ||
|
||
/** | ||
* This interceptor is for client streaming requests. | ||
* | ||
* @param string $method The name of the method to call | ||
* @param array $metadata A metadata map to send to the server (optional) | ||
* @param array $options An array of options (optional) | ||
* @param callable $continuation The next interceptor to call | ||
*/ | ||
public function interceptStreamUnary( | ||
$method, | ||
array $metadata, | ||
array $options, | ||
$continuation | ||
) { | ||
$spanOptions = [ | ||
'name' => 'grpc/clientStreamRequest', | ||
'attributes' => [ | ||
'method' => $method | ||
], | ||
'kind' => Span::KIND_CLIENT | ||
]; | ||
$metadata = $this->injectMetadata($metadata); | ||
return Tracer::inSpan($spanOptions, $continuation, [$method, $metadata, $options]); | ||
} | ||
|
||
/** | ||
* This interceptor is for server streaming requests. | ||
* | ||
* @param string $method The name of the method to call | ||
* @param mixed $argument The argument to the method | ||
* @param array $metadata A metadata map to send to the server (optional) | ||
* @param array $options An array of options (optional) | ||
* @param callable $continuation The next interceptor to call | ||
*/ | ||
public function interceptUnaryStream( | ||
$method, | ||
$argument, | ||
array $metadata, | ||
array $options, | ||
$continuation | ||
) { | ||
$spanOptions = [ | ||
'name' => 'grpc/serverStreamRequest', | ||
'attributes' => [ | ||
'method' => $method | ||
], | ||
'kind' => Span::KIND_CLIENT | ||
]; | ||
$metadata = $this->injectMetadata($metadata); | ||
return Tracer::inSpan($spanOptions, $continuation, [$method, $argument, $metadata, $options]); | ||
} | ||
|
||
/** | ||
* This interceptor is for server streaming requests. | ||
* | ||
* @param string $method The name of the method to call | ||
* @param array $metadata A metadata map to send to the server (optional) | ||
* @param array $options An array of options (optional) | ||
* @param callable $continuation The next interceptor to call | ||
*/ | ||
public function interceptStreamStream( | ||
$method, | ||
array $metadata, | ||
array $options, | ||
$continuation | ||
) { | ||
$spanOptions = [ | ||
'name' => 'grpc/bidiRequest', | ||
'attributes' => [ | ||
'method' => $method | ||
], | ||
'kind' => Span::KIND_CLIENT | ||
]; | ||
$metadata = $this->injectMetadata($metadata); | ||
return Tracer::inSpan($spanOptions, $continuation, [$method, $metadata, $options]); | ||
} | ||
|
||
private function injectMetadata(array $metadata) | ||
{ | ||
$context = Tracer::spanContext(); | ||
return $this->propagator->inject($context, $metadata); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"require": { | ||
"php": "^7.2", | ||
"opencensus/opencensus": "dev-master", | ||
"grpc/grpc": "dev-master", | ||
"ext-opencensus": "*", | ||
"ext-grpc": "*" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^7.0" | ||
}, | ||
"repositories": { | ||
"opencensus": { | ||
"type": "git", | ||
"url": "https://github.com/census-instrumentation/opencensus-php" | ||
}, | ||
"grpc": { | ||
"type": "git", | ||
"url": "https://github.com/grpc/grpc" | ||
} | ||
} | ||
} |
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,13 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit bootstrap="./vendor/autoload.php" colors="true"> | ||
<testsuites> | ||
<testsuite> | ||
<directory>tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
<filter> | ||
<whitelist> | ||
<directory suffix=".php">src</directory> | ||
</whitelist> | ||
</filter> | ||
</phpunit> |
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,27 @@ | ||
#!/bin/bash | ||
# 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. | ||
|
||
set -e | ||
|
||
pushd $(dirname ${BASH_SOURCE[0]}) | ||
source ../setup_test_repo.sh | ||
|
||
sed -i "s|\"opencensus/opencensus\": \"dev-master\"|\"opencensus/opencensus\": \"dev-${BRANCH}\"|" composer.json | ||
sed -i "s|https://github.com/census-instrumentation/opencensus-php|${REPO}|" composer.json | ||
composer install -n --prefer-dist | ||
|
||
vendor/bin/phpunit | ||
|
||
popd |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: fix this dependency when grpc is released with interceptors